//
//  SearchApi.swift
//  SodaLive
//
//  Created by klaus on 3/27/25.
//

import Foundation
import Moya

enum SearchApi {
    case searchUnified(keyword: String, isAdultContentVisible: Bool, contentType: ContentType)
    case searchCreatorList(keyword: String, isAdultContentVisible: Bool, contentType: ContentType, page: Int, size: Int)
    case searchContentList(keyword: String, isAdultContentVisible: Bool, contentType: ContentType, page: Int, size: Int)
    case searchSeriesList(keyword: String, isAdultContentVisible: Bool, contentType: ContentType, page: Int, size: Int)
}

extension SearchApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .searchUnified:
            return "/search"
            
        case .searchCreatorList:
            return "/search/creators"
            
        case .searchContentList:
            return "/search/contents"
            
        case .searchSeriesList:
            return "/search/series"
        }
    }
    
    var method: Moya.Method {
        return .get
    }
    
    var task: Moya.Task {
        switch self {
        case .searchUnified(let keyword, let isAdultContentVisible, let contentType):
            let parameters = [
                "keyword": keyword,
                "isAdultContentVisible": isAdultContentVisible,
                "contentType": contentType
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .searchCreatorList(let keyword, let isAdultContentVisible, let contentType, let page, let size):
            let parameters = [
                "keyword": keyword,
                "isAdultContentVisible": isAdultContentVisible,
                "contentType": contentType,
                "page": page - 1,
                "size": size
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .searchContentList(let keyword, let isAdultContentVisible, let contentType, let page, let size):
            let parameters = [
                "keyword": keyword,
                "isAdultContentVisible": isAdultContentVisible,
                "contentType": contentType,
                "page": page - 1,
                "size": size
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .searchSeriesList(let keyword, let isAdultContentVisible, let contentType, let page, let size):
            let parameters = [
                "keyword": keyword,
                "isAdultContentVisible": isAdultContentVisible,
                "contentType": contentType,
                "page": page - 1,
                "size": size
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
        }
    }
    
    var headers: [String : String]? {
        return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
    }
}
