//
//  ChatApi.swift

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

import Moya

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

enum ChatAPI {
    static let useMock: Bool = true
    
    case aiUserGiftSend(params: [String:Any])
    /// 查询解锁查看图片
    case viewAIUnlockAlbumImg(params: [String:Any])
    case aiUnlockAlbumImg(params: [String:Any])
    case letAISendPrologue(aiId: Int)
}

extension ChatAPI: TargetType {
    var baseURL: URL {
        return URL(string: APIConfig.frog)!
    }

    var path: String {
        switch self {
        case .aiUserGiftSend:
            return "/web/ai-user-gift/send"
        case .viewAIUnlockAlbumImg:
            return "/web/ai-user/view-unlock-album-img"
        case .aiUnlockAlbumImg:
            return "/web/ai-user/unlock-album-img"
        case .letAISendPrologue:
            return "/web/chat/send-dialogue-prologue-message"
        }
    }

    var method: Moya.Method {
        return .post
    }

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

        switch self {
        case .aiUserGiftSend(let params):
            mParams = params
        case .viewAIUnlockAlbumImg(let params):
            mParams = params
        case .aiUnlockAlbumImg(let params):
            mParams = params
        case .letAISendPrologue(let aiId):
            mParams.updateValue(aiId, forKey: "aiId")
        }

        mParams.updateValue("IOS", forKey: "appClient")
        mParams.updateValue(UIDevice.UUID, forKey: "deviceCode")

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

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

    var sampleData: Data {
        switch self {
        case .aiUserGiftSend:
            return Data()
        default:
            return Data()
        }
    }
}
