//
//  NetworkSession.swift
//  punchnet
//
//  Created by 安礼成 on 2026/3/23.
//
import Foundation

struct AuthService {
    private static let baseParams: [String: Any] = [
        "client_id": SystemConfig.getClientId(),
        "mac": SystemConfig.macAddressString(mac: SystemConfig.getMacAddress())
    ]
    
    // 注册会话信息
    struct RegisterSession: Codable {
        let sessionId: Int
        
        enum CodingKeys: String, CodingKey {
            case sessionId = "session_id"
        }
    }
    
    // 重置会话信息
    struct ResetPasswordSession: Codable {
        let sessionId: Int
        
        enum CodingKeys: String, CodingKey {
            case sessionId = "session_id"
        }
    }
    
    static func loginWithAccountAndPassword(username: String, password: String) async throws -> NetworkSession {
        var params: [String: Any] = [
            "username": username,
            "password": password,
            "hostname": Host.current().name ?? "",
            "system": SystemConfig.systemInfo,
            "version": SystemConfig.version_name
        ]
        params.merge(baseParams) {$1}
        
        
        return try await SDLAPIClient.doPost(path: "/auth/login", params: params, as: NetworkSession.self)
    }
    
    static func loginWithToken(token: String) async throws -> NetworkSession {
        var params: [String: Any] = [
            "token": token,
            "hostname": Host.current().name ?? "",
            "system": SystemConfig.systemInfo,
            "version": SystemConfig.version_name
        ]
        params.merge(baseParams) {$1}
        
        NSLog("login params: \(params)")

        return try await SDLAPIClient.doPost(path: "/auth/token", params: params, as: NetworkSession.self)
    }
    
    static func requestRegisterVerifyCode(phoneNumber: String) async throws -> RegisterSession {
        var params: [String: Any] = [
            "username": phoneNumber
        ]
        params.merge(baseParams) {$1}
        
        return try await SDLAPIClient.doPost(path: "/register/sendVerfiyCode", params: params, as: RegisterSession.self)
    }
    
    static func submitRegisterVerifyCode(sessionId: Int, verifyCode: String) async throws -> String {
        var params: [String: Any] = [
            "session_id": sessionId,
            "code": verifyCode,
        ]
        params.merge(baseParams) {$1}
        
        return try await SDLAPIClient.doPost(path: "/register/verfiyCode", params: params, as: String.self)
    }
    
    static func register(sessionId: Int, password: String) async throws -> String {
        var params: [String: Any] = [
            "session_id": sessionId,
            "password": password
        ]
        params.merge(baseParams) {$1}
        
        return try await SDLAPIClient.doPost(path: "/register/submit", params: params, as: String.self)
    }
    
    static func requestResetPasswordVerifyCode(phoneNumber: String) async throws -> ResetPasswordSession {
        var params: [String: Any] = [
            "username": phoneNumber
        ]
        params.merge(baseParams) {$1}
        
        return try await SDLAPIClient.doPost(path: "/password/sendVerfiyCode", params: params, as: ResetPasswordSession.self)
    }
    
    static func submitResetPasswordVerifyCode(sessionId: Int, verifyCode: String) async throws -> String {
        var params: [String: Any] = [
            "session_id": sessionId,
            "code": verifyCode,
        ]
        params.merge(baseParams) {$1}
        
        return try await SDLAPIClient.doPost(path: "/password/verfiyCode", params: params, as: String.self)
    }
    
    static func resetPassword(sessionId: Int, newPassword: String) async throws -> String {
        var params: [String: Any] = [
            "session_id": sessionId,
            "new_password": newPassword,
        ]
        params.merge(baseParams) {$1}
        
        return try await SDLAPIClient.doPost(path: "/password/reset", params: params, as: String.self)
    }
    
}
