package dev.zxq5.chatapp.android.feature.auth

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dev.zxq5.chatapp.android.core.service.MessageStreamService
import dev.zxq5.chatapp.android.data.repository.AuthRepository
import dev.zxq5.chatapp.android.data.repository.LoginResult
import dev.zxq5.chatapp.android.data.repository.SignupResult
import dev.zxq5.chatapp.android.data.repository.AuthState
import dev.zxq5.chatapp.android.api.model.LoginState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class AuthViewModel(private val authRepository: AuthRepository) : ViewModel() {
    
    private val _loginState = MutableStateFlow<LoginState>(LoginState.Idle)
    val loginState: StateFlow<LoginState> = _loginState

    private val _authMode = MutableStateFlow(AuthMode.LOGIN)
    val authMode: StateFlow<AuthMode> = _authMode

    private val _authState = MutableStateFlow(authRepository.getAuthState())
    val authState: StateFlow<AuthState> = _authState

    private val _totpError = MutableStateFlow<String?>(null)
    val totpError: StateFlow<String?> = _totpError

    fun setAuthMode(mode: AuthMode) {
        _authMode.value = mode
        if (_loginState.value is LoginState.Error) {
            _loginState.value = LoginState.Idle
        }
    }

    fun signup(username: String, email: String, password: String, accessToken: String) {
        viewModelScope.launch {
            _loginState.value = LoginState.Loading
            when (val result = authRepository.signup(username, email, password, accessToken)) {
                is SignupResult.Success -> {
                    updateAuthState()
                    _loginState.value = LoginState.Success
                }
                is SignupResult.Error -> {
                    _loginState.value = LoginState.Error(result.message)
                }
            }
        }
    }

    fun login(username: String, password: String) {
        viewModelScope.launch {
            _loginState.value = LoginState.Loading
            when (val result = authRepository.login(username, password)) {
                is LoginResult.Success -> {
                    updateAuthState()
                    _loginState.value = LoginState.Success
                }
                is LoginResult.TotpRequired -> {
                    updateAuthState()
                    _loginState.value = LoginState.TwoFactorRequired
                }
                is LoginResult.Error -> {
                    _loginState.value = LoginState.Error(result.message)
                }
                is LoginResult.TotpError -> {
                    _loginState.value = LoginState.Error(result.message)
                }
            }
        }
    }

    fun verifyTotpLogin(code: String) {
        viewModelScope.launch {
            _loginState.value = LoginState.Loading
            when (val result = authRepository.verifyTotpLogin(code)) {
                is LoginResult.Success -> {
                    updateAuthState()
                    _loginState.value = LoginState.Success
                }
                is LoginResult.TotpError -> {
                    _totpError.value = result.message
                    _loginState.value = LoginState.TwoFactorRequired
                }
                is LoginResult.Error -> {
                    _loginState.value = LoginState.Error(result.message)
                }
                is LoginResult.TotpRequired -> {
                    _loginState.value = LoginState.TwoFactorRequired
                }
            }
        }
    }

    fun logout() {
        authRepository.logout()
        updateAuthState()
        _loginState.value = LoginState.Idle
    }

    private fun updateAuthState() {
        _authState.value = authRepository.getAuthState()
    }
    
    fun clearTotpError() {
        _totpError.value = null
    }
}
