package com.tt.honeyDue.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tt.honeyDue.data.DataManager
import com.tt.honeyDue.models.*
import com.tt.honeyDue.network.ApiResult
import com.tt.honeyDue.network.APILayer
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

/**
 * ViewModel for lookup data.
 * Now uses DataManager as the single source of truth for all lookups.
 * Lookups are loaded once via APILayer.initializeLookups() after login.
 */
class LookupsViewModel : ViewModel() {

    // Expose DataManager's lookup StateFlows directly
    val residenceTypes: StateFlow<List<ResidenceType>> = DataManager.residenceTypes
    val taskFrequencies: StateFlow<List<TaskFrequency>> = DataManager.taskFrequencies
    val taskPriorities: StateFlow<List<TaskPriority>> = DataManager.taskPriorities
    val taskCategories: StateFlow<List<TaskCategory>> = DataManager.taskCategories
    val contractorSpecialties: StateFlow<List<ContractorSpecialty>> = DataManager.contractorSpecialties

    // Keep legacy state flows for compatibility during migration
    private val _residenceTypesState = MutableStateFlow<ApiResult<List<ResidenceType>>>(ApiResult.Idle)
    val residenceTypesState: StateFlow<ApiResult<List<ResidenceType>>> = _residenceTypesState

    private val _taskFrequenciesState = MutableStateFlow<ApiResult<List<TaskFrequency>>>(ApiResult.Idle)
    val taskFrequenciesState: StateFlow<ApiResult<List<TaskFrequency>>> = _taskFrequenciesState

    private val _taskPrioritiesState = MutableStateFlow<ApiResult<List<TaskPriority>>>(ApiResult.Idle)
    val taskPrioritiesState: StateFlow<ApiResult<List<TaskPriority>>> = _taskPrioritiesState

    private val _taskCategoriesState = MutableStateFlow<ApiResult<List<TaskCategory>>>(ApiResult.Idle)
    val taskCategoriesState: StateFlow<ApiResult<List<TaskCategory>>> = _taskCategoriesState

    fun loadResidenceTypes() {
        viewModelScope.launch {
            val cached = DataManager.residenceTypes.value
            if (cached.isNotEmpty()) {
                _residenceTypesState.value = ApiResult.Success(cached)
                return@launch
            }
            _residenceTypesState.value = ApiResult.Loading
            val result = APILayer.getResidenceTypes()
            _residenceTypesState.value = result
        }
    }

    fun loadTaskFrequencies() {
        viewModelScope.launch {
            val cached = DataManager.taskFrequencies.value
            if (cached.isNotEmpty()) {
                _taskFrequenciesState.value = ApiResult.Success(cached)
                return@launch
            }
            _taskFrequenciesState.value = ApiResult.Loading
            val result = APILayer.getTaskFrequencies()
            _taskFrequenciesState.value = result
        }
    }

    fun loadTaskPriorities() {
        viewModelScope.launch {
            val cached = DataManager.taskPriorities.value
            if (cached.isNotEmpty()) {
                _taskPrioritiesState.value = ApiResult.Success(cached)
                return@launch
            }
            _taskPrioritiesState.value = ApiResult.Loading
            val result = APILayer.getTaskPriorities()
            _taskPrioritiesState.value = result
        }
    }

    fun loadTaskCategories() {
        viewModelScope.launch {
            val cached = DataManager.taskCategories.value
            if (cached.isNotEmpty()) {
                _taskCategoriesState.value = ApiResult.Success(cached)
                return@launch
            }
            _taskCategoriesState.value = ApiResult.Loading
            val result = APILayer.getTaskCategories()
            _taskCategoriesState.value = result
        }
    }

    // Load all lookups at once
    fun loadAllLookups() {
        loadResidenceTypes()
        loadTaskFrequencies()
        loadTaskPriorities()
        loadTaskCategories()
    }
}
