//
//  RoleTagApi.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/11/7.
//

import Moya

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

enum RoleTagApi {
    static let useMock: Bool = false
    
    case tagList(params: [String: Any])
}

extension RoleTagApi: TargetType {
    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 .tagList:
            return "/tag/list"
        }
    }

    var method: Moya.Method {
        return .post
    }
    
    var task: Task {
        var mParams = [String: Any]()
        switch self {
        case .tagList(let params):
            // 将传入的参数赋值给 mParams
            mParams = params
        }
        return .requestParameters(parameters: mParams, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return APIConfig.apiHeaders()
    }
    
    var sampleData: Data {
        switch self {
        case .tagList:
            return Data()
        }
    }
}
