//
//  TaskAPI.swift
//  suixinkan
//

import Foundation

@MainActor
/// 任务相关 API，封装创建任务、可选订单与云盘列表接口。
final class TaskAPI {
    private let client: APIClient

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

    /// 创建摄影师任务。
    func createTask(_ request: AddTaskRequest) async throws {
        let _: EmptyResponse = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/task/create",
                body: request
            )
        )
    }

    /// 拉取当前景区可关联订单列表。
    func availableOrders(scenicId: Int) async throws -> [AvailableOrder] {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/task/available-order",
                queryItems: [URLQueryItem(name: "scenic_id", value: String(scenicId))]
            )
        )
    }

    /// 拉取云盘文件列表。
    func cloudFileList(
        parentFolderId: Int,
        name: String = "",
        type: Int = 0,
        orderBy: Int = 2,
        page: Int = 1,
        pageSize: Int = 20
    ) async throws -> CloudFileListResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/cloud-driver/list",
                queryItems: [
                    URLQueryItem(name: "parent_folder_id", value: String(parentFolderId)),
                    URLQueryItem(name: "name", value: name),
                    URLQueryItem(name: "type", value: String(type)),
                    URLQueryItem(name: "order_by", value: String(orderBy)),
                    URLQueryItem(name: "page", value: String(page)),
                    URLQueryItem(name: "page_size", value: String(pageSize)),
                ]
            )
        )
    }

}

/// 空响应占位，用于无 data 字段的成功接口。
private struct EmptyResponse: Decodable, Sendable {}
