//
//  TravelAlbumAPI.swift
//  suixinkan
//
//  Created by Codex on 2026/6/29.
//

import Foundation

private let travelAlbumBase = "/api/yf-handset-app/photog/travel-album"

/// 旅拍相册服务协议，便于 ViewModel 测试替换。
@MainActor
protocol TravelAlbumServing {
    func availableOrders() async throws -> [TravelAlbumAvailableOrder]
    func createAlbum(_ request: TravelAlbumCreateRequest) async throws -> TravelAlbumCreateResponse
    func albumList(page: Int, pageSize: Int) async throws -> ListPayload<TravelAlbumItem>
    func albumInfo(id: Int) async throws -> TravelAlbumItem
    func editAlbum(_ request: TravelAlbumEditRequest) async throws
    func deleteAlbum(_ request: TravelAlbumDeleteRequest) async throws
    func materialList(
        userEquityTravelId: Int,
        page: Int,
        pageSize: Int,
        orderBy: Int,
        isPurchased: Int?
    ) async throws -> ListPayload<TravelAlbumMaterial>
    func uploadMaterial(_ request: TravelAlbumUploadMaterialRequest) async throws -> TravelAlbumMaterial
    func deleteMaterial(_ request: TravelAlbumDeleteMaterialRequest) async throws
    func mpCode(id: Int) async throws -> TravelAlbumMpCodeResponse
}

/// 旅拍相册 API，封装 travel-album 相关接口。
@MainActor
final class TravelAlbumAPI: TravelAlbumServing {
    private let client: APIClient

    /// 初始化旅拍相册 API。
    init(client: APIClient) {
        self.client = client
    }

    /// 获取可绑定订单列表。
    func availableOrders() async throws -> [TravelAlbumAvailableOrder] {
        try await client.send(
            APIRequest(method: .get, path: "\(travelAlbumBase)/available-order")
        )
    }

    /// 创建旅拍相册任务。
    func createAlbum(_ request: TravelAlbumCreateRequest) async throws -> TravelAlbumCreateResponse {
        try await client.send(
            APIRequest(method: .post, path: "\(travelAlbumBase)/create", body: request)
        )
    }

    /// 获取旅拍相册列表。
    func albumList(page: Int, pageSize: Int) async throws -> ListPayload<TravelAlbumItem> {
        try await client.send(
            APIRequest(
                method: .get,
                path: "\(travelAlbumBase)/list",
                queryItems: [
                    URLQueryItem(name: "page", value: "\(max(page, 1))"),
                    URLQueryItem(name: "page_size", value: "\(max(pageSize, 1))")
                ]
            )
        )
    }

    /// 获取旅拍相册详情。
    func albumInfo(id: Int) async throws -> TravelAlbumItem {
        try await client.send(
            APIRequest(
                method: .get,
                path: "\(travelAlbumBase)/info",
                queryItems: [URLQueryItem(name: "id", value: "\(id)")]
            )
        )
    }

    /// 编辑旅拍相册。
    func editAlbum(_ request: TravelAlbumEditRequest) async throws {
        _ = try await client.send(
            APIRequest(method: .post, path: "\(travelAlbumBase)/edit", body: request)
        ) as EmptyPayload
    }

    /// 删除旅拍相册。
    func deleteAlbum(_ request: TravelAlbumDeleteRequest) async throws {
        _ = try await client.send(
            APIRequest(method: .post, path: "\(travelAlbumBase)/delete", body: request)
        ) as EmptyPayload
    }

    /// 获取相册素材列表。
    func materialList(
        userEquityTravelId: Int,
        page: Int,
        pageSize: Int,
        orderBy: Int = 2,
        isPurchased: Int? = nil
    ) async throws -> ListPayload<TravelAlbumMaterial> {
        var queryItems = [
            URLQueryItem(name: "user_equity_travel_id", value: "\(userEquityTravelId)"),
            URLQueryItem(name: "page", value: "\(max(page, 1))"),
            URLQueryItem(name: "page_size", value: "\(max(pageSize, 1))"),
            URLQueryItem(name: "order_by", value: "\(max(orderBy, 1))")
        ]
        if let isPurchased {
            queryItems.append(URLQueryItem(name: "is_purchased", value: "\(isPurchased)"))
        }
        return try await client.send(
            APIRequest(
                method: .get,
                path: "\(travelAlbumBase)/material-list",
                queryItems: queryItems
            )
        )
    }

    /// 登记已上传 OSS 的素材。
    func uploadMaterial(_ request: TravelAlbumUploadMaterialRequest) async throws -> TravelAlbumMaterial {
        try await client.send(
            APIRequest(method: .post, path: "\(travelAlbumBase)/upload-material", body: request)
        )
    }

    /// 删除相册素材。
    func deleteMaterial(_ request: TravelAlbumDeleteMaterialRequest) async throws {
        _ = try await client.send(
            APIRequest(method: .post, path: "\(travelAlbumBase)/delete-material", body: request)
        ) as EmptyPayload
    }

    /// 获取小程序码。
    func mpCode(id: Int) async throws -> TravelAlbumMpCodeResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "\(travelAlbumBase)/mp-code",
                queryItems: [URLQueryItem(name: "id", value: "\(id)")]
            )
        )
    }
}
