//
//  PunchPointAPI.swift
//  suixinkan
//

import Foundation

/// 打卡点管理 API 协议，便于 ViewModel 单元测试注入替身。
@MainActor
protocol PunchPointAPIProtocol {
    /// 拉取打卡点列表。
    func list(scenicAreaId: String, status: Int, page: Int, pageSize: Int) async throws -> PunchPointListResponse
    /// 拉取打卡点详情。
    func info(id: Int64) async throws -> PunchPointDetail
    /// 新建打卡点。
    func add(_ request: PunchPointSaveRequest) async throws
    /// 编辑打卡点。
    func edit(_ request: PunchPointSaveRequest) async throws
    /// 删除打卡点。
    func delete(id: Int64) async throws
}

/// 打卡点管理 API，封装 Android `NetworkApi` 中 `photog/scenic-spot` 相关接口。
@MainActor
final class PunchPointAPI: PunchPointAPIProtocol {
    private let client: APIClient

    /// 初始化打卡点 API。
    init(client: APIClient) {
        self.client = client
    }

    /// 拉取打卡点列表。
    func list(scenicAreaId: String, status: Int, page: Int, pageSize: Int) async throws -> PunchPointListResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/scenic-spot/list",
                queryItems: [
                    URLQueryItem(name: "scenic_area_id", value: scenicAreaId),
                    URLQueryItem(name: "status", value: String(status)),
                    URLQueryItem(name: "page", value: String(max(page, 1))),
                    URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
                ]
            )
        )
    }

    /// 拉取打卡点详情。
    func info(id: Int64) async throws -> PunchPointDetail {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/scenic-spot/info",
                queryItems: [URLQueryItem(name: "id", value: String(id))]
            )
        )
    }

    /// 新建打卡点。
    func add(_ request: PunchPointSaveRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/yf-handset-app/photog/scenic-spot/add", body: request)
        )
    }

    /// 编辑打卡点。
    func edit(_ request: PunchPointSaveRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/yf-handset-app/photog/scenic-spot/edit", body: request)
        )
    }

    /// 删除打卡点。
    func delete(id: Int64) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/scenic-spot/delete",
                body: PunchPointIDRequest(id: id)
            )
        )
    }
}
