package com.carmaintenance.ui.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.carmaintenance.data.model.Appointment
import com.carmaintenance.data.model.ServiceOrder
import com.carmaintenance.data.model.Vehicle
import com.carmaintenance.data.repository.AppointmentRepository
import com.carmaintenance.data.repository.OrderRepository
import com.carmaintenance.data.repository.VehicleRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CustomerViewModel @Inject constructor(
    private val vehicleRepository: VehicleRepository,
    private val orderRepository: OrderRepository,
    private val appointmentRepository: AppointmentRepository
) : ViewModel() {

    // 车辆相关
    private val _vehicles = MutableStateFlow<List<Vehicle>>(emptyList())
    val vehicles: StateFlow<List<Vehicle>> = _vehicles.asStateFlow()

    private val _isLoadingVehicles = MutableStateFlow(false)
    val isLoadingVehicles: StateFlow<Boolean> = _isLoadingVehicles.asStateFlow()

    // 工单相关
    private val _orders = MutableStateFlow<List<ServiceOrder>>(emptyList())
    val orders: StateFlow<List<ServiceOrder>> = _orders.asStateFlow()

    private val _isLoadingOrders = MutableStateFlow(false)
    val isLoadingOrders: StateFlow<Boolean> = _isLoadingOrders.asStateFlow()

    // 预约相关
    private val _appointments = MutableStateFlow<List<Appointment>>(emptyList())
    val appointments: StateFlow<List<Appointment>> = _appointments.asStateFlow()

    private val _isLoadingAppointments = MutableStateFlow(false)
    val isLoadingAppointments: StateFlow<Boolean> = _isLoadingAppointments.asStateFlow()

    private val _message = MutableStateFlow<String?>(null)
    val message: StateFlow<String?> = _message.asStateFlow()

    fun loadCustomerData(customerId: Int) {
        loadVehicles(customerId)
        loadOrders(customerId)
        loadAppointments(customerId)
    }

    fun loadVehicles(customerId: Int) {
        viewModelScope.launch {
            _isLoadingVehicles.value = true
            vehicleRepository.getCustomerVehicles(customerId)
                .onSuccess { _vehicles.value = it }
                .onFailure { _message.value = it.message }
            _isLoadingVehicles.value = false
        }
    }

    fun loadOrders(customerId: Int) {
        viewModelScope.launch {
            _isLoadingOrders.value = true
            orderRepository.getCustomerOrders(customerId)
                .onSuccess { _orders.value = it }
                .onFailure { _message.value = it.message }
            _isLoadingOrders.value = false
        }
    }

    fun loadAppointments(customerId: Int) {
        viewModelScope.launch {
            _isLoadingAppointments.value = true
            appointmentRepository.getCustomerAppointments(customerId)
                .onSuccess { _appointments.value = it }
                .onFailure { _message.value = it.message }
            _isLoadingAppointments.value = false
        }
    }

    fun createAppointment(appointment: Appointment) {
        viewModelScope.launch {
            appointmentRepository.createAppointment(appointment)
                .onSuccess {
                    _message.value = "预约创建成功"
                    if (appointment.customerId > 0) {
                        loadAppointments(appointment.customerId)
                    }
                }
                .onFailure { _message.value = it.message ?: "预约创建失败" }
        }
    }

    fun cancelAppointment(appointmentId: Int, customerId: Int) {
        viewModelScope.launch {
            appointmentRepository.cancelAppointment(appointmentId)
                .onSuccess {
                    _message.value = "预约已取消"
                    loadAppointments(customerId)
                }
                .onFailure { _message.value = it.message ?: "取消预约失败" }
        }
    }

    fun clearMessage() {
        _message.value = null
    }
}
