//
//  ScheduleAPI.swift
//  suixinkan
//
//  Created by Codex on 2026/6/24.
//

import Foundation
import Combine

/// 排班服务协议，抽象排班列表、新增、删除和可关联订单接口。
@MainActor
protocol ScheduleServing {
    /// 获取指定月份有排班的日期集合。
    func monthScheduleDays(scenicId: Int, yearMonth: String) async throws -> [String]

    /// 获取指定日期的排班列表。
    func dayScheduleList(scenicId: Int, date: String) async throws -> [ScheduleItem]

    /// 新增排班。
    func addSchedule(_ request: AddScheduleRequest) async throws

    /// 删除排班。
    func deleteSchedule(id: Int) async throws

    /// 获取排班可关联订单。
    func availableOrderList(scenicId: Int) async throws -> [AvailableOrderResponse]
}

/// 排班 API，封装摄影师排班相关接口。
@MainActor
final class ScheduleAPI: ScheduleServing {
    private let client: APIClient

    /// 初始化排班 API，并注入共享网络客户端。
    init(client: APIClient) {
        self.client = client
    }

    /// 获取指定月份有排班的日期集合。
    func monthScheduleDays(scenicId: Int, yearMonth: String) async throws -> [String] {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/schedule/list-date",
                queryItems: [
                    URLQueryItem(name: "scenic_id", value: "\(scenicId)"),
                    URLQueryItem(name: "date", value: yearMonth)
                ]
            )
        )
    }

    /// 获取指定日期的排班列表。
    func dayScheduleList(scenicId: Int, date: String) async throws -> [ScheduleItem] {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/schedule/list",
                queryItems: [
                    URLQueryItem(name: "scenic_id", value: "\(scenicId)"),
                    URLQueryItem(name: "date", value: date)
                ]
            )
        )
    }

    /// 新增排班。
    func addSchedule(_ request: AddScheduleRequest) async throws {
        _ = try await client.send(
            APIRequest(method: .post, path: "/api/yf-handset-app/photog/schedule/add", body: request)
        ) as EmptyPayload
    }

    /// 删除排班。
    func deleteSchedule(id: Int) async throws {
        _ = try await client.send(
            APIRequest(method: .post, path: "/api/yf-handset-app/photog/schedule/delete", body: ScheduleDeleteRequest(id: id))
        ) as EmptyPayload
    }

    /// 获取排班可关联订单。
    func availableOrderList(scenicId: Int) async throws -> [AvailableOrderResponse] {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/task/available-order",
                queryItems: [URLQueryItem(name: "scenic_id", value: "\(scenicId)")]
            )
        )
    }
}
