package com.tt.honeyDue.testing

import com.tt.honeyDue.data.IDataManager
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.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

/**
 * Test-only [IDataManager] backed by pre-populated [MutableStateFlow]s.
 *
 * Not intended for direct instantiation — use the factories on
 * [FixtureDataManager] (`empty()` / `populated()`) which wire up consistent
 * graphs from [Fixtures]. Because every field is constructor-injected, the
 * class supports arbitrary test scenarios without touching the real
 * [com.tt.honeyDue.data.DataManager] singleton.
 *
 * All lookup helpers resolve from the provided lists. The instance has no
 * side effects: there are no API calls, no disk persistence, no clocks.
 */
class InMemoryDataManager(
    currentUser: User? = null,
    residences: List<Residence> = emptyList(),
    myResidencesResponse: MyResidencesResponse? = null,
    totalSummary: TotalSummary? = null,
    residenceSummaries: Map<Int, ResidenceSummaryResponse> = emptyMap(),
    allTasks: TaskColumnsResponse? = null,
    tasksByResidence: Map<Int, TaskColumnsResponse> = emptyMap(),
    taskCompletions: Map<Int, List<com.tt.honeyDue.models.TaskCompletionResponse>> = emptyMap(),
    documents: List<Document> = emptyList(),
    documentsByResidence: Map<Int, List<Document>> = emptyMap(),
    documentDetail: Map<Int, Document> = emptyMap(),
    contractors: List<ContractorSummary> = emptyList(),
    contractorsByResidence: Map<Int, List<ContractorSummary>> = emptyMap(),
    contractorDetail: Map<Int, com.tt.honeyDue.models.Contractor> = emptyMap(),
    subscription: SubscriptionStatus? = null,
    upgradeTriggers: Map<String, UpgradeTriggerData> = emptyMap(),
    featureBenefits: List<FeatureBenefit> = emptyList(),
    promotions: List<Promotion> = emptyList(),
    residenceTypes: List<ResidenceType> = emptyList(),
    taskFrequencies: List<TaskFrequency> = emptyList(),
    taskPriorities: List<TaskPriority> = emptyList(),
    taskCategories: List<TaskCategory> = emptyList(),
    contractorSpecialties: List<ContractorSpecialty> = emptyList(),
    taskTemplates: List<TaskTemplate> = emptyList(),
    taskTemplatesGrouped: TaskTemplatesGroupedResponse? = null,
    notificationPreferences: com.tt.honeyDue.models.NotificationPreference? = null,
) : IDataManager {

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

    override val currentUser: StateFlow<User?> = MutableStateFlow(currentUser)

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

    override val residences: StateFlow<List<Residence>> = MutableStateFlow(residences)
    override val myResidences: StateFlow<MyResidencesResponse?> = MutableStateFlow(myResidencesResponse)
    override val totalSummary: StateFlow<TotalSummary?> = MutableStateFlow(totalSummary)
    override val residenceSummaries: StateFlow<Map<Int, ResidenceSummaryResponse>> = MutableStateFlow(residenceSummaries)

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

    override val allTasks: StateFlow<TaskColumnsResponse?> = MutableStateFlow(allTasks)
    override val tasksByResidence: StateFlow<Map<Int, TaskColumnsResponse>> = MutableStateFlow(tasksByResidence)
    override val taskCompletions: StateFlow<Map<Int, List<com.tt.honeyDue.models.TaskCompletionResponse>>> = MutableStateFlow(taskCompletions)

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

    override val documents: StateFlow<List<Document>> = MutableStateFlow(documents)
    override val documentsByResidence: StateFlow<Map<Int, List<Document>>> = MutableStateFlow(documentsByResidence)
    override val documentDetail: StateFlow<Map<Int, Document>> = MutableStateFlow(documentDetail)

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

    override val contractors: StateFlow<List<ContractorSummary>> = MutableStateFlow(contractors)
    override val contractorsByResidence: StateFlow<Map<Int, List<ContractorSummary>>> = MutableStateFlow(contractorsByResidence)
    override val contractorDetail: StateFlow<Map<Int, com.tt.honeyDue.models.Contractor>> = MutableStateFlow(contractorDetail)

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

    override val subscription: StateFlow<SubscriptionStatus?> = MutableStateFlow(subscription)
    override val upgradeTriggers: StateFlow<Map<String, UpgradeTriggerData>> = MutableStateFlow(upgradeTriggers)
    override val featureBenefits: StateFlow<List<FeatureBenefit>> = MutableStateFlow(featureBenefits)
    override val promotions: StateFlow<List<Promotion>> = MutableStateFlow(promotions)

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

    override val residenceTypes: StateFlow<List<ResidenceType>> = MutableStateFlow(residenceTypes)
    override val taskFrequencies: StateFlow<List<TaskFrequency>> = MutableStateFlow(taskFrequencies)
    override val taskPriorities: StateFlow<List<TaskPriority>> = MutableStateFlow(taskPriorities)
    override val taskCategories: StateFlow<List<TaskCategory>> = MutableStateFlow(taskCategories)
    override val contractorSpecialties: StateFlow<List<ContractorSpecialty>> = MutableStateFlow(contractorSpecialties)

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

    override val taskTemplates: StateFlow<List<TaskTemplate>> = MutableStateFlow(taskTemplates)
    override val taskTemplatesGrouped: StateFlow<TaskTemplatesGroupedResponse?> = MutableStateFlow(taskTemplatesGrouped)

    override val notificationPreferences: StateFlow<com.tt.honeyDue.models.NotificationPreference?> = MutableStateFlow(notificationPreferences)

    // ==================== LOOKUP HELPERS ====================

    override fun getResidenceType(id: Int?): ResidenceType? =
        id?.let { wanted -> residenceTypes.value.firstOrNull { it.id == wanted } }

    override fun getTaskFrequency(id: Int?): TaskFrequency? =
        id?.let { wanted -> taskFrequencies.value.firstOrNull { it.id == wanted } }

    override fun getTaskPriority(id: Int?): TaskPriority? =
        id?.let { wanted -> taskPriorities.value.firstOrNull { it.id == wanted } }

    override fun getTaskCategory(id: Int?): TaskCategory? =
        id?.let { wanted -> taskCategories.value.firstOrNull { it.id == wanted } }

    override fun getContractorSpecialty(id: Int?): ContractorSpecialty? =
        id?.let { wanted -> contractorSpecialties.value.firstOrNull { it.id == wanted } }
}
