//
//  PlaylistApi.swift
//  SodaLive
//
//  Created by klaus on 12/7/24.
//

import Foundation
import Moya

enum PlaylistApi {
    case getPlaylistList
    case createPlaylist(request: CreatePlaylistRequest)
    case getPlaylistDetail(playlistId: Int)
    case deletePlaylist(playlistId: Int)
    case updatePlaylist(playlistId: Int, request: UpdatePlaylistRequest)
}

extension PlaylistApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .getPlaylistList, .createPlaylist:
            return "/audio-content/playlist"
            
        case .getPlaylistDetail(let playlistId):
            return "/audio-content/playlist/\(playlistId)"
            
        case .deletePlaylist(let playlistId):
            return "/audio-content/playlist/\(playlistId)"
            
        case .updatePlaylist(let playlistId, _):
            return "/audio-content/playlist/\(playlistId)"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .getPlaylistList, .getPlaylistDetail:
            return .get
            
        case .createPlaylist:
            return .post
            
        case .deletePlaylist:
            return .delete
            
        case .updatePlaylist:
            return .put
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .getPlaylistList, .getPlaylistDetail, .deletePlaylist:
            return .requestPlain
            
        case .createPlaylist(let request):
            return .requestJSONEncodable(request)
            
        case .updatePlaylist(_, let request):
            return .requestJSONEncodable(request)
        }
    }
    
    var headers: [String : String]? {
        return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
    }
}
