package com.narutohuo.xindazhou.common.auth.datasource.remote

import com.narutohuo.xindazhou.common.auth.model.LoginRequest
import com.narutohuo.xindazhou.common.auth.model.LoginResponse
import com.narutohuo.xindazhou.common.auth.model.RegisterRequest
import com.narutohuo.xindazhou.common.network.ApiBaseRemoteDataSource

/**
 * 认证远程数据源接口
 */
interface AuthRemoteDataSource {
    /**
     * 用户登录
     */
    suspend fun login(request: LoginRequest): Result<LoginResponse>
    
    /**
     * 用户注册
     */
    suspend fun register(request: RegisterRequest): Result<LoginResponse>
    
    /**
     * 刷新 Token
     */
    suspend fun refreshToken(refreshToken: String): Result<LoginResponse>
}

/**
 * 认证远程数据源实现
 * 
 * 继承 ApiBaseRemoteDataSource，自动获得：
 * - 统一的错误处理
 * - 自动日志记录
 * - 网络异常友好提示
 * - 线程自动切换
 */
class AuthRemoteDataSourceImpl : ApiBaseRemoteDataSource(), AuthRemoteDataSource {
    
    private val authApi: AuthApi by lazy {
        com.narutohuo.xindazhou.common.network.ApiManager.create<AuthApi>()
    }
    
    override suspend fun login(request: LoginRequest): Result<LoginResponse> {
        return executeRequest(
            request = { authApi.login(request) },
            errorMessage = "登录失败"
        )
    }
    
    override suspend fun register(request: RegisterRequest): Result<LoginResponse> {
        return executeRequest(
            request = { authApi.register(request) },
            errorMessage = "注册失败"
        )
    }
    
    override suspend fun refreshToken(refreshToken: String): Result<LoginResponse> {
        return executeRequest(
            request = { authApi.refreshToken(refreshToken) },
            errorMessage = "Token 刷新失败"
        )
    }
}

