//
//  WalletAPI.swift
//  suixinkan
//

import Foundation

@MainActor
/// 钱包服务协议，提供邀请奖励记录所需的汇总与流水接口。
protocol WalletServing {
    /// 获取钱包汇总金额，`type = 2` 表示邀请奖励。
    func walletSummary(type: Int) async throws -> WalletSummaryResponse

    /// 获取钱包流水列表。
    func walletTransactionList(
        startDate: String,
        endDate: String,
        page: Int,
        pageSize: Int,
        type: Int
    ) async throws -> WalletTransactionListResponse
}

@MainActor
/// 钱包页面服务协议，提供钱包首页、提现和积分兑现所需接口。
protocol WalletPageServing: WalletServing {
    /// 获取提现记录列表。
    func walletWithdrawList(page: Int, pageSize: Int) async throws -> WalletWithdrawListResponse

    /// 获取收益明细分组列表。
    func earningDetail(
        startDate: String,
        endDate: String,
        page: Int,
        pageSize: Int
    ) async throws -> EarningDetailResponse

    /// 获取提现申请页信息。
    func withdrawInfo() async throws -> WithdrawInfoResponse

    /// 发送提现短信验证码。
    func withdrawSendSMS() async throws

    /// 提交提现申请。
    func withdrawApply(amount: String, smsCode: String) async throws

    /// 获取积分概览。
    func pointOverview(staffId: Int) async throws -> PointOverviewResponse

    /// 提交积分提现申请。
    func pointWithdrawApply(points: Int, remark: String) async throws

    /// 获取积分提现记录。
    func pointWithdrawList(status: Int?, page: Int, pageSize: Int) async throws -> PointWithdrawListResponse
}

@MainActor
/// 钱包 API，封装钱包首页、提现、邀请奖励和积分兑现接口。
final class WalletAPI: WalletPageServing {
    private let client: APIClient

    /// 初始化钱包 API。
    init(client: APIClient) {
        self.client = client
    }

    /// 获取钱包汇总金额。
    func walletSummary(type: Int = 0) async throws -> WalletSummaryResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/wallet/summary",
                queryItems: [URLQueryItem(name: "type", value: String(type))]
            )
        )
    }

    /// 获取钱包流水列表。
    func walletTransactionList(
        startDate: String,
        endDate: String,
        page: Int = 1,
        pageSize: Int = 10,
        type: Int = 2
    ) async throws -> WalletTransactionListResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/wallet/transaction-list",
                queryItems: [
                    URLQueryItem(name: "start_date", value: startDate),
                    URLQueryItem(name: "end_date", value: endDate),
                    URLQueryItem(name: "page", value: String(max(page, 1))),
                    URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
                    URLQueryItem(name: "type", value: String(type)),
                ]
            )
        )
    }

    /// 获取提现记录列表。
    func walletWithdrawList(page: Int = 1, pageSize: Int = 10) async throws -> WalletWithdrawListResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/wallet/withdraw-list",
                queryItems: [
                    URLQueryItem(name: "page", value: String(max(page, 1))),
                    URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
                ]
            )
        )
    }

    /// 获取收益明细分组列表。
    func earningDetail(
        startDate: String,
        endDate: String,
        page: Int = 1,
        pageSize: Int = 10
    ) async throws -> EarningDetailResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/wallet/earning-detail",
                queryItems: [
                    URLQueryItem(name: "start_date", value: startDate),
                    URLQueryItem(name: "end_date", value: endDate),
                    URLQueryItem(name: "page", value: String(max(page, 1))),
                    URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
                ]
            )
        )
    }

    /// 获取提现申请页信息。
    func withdrawInfo() async throws -> WithdrawInfoResponse {
        try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/photog/wallet/withdraw-info")
        )
    }

    /// 发送提现短信验证码。
    func withdrawSendSMS() async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/wallet/withdraw-send-sms"
            )
        )
    }

    /// 提交提现申请。
    func withdrawApply(amount: String, smsCode: String) async throws {
        let request = WithdrawApplyRequest(amount: amount, smsCode: smsCode)
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/wallet/withdraw-apply",
                body: request
            )
        )
    }

    /// 获取积分概览。
    func pointOverview(staffId: Int) async throws -> PointOverviewResponse {
        try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/point/overview",
                queryItems: [URLQueryItem(name: "staff_id", value: String(staffId))]
            )
        )
    }

    /// 提交积分提现申请。
    func pointWithdrawApply(points: Int, remark: String = "积分提现申请") async throws {
        let request = PointWithdrawApplyRequest(points: points, remark: remark)
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/point/withdraw/apply",
                body: request
            )
        )
    }

    /// 获取积分提现记录。
    func pointWithdrawList(status: Int? = nil, page: Int = 1, pageSize: Int = 10) async throws -> PointWithdrawListResponse {
        let request = PointWithdrawListRequest(status: status, page: max(page, 1), pageSize: max(pageSize, 1))
        return try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/point/withdraw/list",
                body: request
            )
        )
    }
}
