package com.tt.honeyDue.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tt.honeyDue.models.*
import com.tt.honeyDue.network.ApiResult
import com.tt.honeyDue.network.APILayer
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class ContractorViewModel : ViewModel() {

    private val _contractorsState = MutableStateFlow<ApiResult<List<ContractorSummary>>>(ApiResult.Idle)
    val contractorsState: StateFlow<ApiResult<List<ContractorSummary>>> = _contractorsState

    private val _contractorDetailState = MutableStateFlow<ApiResult<Contractor>>(ApiResult.Idle)
    val contractorDetailState: StateFlow<ApiResult<Contractor>> = _contractorDetailState

    private val _createState = MutableStateFlow<ApiResult<Contractor>>(ApiResult.Idle)
    val createState: StateFlow<ApiResult<Contractor>> = _createState

    private val _updateState = MutableStateFlow<ApiResult<Contractor>>(ApiResult.Idle)
    val updateState: StateFlow<ApiResult<Contractor>> = _updateState

    private val _deleteState = MutableStateFlow<ApiResult<Unit>>(ApiResult.Idle)
    val deleteState: StateFlow<ApiResult<Unit>> = _deleteState

    private val _toggleFavoriteState = MutableStateFlow<ApiResult<Contractor>>(ApiResult.Idle)
    val toggleFavoriteState: StateFlow<ApiResult<Contractor>> = _toggleFavoriteState

    fun loadContractors(
        specialty: String? = null,
        isFavorite: Boolean? = null,
        isActive: Boolean? = null,
        search: String? = null,
        forceRefresh: Boolean = false
    ) {
        viewModelScope.launch {
            _contractorsState.value = ApiResult.Loading
            _contractorsState.value = APILayer.getContractors(
                specialty = specialty,
                isFavorite = isFavorite,
                isActive = isActive,
                search = search,
                forceRefresh = forceRefresh
            )
        }
    }

    fun loadContractorDetail(id: Int) {
        viewModelScope.launch {
            _contractorDetailState.value = ApiResult.Loading
            _contractorDetailState.value = APILayer.getContractor(id)
        }
    }

    fun createContractor(request: ContractorCreateRequest) {
        viewModelScope.launch {
            _createState.value = ApiResult.Loading
            _createState.value = APILayer.createContractor(request)
        }
    }

    fun updateContractor(id: Int, request: ContractorUpdateRequest) {
        viewModelScope.launch {
            _updateState.value = ApiResult.Loading
            _updateState.value = APILayer.updateContractor(id, request)
        }
    }

    fun deleteContractor(id: Int) {
        viewModelScope.launch {
            _deleteState.value = ApiResult.Loading
            _deleteState.value = APILayer.deleteContractor(id)
        }
    }

    fun toggleFavorite(id: Int) {
        viewModelScope.launch {
            _toggleFavoriteState.value = ApiResult.Loading
            _toggleFavoriteState.value = APILayer.toggleFavorite(id)
        }
    }

    fun resetCreateState() {
        _createState.value = ApiResult.Idle
    }

    fun resetUpdateState() {
        _updateState.value = ApiResult.Idle
    }

    fun resetDeleteState() {
        _deleteState.value = ApiResult.Idle
    }

    fun resetToggleFavoriteState() {
        _toggleFavoriteState.value = ApiResult.Idle
    }
}
