//
//  MaterialManagementAPI.swift
//  suixinkan
//

import Foundation

/// 素材管理服务协议，抽象列表、详情、上下架、删除、标签、上传与编辑接口。
@MainActor
protocol MaterialManagementServing {
    /// 拉取素材列表。
    func list(status: Int, keyword: String?, page: Int, pageSize: Int) async throws -> MaterialListResponse

    /// 拉取素材详情。
    func detail(id: Int) async throws -> MaterialDetail

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

    /// 删除素材。
    func delete(id: Int) async throws

    /// 新增素材标签。
    func addTag(name: String) async throws -> Int

    /// 上传素材。
    func upload(_ request: MaterialUploadRequest) async throws

    /// 编辑素材。
    func edit(_ request: MaterialUploadRequest) async throws

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

/// 素材管理 API，封装 Android `NetworkApi` 中 `media-album type=1` 接口。
@MainActor
final class MaterialManagementAPI: MaterialManagementServing {
    private let client: APIClient

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

    /// 拉取素材列表，固定 `type=1`。
    func list(status: Int, keyword: String?, page: Int = 1, pageSize: Int = 10) async throws -> MaterialListResponse {
        var items = [
            URLQueryItem(name: "status", value: String(status)),
            URLQueryItem(name: "type", value: "1"),
            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=1`。
    func detail(id: Int) async throws -> MaterialDetail {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/media-album/detail",
                queryItems: [
                    URLQueryItem(name: "id", value: String(id)),
                    URLQueryItem(name: "type", value: "1"),
                ]
            )
        )
    }

    /// 设置素材上下架状态，固定 `type=1`。
    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: "1"),
                ]
            )
        )
    }

    /// 删除素材，固定 `type=1`。
    func delete(id: Int) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/app/media-album/delete",
                queryItems: [
                    URLQueryItem(name: "id", value: String(id)),
                    URLQueryItem(name: "type", value: "1"),
                ]
            )
        )
    }

    /// 新增素材标签。
    func addTag(name: String) async throws -> Int {
        try await client.send(
            APIRequest(
                method: .post,
                path: "/api/app/media-album/add-tag",
                queryItems: [URLQueryItem(name: "name", value: name)]
            )
        )
    }

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

    /// 编辑素材。
    func edit(_ request: MaterialUploadRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/app/media-album/edit", 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)]
            )
        )
    }
}
