//
//  LocationReportAPI.swift
//  suixinkan
//
//  Created by Codex on 2026/6/24.
//

import Foundation
import Combine

/// 位置上报服务协议，抽象上报和历史接口以便 ViewModel 测试替换。
@MainActor
protocol LocationReportServing {
    /// 提交一次位置上报。
    func reportLocation(staffId: Int, latitude: Double, longitude: Double, address: String, type: LocationReportType, scenicId: Int) async throws -> LocationReportSubmitResponse

    /// 获取位置上报历史列表。
    func locationReportList(staffId: Int, page: Int, pageSize: Int, type: LocationReportType, startDate: String?, endDate: String?) async throws -> ListPayload<LocationReportHistoryItem>

    /// 获取位置上报详情，用于超时提醒判断。
    func locationDetail(staffId: Int) async throws -> LocationDetailResponse
}

/// 位置上报 API，负责封装旧工程定位上报相关接口。
@MainActor
final class LocationReportAPI: LocationReportServing {
    private let client: APIClient

    /// 初始化位置上报 API，并注入共享网络客户端。
    init(client: APIClient) {
        self.client = client
    }

    /// 提交一次位置上报。
    func reportLocation(
        staffId: Int,
        latitude: Double,
        longitude: Double,
        address: String,
        type: LocationReportType,
        scenicId: Int
    ) async throws -> LocationReportSubmitResponse {
        try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/loacation/report",
                queryItems: [
                    URLQueryItem(name: "staff_id", value: "\(staffId)"),
                    URLQueryItem(name: "latitude", value: "\(latitude)"),
                    URLQueryItem(name: "longitude", value: "\(longitude)"),
                    URLQueryItem(name: "address", value: address),
                    URLQueryItem(name: "type", value: "\(type.rawValue)"),
                    URLQueryItem(name: "scenic_id", value: "\(scenicId)")
                ]
            )
        )
    }

    /// 获取位置上报历史列表。
    func locationReportList(
        staffId: Int,
        page: Int = 1,
        pageSize: Int = 20,
        type: LocationReportType = .all,
        startDate: String? = nil,
        endDate: String? = nil
    ) async throws -> ListPayload<LocationReportHistoryItem> {
        var query = [
            URLQueryItem(name: "staff_id", value: "\(staffId)"),
            URLQueryItem(name: "page", value: "\(max(page, 1))"),
            URLQueryItem(name: "page_size", value: "\(max(pageSize, 1))"),
            URLQueryItem(name: "type", value: "\(max(type.rawValue, 0))")
        ]
        if let startDate, !startDate.isEmpty {
            query.append(URLQueryItem(name: "start_date", value: startDate))
        }
        if let endDate, !endDate.isEmpty {
            query.append(URLQueryItem(name: "end_date", value: endDate))
        }
        return try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/photog/loacation/list", queryItems: query)
        )
    }

    /// 获取位置上报详情。
    func locationDetail(staffId: Int) async throws -> LocationDetailResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/loacation/detail",
                queryItems: [URLQueryItem(name: "staff_id", value: "\(staffId)")]
            )
        )
    }
}
