package com.diegocayo.organizame.presentation.features.main.profile

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.diegocayo.organizame.data.local.OrganizaMeDatabase
import com.diegocayo.organizame.data.local.preferences.PreferencesDataStore
import com.diegocayo.organizame.domain.model.User
import com.diegocayo.organizame.domain.repository.EventRepository
import com.diegocayo.organizame.domain.repository.SubjectRepository
import com.diegocayo.organizame.domain.repository.TaskRepository
import com.diegocayo.organizame.domain.usecase.GetUserUseCase
import com.diegocayo.organizame.domain.usecase.SetLoginStatusUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ProfileViewModel @Inject constructor(
    private val getUserUseCase: GetUserUseCase,
    private val setLoginStatusUseCase: SetLoginStatusUseCase,
    private val database: OrganizaMeDatabase,
    private val preferencesDataStore: PreferencesDataStore,
    private val taskRepository: TaskRepository,
    private val subjectRepository: SubjectRepository,
    private val eventRepository: EventRepository
) : ViewModel() {

    var user by mutableStateOf<User?>(null)
        private set

    init {
        loadUserData()
    }

    private fun loadUserData() {
        getUserUseCase.prepare(Unit)
            .onEach { userData ->
                user = userData
            }
            .catch { }
            .launchIn(viewModelScope)
    }

    fun logout(onLogoutComplete: () -> Unit) {
        setLoginStatusUseCase.prepare(false)
            .onEach {
                onLogoutComplete()
            }
            .catch { }
            .launchIn(viewModelScope)
    }

    fun deleteAccount(onDeleteComplete: () -> Unit) {
        viewModelScope.launch {
            try {
                taskRepository.getAllTasks().firstOrNull()?.forEach { task ->
                    taskRepository.deleteTask(task)
                }
                eventRepository.getAllEvents().firstOrNull()?.forEach { event ->
                    eventRepository.deleteEvent(event)
                }

                subjectRepository.getAllSubjects().firstOrNull()?.forEach { subject ->
                    subjectRepository.deleteSubject(subject)
                }

                database.clearAllTables()

                preferencesDataStore.clearPreferences()

                setLoginStatusUseCase.prepare(false)
                    .onEach { 
                        onDeleteComplete()
                    }
                    .catch { }
                    .launchIn(viewModelScope)
            } catch (e: Exception) {
                onDeleteComplete()
            }
        }
    }
} 