package com.tt.honeyDue.data

import com.tt.honeyDue.models.ContractorSpecialty
import com.tt.honeyDue.models.ContractorSummary
import com.tt.honeyDue.models.Document
import com.tt.honeyDue.models.FeatureBenefit
import com.tt.honeyDue.models.MyResidencesResponse
import com.tt.honeyDue.models.Promotion
import com.tt.honeyDue.models.Residence
import com.tt.honeyDue.models.ResidenceSummaryResponse
import com.tt.honeyDue.models.ResidenceType
import com.tt.honeyDue.models.SubscriptionStatus
import com.tt.honeyDue.models.TaskCategory
import com.tt.honeyDue.models.TaskColumnsResponse
import com.tt.honeyDue.models.TaskFrequency
import com.tt.honeyDue.models.TaskPriority
import com.tt.honeyDue.models.TaskTemplate
import com.tt.honeyDue.models.TaskTemplatesGroupedResponse
import com.tt.honeyDue.models.TotalSummary
import com.tt.honeyDue.models.UpgradeTriggerData
import com.tt.honeyDue.models.User
import kotlinx.coroutines.flow.StateFlow

/**
 * Contract covering the [DataManager] surface consumed by Compose screens
 * and the parity-gallery fixture factories.
 *
 * This interface exists so screens can depend on an abstraction that tests,
 * previews, and the parity-gallery can substitute via [LocalDataManager].
 * The member set intentionally mirrors every StateFlow and lookup helper a
 * screen may read — [com.tt.honeyDue.testing.FixtureDataManager] produces
 * fully-populated fakes so snapshot renders have the data every surface
 * expects without reaching into the production singleton.
 *
 * ViewModels, [com.tt.honeyDue.network.APILayer], and [PersistenceManager]
 * continue to use the concrete [DataManager] singleton directly; they are
 * not covered by this interface.
 */
interface IDataManager {

    // ==================== AUTH ====================

    /** Observed by [com.tt.honeyDue.ui.screens.ProfileScreen], [com.tt.honeyDue.ui.screens.ResidenceDetailScreen], [com.tt.honeyDue.ui.screens.ResidenceFormScreen]. */
    val currentUser: StateFlow<User?>

    // ==================== RESIDENCES ====================

    /** Observed by [com.tt.honeyDue.ui.screens.ContractorDetailScreen] and the onboarding first-task screen. */
    val residences: StateFlow<List<Residence>>

    /** Full my-residences API response (may include metadata beyond the list itself). */
    val myResidences: StateFlow<MyResidencesResponse?>

    /** Observed by [com.tt.honeyDue.ui.screens.HomeScreen] and [com.tt.honeyDue.ui.screens.ResidencesScreen]. */
    val totalSummary: StateFlow<TotalSummary?>

    /** Per-residence summary cache (keyed by residence id). */
    val residenceSummaries: StateFlow<Map<Int, ResidenceSummaryResponse>>

    // ==================== TASKS ====================

    /** Kanban board covering all tasks across all residences. */
    val allTasks: StateFlow<TaskColumnsResponse?>

    /** Kanban board cache keyed by residence id. */
    val tasksByResidence: StateFlow<Map<Int, TaskColumnsResponse>>

    /** Task completions keyed by task id. Populated by APILayer.getTaskCompletions. */
    val taskCompletions: StateFlow<Map<Int, List<com.tt.honeyDue.models.TaskCompletionResponse>>>

    // ==================== DOCUMENTS ====================

    val documents: StateFlow<List<Document>>

    val documentsByResidence: StateFlow<Map<Int, List<Document>>>

    /** Document detail (full Document with user+images) cached by id. Populated by APILayer.getDocument. */
    val documentDetail: StateFlow<Map<Int, Document>>

    // ==================== CONTRACTORS ====================

    val contractors: StateFlow<List<ContractorSummary>>

    /** Contractor list per residence id (from residence-scoped fetches). */
    val contractorsByResidence: StateFlow<Map<Int, List<ContractorSummary>>>

    /** Contractor detail (full Contractor with user association) cached by id. Populated by APILayer.getContractor. */
    val contractorDetail: StateFlow<Map<Int, com.tt.honeyDue.models.Contractor>>

    // ==================== SUBSCRIPTION ====================

    /** Observed by [com.tt.honeyDue.ui.screens.ProfileScreen]. */
    val subscription: StateFlow<SubscriptionStatus?>

    val upgradeTriggers: StateFlow<Map<String, UpgradeTriggerData>>

    /** Observed by [com.tt.honeyDue.ui.screens.subscription.FeatureComparisonScreen]. */
    val featureBenefits: StateFlow<List<FeatureBenefit>>

    val promotions: StateFlow<List<Promotion>>

    // ==================== LOOKUPS ====================

    val residenceTypes: StateFlow<List<ResidenceType>>
    val taskFrequencies: StateFlow<List<TaskFrequency>>
    val taskPriorities: StateFlow<List<TaskPriority>>
    val taskCategories: StateFlow<List<TaskCategory>>
    val contractorSpecialties: StateFlow<List<ContractorSpecialty>>

    // ==================== TASK TEMPLATES ====================

    val taskTemplates: StateFlow<List<TaskTemplate>>
    val taskTemplatesGrouped: StateFlow<TaskTemplatesGroupedResponse?>

    // ==================== NOTIFICATION PREFERENCES ====================

    /** User's server-backed notification preferences. Populated by APILayer.getNotificationPreferences. */
    val notificationPreferences: StateFlow<com.tt.honeyDue.models.NotificationPreference?>

    // ==================== O(1) LOOKUP HELPERS ====================

    fun getResidenceType(id: Int?): ResidenceType?
    fun getTaskFrequency(id: Int?): TaskFrequency?
    fun getTaskPriority(id: Int?): TaskPriority?
    fun getTaskCategory(id: Int?): TaskCategory?
    fun getContractorSpecialty(id: Int?): ContractorSpecialty?
}
