//
//  SampleManagementAPI.swift
//  suixinkan
//

import Foundation

/// 样片管理服务协议，抽象列表、详情、上下架、上传与关联数据接口。
@MainActor
protocol SampleManagementServing {
    /// 拉取样片列表。
    func list(status: Int, keyword: String?, page: Int, pageSize: Int) async throws -> SampleMaterialListResponse

    /// 拉取样片详情。
    func detail(id: Int) async throws -> SampleDetail

    /// 设置样片上下架状态。
    func setListingStatus(id: Int, listingStatus: Int) async throws

    /// 上传样片相册。
    func upload(_ request: SampleUploadMaterialRequest) async throws

    /// 拉取景区全部打卡点。
    func scenicSpotListAll(scenicId: String) async throws -> ScenicSpotListResponse

    /// 拉取当前景区项目列表。
    func projectList(scenicId: String, name: String?, page: Int, pageSize: Int) async throws -> SampleProjectListResponse
}

/// 样片管理 API，封装 Android `NetworkApi` 中 media-album 与项目接口。
@MainActor
final class SampleManagementAPI: SampleManagementServing {
    private let client: APIClient

    /// 初始化样片管理 API。
    init(client: APIClient) {
        self.client = client
    }

    /// 拉取样片列表，固定 `type=2`。
    func list(status: Int, keyword: String?, page: Int = 1, pageSize: Int = 10) async throws -> SampleMaterialListResponse {
        var items = [
            URLQueryItem(name: "status", value: String(status)),
            URLQueryItem(name: "type", value: "2"),
            URLQueryItem(name: "page", value: String(max(page, 1))),
            URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
        ]
        if let keyword = keyword?.trimmingCharacters(in: .whitespacesAndNewlines), !keyword.isEmpty {
            items.append(URLQueryItem(name: "keyword", value: keyword))
        }
        return try await client.send(
            APIRequest(method: .get, path: "/api/app/media-album/list", queryItems: items)
        )
    }

    /// 拉取样片详情，固定 `type=2`。
    func detail(id: Int) async throws -> SampleDetail {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/media-album/detail",
                queryItems: [
                    URLQueryItem(name: "id", value: String(id)),
                    URLQueryItem(name: "type", value: "2"),
                ]
            )
        )
    }

    /// 设置样片上下架状态，固定 `type=2`。
    func setListingStatus(id: Int, listingStatus: Int) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/app/media-album/operation",
                queryItems: [
                    URLQueryItem(name: "id", value: String(id)),
                    URLQueryItem(name: "listing_status", value: String(listingStatus)),
                    URLQueryItem(name: "type", value: "2"),
                ]
            )
        )
    }

    /// 上传样片相册。
    func upload(_ request: SampleUploadMaterialRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/app/media-album/upload", body: request)
        )
    }

    /// 拉取景区全部打卡点。
    func scenicSpotListAll(scenicId: String) async throws -> ScenicSpotListResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/scenic-spot/list-all",
                queryItems: [URLQueryItem(name: "scenic_id", value: scenicId)]
            )
        )
    }

    /// 拉取当前景区项目列表。
    func projectList(
        scenicId: String,
        name: String? = nil,
        page: Int = 1,
        pageSize: Int = 10
    ) async throws -> SampleProjectListResponse {
        var items = [
            URLQueryItem(name: "scenic_id", value: scenicId),
            URLQueryItem(name: "page", value: String(max(page, 1))),
            URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
        ]
        if let name = name?.trimmingCharacters(in: .whitespacesAndNewlines), !name.isEmpty {
            items.append(URLQueryItem(name: "name", value: name))
        }
        return try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/photog/project/list", queryItems: items)
        )
    }
}
