//
//  AuditionApi.swift
//  SodaLive
//
//  Created by klaus on 1/5/25.
//

import Foundation
import Moya

enum AuditionApi {
    case getAuditionList(page: Int, size: Int)
    case getAuditionDetail(auditionId: Int)
    case getAuditionRoleDetail(auditionRoleId: Int)
    case getAuditionApplicantList(auditionRoleId: Int, sortType: AuditionApplicantSortType, page: Int, size: Int)
    case applyAudition(parameters: [MultipartFormData])
}

extension AuditionApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .getAuditionList:
            return "/audition"
            
        case .getAuditionDetail(let auditionId):
            return "/audition/\(auditionId)"
            
        case .getAuditionRoleDetail(let auditionRoleId):
            return "/audition/role/\(auditionRoleId)"
            
        case .getAuditionApplicantList:
            return "/audition/applicant"
            
        case .applyAudition:
            return "/audition/applicant"
        }
    }
    
    var method: Moya.Method {
        switch self {
            
        case .getAuditionList, .getAuditionDetail, . getAuditionRoleDetail, .getAuditionApplicantList:
            return .get
            
        case .applyAudition:
            return .post
        }
    }
    
    var task: Moya.Task {
        switch self {
            
        case .getAuditionList(let page, let size):
            let parameters = [
                "page": page - 1,
                "size": size
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .getAuditionDetail, .getAuditionRoleDetail:
            return .requestPlain
            
        case .getAuditionApplicantList(let auditionRoleId, let sortType, let page, let size):
            let parameters = [
                "auditionRoleId": auditionRoleId,
                "page": page - 1,
                "size": size,
                "sortType": sortType
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .applyAudition(let parameters):
            return .uploadMultipart(parameters)
        }
    }
    
    var headers: [String : String]? {
        return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
    }
}
