package com.rugvedsarika.privatelog.ui.auth

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.rugvedsarika.privatelog.utils.PreferenceHelper

class AuthViewModel(application: Application) : AndroidViewModel(application) {

    private val prefs = PreferenceHelper(application)

    private val _authState = MutableLiveData<AuthState>()
    val authState: LiveData<AuthState> = _authState

    private val _isPinSet = MutableLiveData<Boolean>()
    val isPinSet: LiveData<Boolean> = _isPinSet

    init {
        _isPinSet.value = prefs.isPinSet()
    }

    fun setPin(pin: String) {
        prefs.savePin(pin)
        _isPinSet.value = true
        _authState.value = AuthState.Authenticated
    }

    fun verifyPin(pin: String) {
        val storedPin = prefs.getPin()
        if (storedPin == pin) {
            _authState.value = AuthState.Authenticated
        } else {
            _authState.value = AuthState.InvalidPin
        }
    }

    fun biometricAuthSuccess() {
        _authState.value = AuthState.Authenticated
    }
}

sealed class AuthState {
    object Authenticated : AuthState()
    object InvalidPin : AuthState()
    object Idle : AuthState()
}
