//
//  CreatorCommunityApi.swift
//  SodaLive
//
//  Created by klaus on 2023/12/19.
//

import Foundation
import Moya

enum CreatorCommunityApi {
    case createCommunityPost(parameters: [MultipartFormData])
    case modifyCommunityPost(parameters: [MultipartFormData])
    case getCommunityPostList(creatorId: Int, page: Int, size: Int)
    case getCommunityPostDetail(postId: Int)
    case communityPostLike(postId: Int)
    case createCommunityPostComment(comment: String, postId: Int, parentId: Int?)
    case getCommunityPostCommentList(postId: Int, page: Int, size: Int)
    case getCommentReplyList(commentId: Int, page: Int, size: Int)
    case modifyComment(request: ModifyCommunityPostCommentRequest)
    case getLatestPostListFromCreatorsYouFollow
    case purchaseCommunityPost(postId: Int)
}

extension CreatorCommunityApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .createCommunityPost, .getCommunityPostList, .modifyCommunityPost:
            return "/creator-community"
            
        case .communityPostLike:
            return "/creator-community/like"
            
        case .createCommunityPostComment, .modifyComment:
            return "/creator-community/comment"
            
        case .getCommunityPostCommentList(let postId, _, _):
            return "/creator-community/\(postId)/comment"
            
        case .getCommentReplyList(let commentId, _, _):
            return "/creator-community/comment/\(commentId)"
            
        case .getCommunityPostDetail(let postId):
            return "/creator-community/\(postId)"
            
        case .getLatestPostListFromCreatorsYouFollow:
            return "/creator-community/latest"
            
        case .purchaseCommunityPost:
            return "/creator-community/purchase"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .createCommunityPost, .communityPostLike, .createCommunityPostComment, .purchaseCommunityPost:
            return .post
            
        case .getCommunityPostList, .getCommunityPostCommentList, .getCommentReplyList, .getCommunityPostDetail, .getLatestPostListFromCreatorsYouFollow:
            return .get
            
        case .modifyComment, .modifyCommunityPost:
            return .put
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .createCommunityPost(let parameters):
            return .uploadMultipart(parameters)
            
        case .modifyCommunityPost(let parameters):
            return .uploadMultipart(parameters)
            
        case .getCommunityPostList(let creatorId, let page, let size):
            let parameters = [
                "creatorId": creatorId,
                "page": page - 1,
                "size": size,
                "timezone": TimeZone.current.identifier
            ] as [String: Any]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .communityPostLike(let postId):
            let request = PostCommunityPostLikeRequest(postId: postId)
            return .requestJSONEncodable(request)
            
        case .createCommunityPostComment(let comment, let postId, let parentId):
            let request = CreateCommunityPostCommentRequest(comment: comment, postId: postId, parentId: parentId)
            return .requestJSONEncodable(request)
            
        case .getCommunityPostCommentList(_, let page, let size):
            let parameters = [
                "page": page - 1,
                "size": size,
                "timezone": TimeZone.current.identifier
            ] as [String: Any]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .getCommentReplyList(_, let page, let size):
            let parameters = [
                "page": page - 1,
                "size": size,
                "timezone": TimeZone.current.identifier
            ] as [String: Any]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .modifyComment(let request):
            return .requestJSONEncodable(request)
        
        case .getCommunityPostDetail:
            let parameters = ["timezone": TimeZone.current.identifier] as [String: Any]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .getLatestPostListFromCreatorsYouFollow:
            let parameters = ["timezone": TimeZone.current.identifier] as [String: Any]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .purchaseCommunityPost(let postId):
            return .requestJSONEncodable(PurchasePostRequest(postId: postId))
        }
    }
    
    var headers: [String : String]? {
        return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
    }
}
