package com.example.elva.ui.calendar

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.elva.data.models.health.CycleInfo
import com.example.elva.data.models.health.Period
import com.example.elva.data.repository.HealthRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import java.time.LocalDate
import java.time.YearMonth
import javax.inject.Inject

@HiltViewModel
class PeriodCalendarViewModel @Inject constructor(
    private val healthRepository: HealthRepository
) : ViewModel() {

    private val _state = MutableStateFlow(PeriodCalendarState())
    val state: StateFlow<PeriodCalendarState> = _state

    init {
        loadData()
    }

    fun loadData() {
        viewModelScope.launch {
            try {
                val periods = healthRepository.getPeriods()
                val cycleInfo = healthRepository.getCycleInfo()
                _state.value = _state.value.copy(
                    periods = periods,
                    cycleInfo = cycleInfo,
                    isLoading = false
                )
            } catch (e: Exception) {
                _state.value = _state.value.copy(
                    error = e.message,
                    isLoading = false
                )
            }
        }
    }

    fun previousMonth() {
        _state.value = _state.value.copy(
            currentMonth = _state.value.currentMonth.minusMonths(1)
        )
    }

    fun nextMonth() {
        _state.value = _state.value.copy(
            currentMonth = _state.value.currentMonth.plusMonths(1)
        )
    }

    fun onDateSelected(date: LocalDate) {
        _state.value = _state.value.copy(selectedDate = date)
    }

    fun clearSelection() {
        _state.value = _state.value.copy(selectedDate = null)
    }

    fun savePeriod(period: Period) {
        viewModelScope.launch {
            try {
                if (period.id == 0L) {
                    healthRepository.addPeriod(period)
                } else {
                    healthRepository.updatePeriod(period)
                }
                loadData()
            } catch (e: Exception) {
                _state.value = _state.value.copy(error = e.message)
            }
        }
    }

    fun deletePeriod(periodId: Long) {
        viewModelScope.launch {
            try {
                healthRepository.deletePeriod(periodId)
                loadData()
            } catch (e: Exception) {
                _state.value = _state.value.copy(error = e.message)
            }
        }
    }
}

data class PeriodCalendarState(
    val currentMonth: YearMonth = YearMonth.now(),
    val periods: List<Period> = emptyList(),
    val cycleInfo: CycleInfo = CycleInfo(
        lastPeriodStart = null,
        nextPeriodPrediction = null,
        ovulationPrediction = null
    ),
    val selectedDate: LocalDate? = null,
    val isLoading: Boolean = true,
    val error: String? = null
)

