//
//  AuthAPI.swift
//  suixinkan
//
//  Created by Codex on 2026/6/18.
//

import Foundation
import Observation

@MainActor
@Observable
/// 登录认证 API，封装 v9 登录和账号选择相关接口。
final class AuthAPI {
    @ObservationIgnored private let client: APIClient

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

    /// 使用手机号和密码发起 v9 登录，返回临时 token 及可选账号列表。
    func login(username: String, password: String) async throws -> V9AuthResponse {
        try await client.send(
            APIRequest(
                method: .post,
                path: "/api/app/v9/login",
                body: LoginRequest(username: username, password: password)
            )
        )
    }

    /// 选择具体景区或门店账号，换取正式登录 token。
    func setUser(_ request: SetUserRequest, tokenOverride: String? = nil) async throws -> V9AuthResponse {
        try await client.send(
            APIRequest(
                method: .post,
                path: "/api/app/v9/set-user",
                body: request
            ),
            tokenOverride: tokenOverride
        )
    }
}
