//
//  HomeAPI.swift
//  suixinkan
//

import Foundation

@MainActor
/// 首页 API，封装权限、门店与位置上报相关接口。
final class HomeAPI {
    private let client: APIClient

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

    /// 拉取当前账号全部角色权限配置。
    func rolePermissions() async throws -> [RolePermissionResponse] {
        try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/role-permission")
        )
    }

    /// 拉取全部门店列表。
    func storeList() async throws -> StoreListResponse {
        try await client.send(
            APIRequest(method: .get, path: "/api/app/store/all")
        )
    }

    /// 拉取摄影师位置上报详情。
    func locationDetail(staffId: String) 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)]
            )
        )
    }

    /// 上报摄影师位置。
    func reportLocation(
        staffId: String,
        latitude: Double,
        longitude: Double,
        address: String,
        type: Int,
        scenicId: String
    ) async throws -> LocationReportResponse {
        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: String(latitude)),
                    URLQueryItem(name: "longitude", value: String(longitude)),
                    URLQueryItem(name: "address", value: address),
                    URLQueryItem(name: "type", value: String(type)),
                    URLQueryItem(name: "scenic_id", value: scenicId),
                ]
            )
        )
    }

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

    /// 拉取待审核权限申请列表。
    func roleApplyPending() async throws -> [RoleApplyPendingResponse] {
        try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/photog/role-apply/all")
        )
    }

    /// 拉取全部可选景区。
    func scenicListAll() async throws -> ScenicListAllResponse {
        try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/photog/scenic/list-all")
        )
    }

    /// 提交角色权限申请。
    func submitRoleApply(scenicIds: [Int], roleId: Int) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/role-apply/submit",
                body: RoleApplySubmitRequest(scenicId: scenicIds, roleId: roleId)
            )
        )
    }

    /// 拉取景区申请待审核列表。
    func scenicApplicationPending() async throws -> ScenicApplicationPendingsResponse {
        try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/photog/scenic-apply/all")
        )
    }

    /// 提交景区申请。
    func submitScenicApplication(_ request: ScenicApplicationSubmitRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/scenic-apply/submit",
                body: request
            )
        )
    }
}
