//
//  UserApi.swift
//  SodaLive
//
//  Created by klaus on 2023/08/09.
//

import Foundation
import Moya

enum UserApi {
    case login(request: LoginRequest)
    case signUp(parameters: [MultipartFormData])
    case findPassword(request: ForgotPasswordRequest)
    case searchUser(nickname: String)
    case getMypage
    case getMemberInfo
    case notification(request: UpdateNotificationSettingRequest)
    case logout
    case logoutAllDevice
    case signOut(request: SignOutRequest)
    case updatePushToken(request: PushTokenUpdateRequest)
}

extension UserApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .login:
            return "/member/login"
            
        case .signUp:
            return "/member/signup"
            
        case .findPassword:
            return "/forgot-password"
            
        case .searchUser:
            return "/member/search"
            
        case .getMypage:
            return "/member/mypage"
            
        case .getMemberInfo:
            return "/member/info"
            
        case .notification:
            return "/member/notification"
            
        case .logout:
            return "/member/logout"
            
        case .logoutAllDevice:
            return "/member/logout/all"
            
        case .signOut:
            return "/member/sign_out"
            
        case .updatePushToken:
            return "/member/push-token/update"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .login, .signUp, .findPassword, .notification, .logout, .logoutAllDevice, .signOut:
            return .post
            
        case .searchUser, .getMypage, .getMemberInfo:
            return .get
            
        case .updatePushToken:
            return .put
        }
    }
    
    var task: Task {
        switch self {
        case .login(let request):
            return .requestJSONEncodable(request)
            
        case .signUp(let parameters):
            return .uploadMultipart(parameters)
            
        case .findPassword(let request):
            return .requestJSONEncodable(request)
            
        case .searchUser(let nickname):
            return .requestParameters(parameters: ["nickname" : nickname], encoding: URLEncoding.queryString)
            
        case .getMypage:
            return .requestParameters(parameters: ["container" : "ios"], encoding: URLEncoding.queryString)
            
        case .getMemberInfo, .logout, .logoutAllDevice:
            return .requestPlain
            
        case .notification(let request):
            return .requestJSONEncodable(request)
            
        case .signOut(let request):
            return .requestJSONEncodable(request)
            
        case .updatePushToken(let request):
            return .requestJSONEncodable(request)
        }
    }
    
    var headers: [String : String]? {
        switch self {
        case .login, .signUp, .findPassword:
            return nil
            
        default:
            return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
        }
    }
}
