package com.example.ops_android.ui.order

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.ops_android.data.model.Cost
import com.example.ops_android.data.model.PurchaseOrder
import com.example.ops_android.data.model.StatusLog
import com.example.ops_android.data.repository.OrderRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

data class OrderDetailUiState(
    val order: PurchaseOrder? = null,
    val isLoading: Boolean = false,
    val errorMessage: String? = null,
    val showStatusDialog: Boolean = false,
    val selectedStatus: String = "",
    val statusComment: String = "",
    val isSubmitting: Boolean = false
)

class OrderDetailViewModel(
    private val orderRepository: OrderRepository,
    private val orderId: Int
) : ViewModel() {

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

    init {
        loadOrder()
    }

    fun loadOrder() {
        viewModelScope.launch {
            _uiState.value = _uiState.value.copy(isLoading = true)
            val result = orderRepository.getOrder(orderId)
            result.fold(
                onSuccess = { order ->
                    _uiState.value = _uiState.value.copy(order = order, isLoading = false)
                },
                onFailure = { e ->
                    _uiState.value = _uiState.value.copy(isLoading = false, errorMessage = e.message)
                }
            )
        }
    }

    fun showStatusDialog(status: String) {
        _uiState.value = _uiState.value.copy(
            showStatusDialog = true,
            selectedStatus = status,
            statusComment = ""
        )
    }

    fun dismissStatusDialog() {
        _uiState.value = _uiState.value.copy(showStatusDialog = false)
    }

    fun onStatusCommentChange(comment: String) {
        _uiState.value = _uiState.value.copy(statusComment = comment)
    }

    fun submitStatusChange() {
        viewModelScope.launch {
            val state = _uiState.value
            _uiState.value = state.copy(isSubmitting = true)
            val result = orderRepository.updateOrderStatus(
                id = orderId,
                status = state.selectedStatus,
                comment = state.statusComment.ifBlank { null }
            )
            result.fold(
                onSuccess = {
                    _uiState.value = _uiState.value.copy(showStatusDialog = false, isSubmitting = false)
                    loadOrder()
                },
                onFailure = { e ->
                    _uiState.value = _uiState.value.copy(isSubmitting = false, errorMessage = e.message)
                }
            )
        }
    }

    class Factory(
        private val orderRepository: OrderRepository,
        private val orderId: Int
    ) : ViewModelProvider.Factory {
        @Suppress("UNCHECKED_CAST")
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return OrderDetailViewModel(orderRepository, orderId) as T
        }
    }
}
