//
//  APIConfig.swift
//  WoolniOriginalSwift
//
//  Created by lyu dong on 2025/7/2.
//  Copyright © 2025 lydsnm. All rights reserved.
//

import Foundation
import Moya

struct APIConfig {
    #if CLPRODUCT
        static let environment: AppEnvironment = .product
    #elseif CLAPPSTORE
        static let environment: AppEnvironment = .appStore
    #else
        static let environment: AppEnvironment = .test
    #endif

    static let versionNum: Int = 100

    static let apiLogEnable: Bool = true

    /// 🔥Mock数据总开关
    #if DEBUG
    static let useMock: Bool = true
    #else
    static let useMock: Bool = false
    #endif

    enum AppEnvironment {
        case dev
        case test
        case product
        case appStore
    }

    private static var headers: [String: String]? {
        return ["content-type": "application/json",
                "accept": "application/json,text/plain"]
    }

    static func apiHeaders() -> [String: String]? {
        var updatHeaders = headers

        let platform = "IOS" // _\(Bundle.appVersion)
        updatHeaders?.updateValue(platform, forKey: "platform")

        let tokenNow = UserCore.shared.token
        if tokenNow.count > 0 {
            updatHeaders?.updateValue(tokenNow, forKey: "AUTH_TK")
        }

        updatHeaders?.updateValue("\(versionNum)", forKey: "versionNum")

        updatHeaders?.updateValue(UIDevice.UUID, forKey: "AUTH_DID")
        /*
         if let lan = Languages.preferedLans.first {
         updatHeaders?.updateValue(lan.rawValue, forKey: "accept-language")
         }

         if let userCountryCode = UserCore.shared.user?.countryCode, userCountryCode.isNotBlank{
         updatHeaders?.updateValue(userCountryCode, forKey: "country")
         }else if let regioncode = Locale.current.regionCode, regioncode.count > 0{
         updatHeaders?.updateValue(regioncode, forKey: "country")
         }
         */

        // --- did2 统一设备标识（加密）header传递
        //        let uuid = UIDevice.UUID
        //        let token = UserCore.shared.token
        //        let str = uuid
        //        let key = (token + "AHkt5aUUtO6HZPid").md5().uppercased()
        //        let aes = try! AES(key: key, iv: "HBB4UO5kEmM4169Z")
        //        let encrypted = try? aes.encrypt(str.bytes)
        //        let result = encrypted?.toBase64() ?? ""
        //        updatHeaders?.updateValue(result, forKey: "dId2")

        return updatHeaders
    }

    static var bear: String {
        return "https://test-bear.crushlevel.ai"
    }

    /// AI相关、登陆注册、用户管理、字典
    static var frog: String {
        return "https://test-frog.crushlevel.ai"
    }
    
    static var lion: String {
        return "https://test-lion.crushlevel.ai"
    }
    
    /// S3、
    static var shark: String {
        return "https://test-shark.crushlevel.ai"
    }
    
    /// 生成图片
    static var cow: String {
        return "https://test-cow.crushlevel.ai"
    }
    
    static var pigeon: String{
        return "https://test-pigeon.crushlevel.ai"
    }
}
