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

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

/**
 * 认证远程数据源接口
 */
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 {
        ApiManager.create<AuthApi>()
    }
    
    override suspend fun login(request: LoginRequest): Result<LoginResponse> {
        return executeRequestResponse(
            request = { authApi.login(request) }
        ).toResult()
    }
    
    override suspend fun register(request: RegisterRequest): Result<LoginResponse> {
        return executeRequestResponse(
            request = { authApi.register(request) }
        ).toResult()
    }
    
    override suspend fun refreshToken(refreshToken: String): Result<LoginResponse> {
        return executeRequestResponse(
            request = { authApi.refreshToken(refreshToken) }
        ).toResult()
    }
}

