//
//  ProfileAPI.swift
//  suixinkan
//
//  Created by Codex on 2026/6/20.
//

import Foundation
import Observation

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

    /// 初始化个人信息 API，并注入共享网络客户端。
    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 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 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",
                body: EmptyPayload()
            )
        )
    }

    /// 提交实名认证资料。
    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
            )
        )
    }
}

extension ProfileAPI: UserProfileServing {}
