//
//  NetworkServices.swift
//  suixinkan
//

import Foundation

@MainActor
/// 全局网络依赖容器，集中提供 APIClient 与各业务 API。
final class NetworkServices {
    static let shared = NetworkServices()

    let apiClient: APIClient
    let authAPI: AuthAPI
    let profileAPI: ProfileAPI
    let statisticsAPI: StatisticsAPI
    let orderAPI: OrderAPI
    let homeAPI: HomeAPI
    let paymentAPI: PaymentAPI
    let taskAPI: TaskAPI
    let inviteAPI: InviteAPI
    let walletAPI: WalletAPI
    let scenicQueueAPI: ScenicQueueAPI
    let travelAlbumAPI: TravelAlbumAPI
    let settingAPI: SettingAPI
    let sampleManagementAPI: SampleManagementAPI
    let materialManagementAPI: MaterialManagementAPI
    let punchPointAPI: PunchPointAPI
    let liveAPI: LiveAPI
    let cloudDriveAPI: CloudDriveAPI
    let messageCenterAPI: MessageCenterAPI
    let pushAPI: PushAPI
    let wildPhotographerReportAPI: WildPhotographerReportAPI
    let uploadAPI: UploadAPI
    let ossUploadService: OSSUploadService

    private init() {
        let client = APIClient(environment: .current)
        apiClient = client
        authAPI = AuthAPI(client: client)
        profileAPI = ProfileAPI(client: client)
        statisticsAPI = StatisticsAPI(client: client)
        orderAPI = OrderAPI(client: client)
        homeAPI = HomeAPI(client: client)
        paymentAPI = PaymentAPI(client: client)
        taskAPI = TaskAPI(client: client)
        inviteAPI = InviteAPI(client: client)
        walletAPI = WalletAPI(client: client)
        scenicQueueAPI = ScenicQueueAPI(client: client)
        travelAlbumAPI = TravelAlbumAPI(client: client)
        settingAPI = SettingAPI(client: client)
        sampleManagementAPI = SampleManagementAPI(client: client)
        materialManagementAPI = MaterialManagementAPI(client: client)
        punchPointAPI = PunchPointAPI(client: client)
        liveAPI = LiveAPI(client: client)
        cloudDriveAPI = CloudDriveAPI(client: client)
        messageCenterAPI = MessageCenterAPI(client: client)
        pushAPI = PushAPI(client: client)
        wildPhotographerReportAPI = WildPhotographerReportAPI(client: client)
        uploadAPI = UploadAPI(client: client)
        ossUploadService = OSSUploadService(configService: uploadAPI)
        client.bindAuthTokenProvider {
            let token = AppStore.shared.session.token.trimmingCharacters(in: .whitespacesAndNewlines)
            return token.isEmpty ? nil : token
        }
    }

    /// 测试专用初始化，允许注入 mock `APIClient`。
    init(apiClient: APIClient) {
        self.apiClient = apiClient
        authAPI = AuthAPI(client: apiClient)
        profileAPI = ProfileAPI(client: apiClient)
        statisticsAPI = StatisticsAPI(client: apiClient)
        orderAPI = OrderAPI(client: apiClient)
        homeAPI = HomeAPI(client: apiClient)
        paymentAPI = PaymentAPI(client: apiClient)
        taskAPI = TaskAPI(client: apiClient)
        inviteAPI = InviteAPI(client: apiClient)
        walletAPI = WalletAPI(client: apiClient)
        scenicQueueAPI = ScenicQueueAPI(client: apiClient)
        travelAlbumAPI = TravelAlbumAPI(client: apiClient)
        settingAPI = SettingAPI(client: apiClient)
        sampleManagementAPI = SampleManagementAPI(client: apiClient)
        materialManagementAPI = MaterialManagementAPI(client: apiClient)
        punchPointAPI = PunchPointAPI(client: apiClient)
        liveAPI = LiveAPI(client: apiClient)
        cloudDriveAPI = CloudDriveAPI(client: apiClient)
        messageCenterAPI = MessageCenterAPI(client: apiClient)
        pushAPI = PushAPI(client: apiClient)
        wildPhotographerReportAPI = WildPhotographerReportAPI(client: apiClient)
        uploadAPI = UploadAPI(client: apiClient)
        ossUploadService = OSSUploadService(configService: uploadAPI)
    }
}
