//
//  LiveAPI.swift
//  suixinkan
//
//  Created by Codex on 2026/6/25.
//

import Foundation
import Combine

/// 直播服务协议，定义直播管理和直播相册接口能力。
@MainActor
protocol LiveServing {
    func liveList(scenicId: Int, page: Int, pageSize: Int) async throws -> LiveListResponse
    func liveDetail(liveId: Int) async throws -> LiveEntity
    func liveCreate(_ request: LiveCreateRequest) async throws
    func liveStart(liveId: Int) async throws
    func liveStop(liveId: Int) async throws
    func liveFinish(liveId: Int) async throws
    func liveSetPushMode(liveId: Int, mode: Int) async throws
    func liveAlbumList(scenicId: Int, startTime: String?, endTime: String?, page: Int, pageSize: Int) async throws -> LiveAlbumFolderListResponse
    func liveAlbumCreateFolder(_ request: LiveAlbumCreateFolderRequest) async throws
    func liveAlbumDeleteFolder(folderId: Int) async throws
    func liveAlbumFolderDetail(folderId: Int) async throws -> LiveAlbumFolderItem
    func liveAlbumDeleteFiles(folderId: Int, fileIds: [Int]) async throws
}

@MainActor
/// 直播 API，封装手动直播和直播相册网络请求。
final class LiveAPI {
    private let client: APIClient

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

    /// 获取直播列表。
    func liveList(scenicId: Int, page: Int = 1, pageSize: Int = 10) async throws -> LiveListResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/manual-live/list",
                queryItems: [
                    URLQueryItem(name: "scenic_id", value: String(scenicId)),
                    URLQueryItem(name: "page", value: String(max(page, 1))),
                    URLQueryItem(name: "page_size", value: String(max(pageSize, 1)))
                ]
            )
        )
    }

    /// 获取直播详情。
    func liveDetail(liveId: Int) async throws -> LiveEntity {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/manual-live/detail",
                queryItems: [URLQueryItem(name: "live_id", value: String(liveId))]
            )
        )
    }

    /// 创建直播。
    func liveCreate(_ request: LiveCreateRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/app/manual-live/create", body: request)
        )
    }

    /// 开始直播。
    func liveStart(liveId: Int) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/app/manual-live/start", body: LiveControlRequest(liveId: liveId))
        )
    }

    /// 暂停直播。
    func liveStop(liveId: Int) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/app/manual-live/stop", body: LiveControlRequest(liveId: liveId))
        )
    }

    /// 结束直播。
    func liveFinish(liveId: Int) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/app/manual-live/finish", body: LiveControlRequest(liveId: liveId))
        )
    }

    /// 切换直播推流模式。
    func liveSetPushMode(liveId: Int, mode: Int) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/app/manual-live/set-push-mode", body: LivePushModeRequest(liveId: liveId, manualPushMode: mode))
        )
    }

    /// 获取直播相册列表。
    func liveAlbumList(
        scenicId: Int,
        startTime: String? = nil,
        endTime: String? = nil,
        page: Int = 1,
        pageSize: Int = 10
    ) async throws -> LiveAlbumFolderListResponse {
        var queryItems = [
            URLQueryItem(name: "scenic_id", value: String(scenicId)),
            URLQueryItem(name: "page", value: String(max(page, 1))),
            URLQueryItem(name: "page_size", value: String(max(pageSize, 1)))
        ]
        if let startTime = startTime?.liveTrimmed, !startTime.isEmpty {
            queryItems.append(URLQueryItem(name: "start_time", value: startTime))
        }
        if let endTime = endTime?.liveTrimmed, !endTime.isEmpty {
            queryItems.append(URLQueryItem(name: "end_time", value: endTime))
        }
        return try await client.send(
            APIRequest(method: .get, path: "/api/app/view-album/folders", queryItems: queryItems)
        )
    }

    /// 创建直播相册。
    func liveAlbumCreateFolder(_ request: LiveAlbumCreateFolderRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/app/view-album/create-folder", body: request)
        )
    }

    /// 删除直播相册。
    func liveAlbumDeleteFolder(folderId: Int) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(method: .post, path: "/api/app/view-album/delete-folder", body: LiveAlbumDeleteFolderRequest(folderId: folderId))
        )
    }

    /// 获取直播相册详情。
    func liveAlbumFolderDetail(folderId: Int) async throws -> LiveAlbumFolderItem {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/view-album/folder-detail",
                queryItems: [URLQueryItem(name: "folder_id", value: String(folderId))]
            )
        )
    }

    /// 删除直播相册内的素材。
    func liveAlbumDeleteFiles(folderId: Int, fileIds: [Int]) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/app/view-album/delete-files",
                body: LiveAlbumDeleteFilesRequest(folderId: folderId, fileIds: fileIds)
            )
        )
    }
}

extension LiveAPI: LiveServing {}
