package com.tt.honeyDue.repository

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.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

/**
 * Singleton repository for managing lookup data across the entire app.
 *
 * @deprecated Use DataManager directly. This class is kept for backwards compatibility
 * and simply delegates to DataManager.
 */
object LookupsRepository {
    private val scope = CoroutineScope(Dispatchers.Default)

    // Delegate to DataManager
    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
    val isInitialized: StateFlow<Boolean> = DataManager.lookupsInitialized

    /**
     * Load all lookups from the API via DataManager.
     * This should be called once when the user logs in.
     */
    fun initialize() {
        // DataManager handles initialization via APILayer.initializeLookups()
        if (DataManager.lookupsInitialized.value) {
            return
        }

        scope.launch {
            APILayer.initializeLookups()
        }
    }

    /**
     * Clear all cached data via DataManager.
     * This should be called when the user logs out.
     */
    fun clear() {
        // DataManager.clear() is called by APILayer.logout()
        // This method is kept for backwards compatibility
    }

    /**
     * Force refresh all lookups from the API.
     */
    fun refresh() {
        scope.launch {
            APILayer.initializeLookups()
        }
    }
}
