package com.example.ops_android.data.repository

import com.example.ops_android.data.local.SessionManager
import com.example.ops_android.data.model.Cookie
import com.example.ops_android.data.model.UserDetail
import com.example.ops_android.data.remote.api.*

class AuthRepository(
    private val userApi: UserApi,
    private val sessionManager: SessionManager
) {
    suspend fun login(username: String, password: String, remember: Boolean = true): Result<Cookie> {
        return try {
            val response = userApi.login(LoginRequest(username, password, remember))
            if (response.errCode == 0 && response.data?.cookie != null) {
                val cookie = response.data.cookie
                sessionManager.saveCookie(cookie)
                Result.success(cookie)
            } else {
                Result.failure(Exception("登录失败 (err=${response.errCode}, cookie=${response.data?.cookie})"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun register(username: String, email: String, password: String): Result<Unit> {
        return try {
            val response = userApi.register(RegisterRequest(username, email, password))
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("注册失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun getUserInfo(): Result<Pair<com.example.ops_android.data.model.UserInfo?, UserDetail?>> {
        return try {
            val response = userApi.getUserInfo()
            if (response.errCode == 0 && response.data != null) {
                Result.success(Pair(response.data.user, response.data.userInfo))
            } else {
                Result.failure(Exception("获取用户信息失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun changePassword(oldPass: String, newPass: String): Result<Unit> {
        return try {
            val response = userApi.changePassword(ChangePasswordRequest(oldPass, newPass))
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("修改密码失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun changeEmail(newEmail: String): Result<Unit> {
        return try {
            val response = userApi.changeEmail(ChangeEmailRequest(newEmail))
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("修改邮箱失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun updateInfo(data: UpdateInfoRequest): Result<Unit> {
        return try {
            val response = userApi.updateInfo(data)
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("更新信息失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun logout() {
        sessionManager.logout()
    }

    suspend fun isLoggedIn(): Boolean = sessionManager.isLoggedIn()

    suspend fun restoreSession(): Boolean {
        if (!sessionManager.isLoggedIn()) return false
        val result = getUserInfo()
        return result.isSuccess
    }
}
