//
//  RouletteApi.swift
//  SodaLive
//
//  Created by klaus on 2023/12/06.
//

import Foundation
import Moya

enum RouletteApi {
    case getRoulette(creatorId: Int)
    case getAllRoulette(creatorId: Int)
    case createRoulette(request: CreateRouletteRequest)
    case updateRoulette(request: UpdateRouletteRequest)
    case spinRoulette(request: SpinRouletteRequest)
    case refundRouletteDonation(roomId: Int)
}

extension RouletteApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .getRoulette, .createRoulette, .updateRoulette:
            return "/v2/roulette"
            
        case .getAllRoulette:
            return "/v2/roulette/creator"
            
        case .spinRoulette:
            return "/v2/roulette/spin"
            
        case .refundRouletteDonation(let roomId):
            return "/v2/roulette/refund/\(roomId)"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .getRoulette, .getAllRoulette:
            return .get
            
        case .createRoulette, .spinRoulette, .refundRouletteDonation:
            return .post
            
        case .updateRoulette:
            return .put
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .getRoulette(let creatorId):
            let parameters = [
                "creatorId": creatorId
            ] as [String : Any]
            
            return .requestParameters(
                parameters: parameters,
                encoding: URLEncoding.queryString
            )
            
        case .getAllRoulette(let creatorId):
            let parameters = [
                "creatorId": creatorId
            ] as [String : Any]
            
            return .requestParameters(
                parameters: parameters,
                encoding: URLEncoding.queryString
            )
            
        case .createRoulette(let request):
            return .requestJSONEncodable(request)
            
        case .updateRoulette(let request):
            return .requestJSONEncodable(request)
            
        case .spinRoulette(let request):
            return .requestJSONEncodable(request)
            
        case .refundRouletteDonation:
            return .requestPlain
        }
    }
    
    var headers: [String : String]? {
        return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
    }
}
