//
//  CloudDriveAPI.swift
//  suixinkan
//

import Foundation

/// 云盘接口协议，便于 ViewModel 注入测试替身。
@MainActor
protocol CloudDriveServing {
    /// 拉取云盘文件列表。
    func cloudFileList(
        parentFolderId: Int,
        name: String,
        type: Int,
        orderBy: Int,
        page: Int,
        pageSize: Int
    ) async throws -> CloudFileListResponse

    /// 创建云盘文件夹。
    func createCloudFolder(parentFolderId: Int, name: String) async throws

    /// 登记上传到 OSS 后的云盘文件。
    func uploadCloudFile(parentFolderId: Int, fileUrl: String, fileName: String) async throws

    /// 删除云盘文件或文件夹。
    func deleteCloudFiles(_ request: CloudFileDeleteRequest) async throws

    /// 移动云盘文件或文件夹。
    func moveCloudFiles(_ request: CloudFileMoveRequest) async throws

    /// 检查是否允许上传云盘文件。
    func checkUploadPermission() async throws -> CloudUploadPermissionResponse

    /// 修改云盘文件夹名称。
    func modifyCloudFolderName(fileId: Int, name: String) async throws

    /// 修改云盘文件名称。
    func modifyCloudFileName(fileId: Int, fileName: String) async throws
}

@MainActor
/// 云盘 API，路径与 Android `NetworkApi` 的 cloud-driver 区域保持一致。
final class CloudDriveAPI: CloudDriveServing {
    private let client: APIClient

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

    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)),
                ]
            )
        )
    }

    func createCloudFolder(parentFolderId: Int, name: String) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/cloud-driver/folder-create",
                body: CloudFolderCreateRequest(parentFolderId: parentFolderId, name: name)
            )
        )
    }

    func uploadCloudFile(parentFolderId: Int, fileUrl: String, fileName: String) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/cloud-driver/file-upload",
                body: CloudFileUploadRequest(parentFolderId: parentFolderId, fileUrl: fileUrl, fileName: fileName)
            )
        )
    }

    func deleteCloudFiles(_ request: CloudFileDeleteRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/cloud-driver/delete",
                body: request
            )
        )
    }

    func moveCloudFiles(_ request: CloudFileMoveRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/cloud-driver/move",
                body: request
            )
        )
    }

    func checkUploadPermission() async throws -> CloudUploadPermissionResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/cloud-driver/check-upload-permission"
            )
        )
    }

    func modifyCloudFolderName(fileId: Int, name: String) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/cloud-driver/folder-edit",
                body: CloudFolderModifyRequest(fileId: fileId, name: name)
            )
        )
    }

    func modifyCloudFileName(fileId: Int, fileName: String) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/cloud-driver/file-edit",
                body: CloudFileModifyRequest(fileId: fileId, fileName: fileName)
            )
        )
    }
}
