//
//  IMApi.swift

//
//  Created by Leon on 2025/8/19.
//

import Moya

let IMProvider = APIConfig.useMock && IMAPI.useMock
    ? MoyaProvider<IMAPI>(endpointClosure: myEndpointClosure, stubClosure: { target in
        let data = target.sampleData
        if data.count > 0 {
            return .delayed(seconds: 0.7)
        } else {
            return .never
        }
    })
    : MoyaProvider<IMAPI>(requestClosure: myRequestClosure)

enum IMAPI {
    static let useMock: Bool = true
    
    /// 获取IM账号登陆信息
    case getIMAccount
    case aiUserMsgFeedback(params: [String:Any])
    /// 系统消息统计
    case messageStat
    case messageList(params: [String:Any])
}

extension IMAPI: TargetType {
    var baseURL: URL {
        return URL(string: APIConfig.pigeon)!
    }

    var path: String {
        switch self {
        case .getIMAccount:
            return "/web/im-user/get-account"
        case .aiUserMsgFeedback:
            return "/web/fb/v1"
        case .messageStat:
            return "/web/message/stat"
        case .messageList:
            return "/web/message/list"
        }
    }

    var method: Moya.Method {
        return .post
    }

    var task: Task {
        var mParams = [String: Any]()

        mParams.updateValue("IOS", forKey: "appClient")
        mParams.updateValue(UIDevice.UUID, forKey: "deviceCode")
        
        switch self {
        case .getIMAccount:
            break
        case .aiUserMsgFeedback(let params):
            mParams = params
        case .messageStat:
            break
        case .messageList(let params):
            mParams = params
        }

        return .requestParameters(parameters: mParams, encoding: JSONEncoding.default)
    }

    var headers: [String: String]? {
        return APIConfig.apiHeaders()
    }

    var sampleData: Data {
        switch self {
//        case .userBaseInfo:
//            return .aiMockInfo()
//        case .messageStat:
//            let string = """
//                  {
//                    "unRead" : 12,
//                    "latestContent" : "最新的消息blabla",
//                    "latestTime" : 1756360311664
//                  }
//                """
//            return .okContent(string: string)
        default:
            return Data()
        }
    }
}
