//
//  ProfileAPI.swift
//  suixinkan
//

import Foundation

@MainActor
/// 个人信息 API，封装“我的”页面需要的用户资料和资料更新接口。
final class ProfileAPI {
    private let client: APIClient

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

    /// 拉取当前登录用户的基础资料。
    func userInfo() async throws -> UserInfoResponse {
        try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/userinfo")
        )
    }

    /// 拉取摄影师在当前景区的个人空间配置。
    func profileInfo(scenicId: Int) async throws -> PhotographerProfileInfoResponse {
        try await client.send(
            APIRequest(
                method: .get,
                path: "/api/yf-handset-app/photog/profile/info",
                queryItems: [URLQueryItem(name: "scenic_id", value: String(scenicId))]
            )
        )
    }

    /// 更新摄影师个人空间配置。
    func updateProfileInfo(_ request: UpdatePhotographerProfileInfoRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/profile/info-update",
                body: request
            )
        )
    }

    /// 添加摄影师日程。
    func addSchedule(_ request: AddPhotographerScheduleRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/schedule/add",
                body: request
            )
        )
    }

    /// 删除摄影师日程。
    func deleteSchedule(id: Int) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/schedule/delete",
                body: PhotographerScheduleDeleteRequest(id: id)
            )
        )
    }

    /// 上传摄影师头像，对齐 Android multipart 头像更新接口。
    func uploadAvatar(data: Data, fileName: String) async throws {
        let _: EmptyPayload = try await client.sendMultipart(
            path: "/api/yf-handset-app/photog/userinfo-update-avatar",
            fileName: fileName,
            mimeType: "image/jpeg",
            data: data,
            timeoutInterval: 60
        )
    }

    /// 拉取当前登录账号可切换的景区账号和门店账号。
    func switchableAccounts() async throws -> V9AuthResponse {
        try await client.send(
            APIRequest(method: .get, path: "/api/app/v9/accounts")
        )
    }

    /// 拉取当前登录用户的实名认证状态。
    func realNameInfo() async throws -> RealNameInfoResponse {
        try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/photog/real-name/info")
        )
    }

    /// 拉取当前登录用户的银行卡信息。
    func bankCardInfo() async throws -> BankCardInfoResponse {
        try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/photog/wallet/bank-card/info")
        )
    }

    /// 拉取开户银行列表。
    func bankList() async throws -> BankListResponse {
        try await client.send(
            APIRequest(method: .get, path: "/api/yf-handset-app/photog/wallet/bank-list")
        )
    }

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

    /// 更新用户昵称、密码或头像地址。
    func updateUserInfo(nickname: String? = nil, password: String? = nil, avatar: String? = nil) async throws {
        let request = UpdateInfoRequest(nickname: nickname, password: password, avatar: avatar)
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/userinfo-update",
                body: request
            )
        )
    }

    /// 单独更新用户头像 URL，供 OSS 上传完成后回写服务端。
    func updateUserAvatarURL(_ fileURL: String) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/userinfo-update-avatar-url",
                queryItems: [URLQueryItem(name: "file_url", value: fileURL)]
            )
        )
    }

    /// 发送实名认证短信验证码。
    func realNameSmsVerifyCode() async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/real-name/sms-verify-code"
            )
        )
    }

    /// 提交实名认证资料。
    func realNameSubmit(_ request: RealNameAuthRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/real-name/submit",
                body: request
            )
        )
    }

    /// 发送银行卡绑定短信验证码。
    func bankCardSmsVerifyCode() async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/wallet/bank-card/sms-verify-code",
                body: EmptyPayload()
            )
        )
    }

    /// 提交或更新银行卡信息。
    func updateBankCard(_ request: UpdateBankInfoRequest) async throws {
        let _: EmptyPayload = try await client.send(
            APIRequest(
                method: .post,
                path: "/api/yf-handset-app/photog/wallet/bank-card/update",
                body: request
            )
        )
    }
}
