package com.narutohuo.xindazhou.user.repository

import com.narutohuo.xindazhou.user.datasource.local.AuthLocalDataSource
import com.narutohuo.xindazhou.user.datasource.local.AuthLocalDataSourceImpl
import com.narutohuo.xindazhou.user.datasource.remote.AuthRemoteDataSource
import com.narutohuo.xindazhou.user.datasource.remote.AuthRemoteDataSourceImpl
import com.narutohuo.xindazhou.core.log.ILog
import com.narutohuo.xindazhou.user.model.LoginRequest
import com.narutohuo.xindazhou.user.model.LoginResponse
import com.narutohuo.xindazhou.user.model.RegisterRequest

/**
 * 认证数据仓库
 * 
 * 负责统一管理认证相关的数据获取和缓存
 */
class AuthRepository(
    private val remoteDataSource: AuthRemoteDataSource = AuthRemoteDataSourceImpl(),
    private val localDataSource: AuthLocalDataSource = AuthLocalDataSourceImpl()
) {
    
    companion object {
        private const val TAG = "AuthRepository"
    }
    
    /**
     * 用户登录
     * 
     * @param mobile 手机号
     * @param password 密码
     * @return 登录结果
     */
    suspend fun login(mobile: String, password: String): Result<LoginResponse> {
        return try {
            val request = LoginRequest(mobile, password)
            
            // 从远程数据源获取数据
            val result = remoteDataSource.login(request)
            
            // 如果成功，保存到本地
            result.onSuccess { response ->
                localDataSource.saveToken(response)
                ILog.d(TAG, "login: Token已保存")
            }
            
            result
        } catch (e: Exception) {
            ILog.e(TAG, "login: 登录异常", e)
            Result.failure(e)
        }
    }
    
    /**
     * 用户注册
     * 
     * @param mobile 手机号
     * @param password 密码
     * @return 注册结果
     */
    suspend fun register(mobile: String, password: String): Result<LoginResponse> {
        return try {
            val request = RegisterRequest(mobile, password)
            
            // 从远程数据源获取数据
            val result = remoteDataSource.register(request)
                
            // 如果成功，保存到本地
            result.onSuccess { response ->
                localDataSource.saveToken(response)
                ILog.d(TAG, "register: Token已保存")
            }
            
            result
        } catch (e: Exception) {
            ILog.e(TAG, "register: 注册异常", e)
            Result.failure(e)
        }
    }
    
    /**
     * 刷新 Token
     * 
     * @param refreshToken 刷新令牌
     * @return 刷新结果（包含新的 accessToken 和 refreshToken）
     */
    suspend fun refreshToken(refreshToken: String): Result<LoginResponse> {
        return try {
            // 从远程数据源获取数据
            val result = remoteDataSource.refreshToken(refreshToken)
            
            // 如果成功，保存到本地
            result.onSuccess { response ->
                localDataSource.saveToken(response)
                ILog.d(TAG, "refreshToken: Token已刷新并保存")
            }
            
            result
        } catch (e: Exception) {
            ILog.e(TAG, "refreshToken: 刷新异常", e)
            Result.failure(e)
        }
    }
}

