//
//  ScenicPermissionAPI.swift
//  suixinkan
//
//  Created by Codex on 2026/6/23.
//

import Foundation
import Observation

/// 景区权限模块服务协议，定义景区选择、权限申请和景区申请所需接口。
@MainActor
protocol ScenicPermissionServing {
    /// 获取所有可申请景区列表。
    func scenicListAll() async throws -> ScenicListAllResponse

    /// 获取省市区地区树。
    func areas() async throws -> [ScenicAreaNode]

    /// 获取当前用户的景区申请记录。
    func scenicApplicationPendingAll() async throws -> ScenicApplicationPendingsResponse

    /// 提交新增景区入驻申请。
    func scenicSubmit(_ request: ScenicApplicationSubmitRequest) async throws

    /// 提交景区申请图片占位信息。
    func scenicApplicationUploadPlaceholder(_ items: [ScenicApplicationUploadPlaceholder]) async throws

    /// 获取当前用户的角色权限申请记录。
    func roleApplyAll() async throws -> [RoleApplyPendingResponse]

    /// 提交角色景区权限申请。
    func roleApplySubmit(roleId: Int, scenicIds: [Int]) async throws
}

@MainActor
@Observable
/// 景区权限 API，封装景区选择和申请相关网络请求。
final class ScenicPermissionAPI {
    @ObservationIgnored private let client: APIClient

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

    /// 获取所有可申请景区列表。
    func scenicListAll() async throws -> ScenicListAllResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/scenic/list-all"
            )
        )
    }

    /// 获取省市区地区树。
    func areas() async throws -> [ScenicAreaNode] {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/app/config/areas"
            )
        )
    }

    /// 获取当前用户的景区申请记录。
    func scenicApplicationPendingAll() async throws -> ScenicApplicationPendingsResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/scenic-apply/all"
            )
        )
    }

    /// 提交新增景区入驻申请。
    func scenicSubmit(_ request: ScenicApplicationSubmitRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/scenic-apply/submit",
                body: request
            )
        )
    }

    /// 提交景区申请图片占位信息。
    func scenicApplicationUploadPlaceholder(_ items: [ScenicApplicationUploadPlaceholder]) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/scenic-apply/upload-placeholder",
                body: ["files": items]
            )
        )
    }

    /// 获取当前用户的角色权限申请记录。
    func roleApplyAll() async throws -> [RoleApplyPendingResponse] {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/role-apply/all"
            )
        )
    }

    /// 提交角色景区权限申请。
    func roleApplySubmit(roleId: Int, scenicIds: [Int]) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/role-apply/submit",
                body: RoleApplySubmitRequest(scenicId: scenicIds, roleId: roleId)
            )
        )
    }
}

extension ScenicPermissionAPI: ScenicPermissionServing {}
