//
//  OssApi.swift

//
//  Created by Leon on 2025/7/29.
//

import Moya

let OssProvider = APIConfig.useMock && OssAPI.useMock
    ? MoyaProvider<OssAPI>(endpointClosure: myEndpointClosure, stubClosure: { target in
        let data = target.sampleData
        if data.count > 0 {
            return .delayed(seconds: 0.5)
        } else {
            return .never
        }
    })
    : MoyaProvider<OssAPI>(requestClosure: myRequestClosure)

enum OssAPI {
    static let useMock: Bool = false
    
    case getS3Token(bucketNameEnum: BucketS3Enum = .ROLE, suffix: SuffixS3Enum = .jpeg)
    case nsfwCheck(fileFullPath: String, s3BucketEnum: String = BucketS3Enum.ALBUM.rawValue)
}

extension OssAPI: TargetType {
    var baseURL: URL {
        return URL(string: APIConfig.shark)!
    }

    var path: String {
        switch self {
        case .getS3Token:
            return "/web/file/sts-tk"
        case .nsfwCheck:
            return "/web/file/check"
        }
    }

    var method: Moya.Method {
        return .post
    }

    var task: Task {
        var mParams = [String: Any]()

        switch self {
        case let .getS3Token(bucketNameEnum, suffix):
            mParams = ["bizTypeEnum": bucketNameEnum.rawValue, "suffix": suffix.rawValue]
        case let .nsfwCheck(fileFullPath, s3BucketEnum):
            mParams = ["fileFullPath": fileFullPath, "bizTypeEnum": s3BucketEnum]
        }

        mParams.updateValue("IOS", forKey: "appClient")
        mParams.updateValue(UIDevice.UUID, forKey: "deviceCode")
        
        return .requestParameters(parameters: mParams, encoding: JSONEncoding.default)
    }

    var headers: [String: String]? {
        return APIConfig.apiHeaders()
    }

    var sampleData: Data {
        switch self {
        case .getS3Token:
            #warning("to do")
            let mockJSON = """
              {

            }
            """
            return Data(mockJSON.utf8)
        case .nsfwCheck:
            let mockJSON = """
              {

            }
            """
            return Data(mockJSON.utf8)
        }
    }
}
