package com.example.ops_android.ui.home

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.ops_android.data.model.OrderCounts
import com.example.ops_android.data.model.ScheduleEvent
import com.example.ops_android.data.model.WorkOrderCounts
import com.example.ops_android.data.repository.OrderRepository
import com.example.ops_android.data.repository.ScheduleRepository
import com.example.ops_android.data.repository.WorkOrderRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.*

data class HomeUiState(
    val todayEvents: List<ScheduleEvent> = emptyList(),
    val orderCounts: OrderCounts? = null,
    val workOrderCounts: WorkOrderCounts? = null,
    val isLoading: Boolean = false,
    val isRefreshing: Boolean = false,
    val errorMessage: String? = null
)

class HomeViewModel(
    private val orderRepository: OrderRepository,
    private val workOrderRepository: WorkOrderRepository,
    private val scheduleRepository: ScheduleRepository
) : ViewModel() {

    private val _uiState = MutableStateFlow(HomeUiState())
    val uiState: StateFlow<HomeUiState> = _uiState.asStateFlow()

    init {
        loadData()
    }

    fun loadData() {
        viewModelScope.launch {
            _uiState.value = _uiState.value.copy(isLoading = true, errorMessage = null)
            try {
                val today = SimpleDateFormat("yyyy-MM-dd", Locale.US).format(Date())
                val eventsResult = scheduleRepository.getEvents(today, today)
                val orderCounts = orderRepository.getOrderCount()
                val workOrderCounts = workOrderRepository.getCount()

                _uiState.value = _uiState.value.copy(
                    todayEvents = eventsResult.getOrNull() ?: emptyList(),
                    orderCounts = orderCounts.getOrNull(),
                    workOrderCounts = workOrderCounts.getOrNull(),
                    isLoading = false
                )
            } catch (e: Exception) {
                _uiState.value = _uiState.value.copy(
                    isLoading = false,
                    errorMessage = e.message
                )
            }
        }
    }

    fun refresh() {
        viewModelScope.launch {
            _uiState.value = _uiState.value.copy(isRefreshing = true)
            val today = SimpleDateFormat("yyyy-MM-dd", Locale.US).format(Date())
            val eventsResult = scheduleRepository.getEvents(today, today)
            val orderCounts = orderRepository.getOrderCount()
            val workOrderCounts = workOrderRepository.getCount()

            _uiState.value = _uiState.value.copy(
                todayEvents = eventsResult.getOrNull() ?: emptyList(),
                orderCounts = orderCounts.getOrNull(),
                workOrderCounts = workOrderCounts.getOrNull(),
                isRefreshing = false
            )
        }
    }

    class Factory(
        private val orderRepository: OrderRepository,
        private val workOrderRepository: WorkOrderRepository,
        private val scheduleRepository: ScheduleRepository
    ) : ViewModelProvider.Factory {
        @Suppress("UNCHECKED_CAST")
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return HomeViewModel(orderRepository, workOrderRepository, scheduleRepository) as T
        }
    }
}
