//
//  StatisticsAPI.swift
//  suixinkan
//

import Foundation

@MainActor
/// 数据统计 API，封装摄影师/景区管理员/店铺管理员三套统计接口。
final class StatisticsAPI {
    private let client: APIClient
    private let pageSize = 15

    init(client: APIClient) {
        self.client = client
    }

    /// 摄影师汇总统计。
    func photographerSummary(scenicId: Int, range: String) async throws -> StatisticsSummaryResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/analyse/user",
                queryItems: [
                    URLQueryItem(name: "scenic_id", value: String(scenicId)),
                    URLQueryItem(name: "range", value: range),
                ]
            )
        )
    }

    /// 摄影师日明细分页。
    func photographerDaily(
        scenicId: Int,
        startTime: String,
        endTime: String,
        page: Int
    ) async throws -> ListDataResponse<StatisticsDailyItem> {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/analyse/user/daily",
                queryItems: dailyQueryItems(
                    idName: "scenic_id",
                    idValue: scenicId,
                    startTime: startTime,
                    endTime: endTime,
                    page: page
                )
            )
        )
    }

    /// 景区管理员汇总统计。
    func scenicAdminSummary(scenicId: Int, range: String) async throws -> StatisticsSummaryResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/scenic-admin/analyse",
                queryItems: [
                    URLQueryItem(name: "scenic_id", value: String(scenicId)),
                    URLQueryItem(name: "range", value: range),
                ]
            )
        )
    }

    /// 景区管理员日明细分页。
    func scenicAdminDaily(
        scenicId: Int,
        startTime: String,
        endTime: String,
        page: Int
    ) async throws -> ListDataResponse<StatisticsDailyItem> {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/scenic-admin/analyse/daily",
                queryItems: dailyQueryItems(
                    idName: "scenic_id",
                    idValue: scenicId,
                    startTime: startTime,
                    endTime: endTime,
                    page: page
                )
            )
        )
    }

    /// 店铺管理员汇总统计。
    func storeSummary(storeId: Int, range: String) async throws -> StatisticsSummaryResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/store/analyse",
                queryItems: [
                    URLQueryItem(name: "store_id", value: String(storeId)),
                    URLQueryItem(name: "range", value: range),
                ]
            )
        )
    }

    /// 店铺管理员日明细分页。
    func storeDaily(
        storeId: Int,
        startTime: String,
        endTime: String,
        page: Int
    ) async throws -> ListDataResponse<StatisticsDailyItem> {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/store/analyse/daily",
                queryItems: dailyQueryItems(
                    idName: "store_id",
                    idValue: storeId,
                    startTime: startTime,
                    endTime: endTime,
                    page: page
                )
            )
        )
    }

    private func dailyQueryItems(
        idName: String,
        idValue: Int,
        startTime: String,
        endTime: String,
        page: Int
    ) -> [URLQueryItem] {
        [
            URLQueryItem(name: idName, value: String(idValue)),
            URLQueryItem(name: "start_time", value: startTime),
            URLQueryItem(name: "end_time", value: endTime),
            URLQueryItem(name: "page", value: String(page)),
            URLQueryItem(name: "paeg_size", value: String(pageSize)),
        ]
    }
}
