//
//  RoleApi.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/11/6.
//

import Moya

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

enum RoleAPI {
    static let useMock: Bool = false
    
    // 查询角色列表
    case characterList(params: [String: Any])
    
}

extension RoleAPI: TargetType {
    var method: Moya.Method {
        return .post
    }
    
    var task: Task {
        var mParams = [String: Any]()
        switch self {
        case .characterList(let params):
            // 将传入的参数赋值给 mParams
            mParams = params
        }
        return .requestParameters(parameters: mParams, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return APIConfig.apiHeaders()
    }
    
    var baseURL: URL {
        // 确保 URL 格式正确，避免强制解包导致的崩溃
        guard let url = URL(string: APIConfig.role) else {
            fatalError("Invalid baseURL: \(APIConfig.role)")
        }
        return url
    }
    
    var path: String {
        switch self {
        case .characterList:
            return "/character/list"
        }
    }
    
    var sampleData: Data {
        switch self {
        case .characterList:
            return Data()
        }
    }
}
