package com.tt.honeyDue.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tt.honeyDue.models.TaskColumnsResponse
import com.tt.honeyDue.models.CustomTask
import com.tt.honeyDue.models.TaskCreateRequest
import com.tt.honeyDue.models.TaskCompletionResponse
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 TaskViewModel : ViewModel() {

    private val _tasksState = MutableStateFlow<ApiResult<TaskColumnsResponse>>(ApiResult.Idle)
    val tasksState: StateFlow<ApiResult<TaskColumnsResponse>> = _tasksState

    private val _tasksByResidenceState = MutableStateFlow<ApiResult<TaskColumnsResponse>>(ApiResult.Idle)
    val tasksByResidenceState: StateFlow<ApiResult<TaskColumnsResponse>> = _tasksByResidenceState

    private val _taskAddNewCustomTaskState = MutableStateFlow<ApiResult<CustomTask>>(ApiResult.Idle)
    val taskAddNewCustomTaskState: StateFlow<ApiResult<CustomTask>> = _taskAddNewCustomTaskState

    private val _taskCompletionsState = MutableStateFlow<ApiResult<List<TaskCompletionResponse>>>(ApiResult.Idle)
    val taskCompletionsState: StateFlow<ApiResult<List<TaskCompletionResponse>>> = _taskCompletionsState

    fun loadTasks(forceRefresh: Boolean = false) {
        println("TaskViewModel: loadTasks called")
        viewModelScope.launch {
            _tasksState.value = ApiResult.Loading
            _tasksState.value = APILayer.getTasks(forceRefresh = forceRefresh)
            println("TaskViewModel: loadTasks result: ${_tasksState.value}")
        }
    }

    fun loadTasksByResidence(residenceId: Int, forceRefresh: Boolean = false) {
        viewModelScope.launch {
            _tasksByResidenceState.value = ApiResult.Loading
            _tasksByResidenceState.value = APILayer.getTasksByResidence(
                residenceId = residenceId,
                forceRefresh = forceRefresh
            )
        }
    }

    fun createNewTask(request: TaskCreateRequest) {
        println("TaskViewModel: createNewTask called with $request")
        viewModelScope.launch {
            println("TaskViewModel: Setting state to Loading")
            _taskAddNewCustomTaskState.value = ApiResult.Loading
            val result = APILayer.createTask(request)
            println("TaskViewModel: API result: $result")
            _taskAddNewCustomTaskState.value = result
        }
    }


    fun resetAddTaskState() {
        _taskAddNewCustomTaskState.value = ApiResult.Idle
    }

    fun updateTask(taskId: Int, request: TaskCreateRequest, onComplete: (Boolean) -> Unit) {
        viewModelScope.launch {
            when (val result = APILayer.updateTask(taskId, request)) {
                is ApiResult.Success -> {
                    onComplete(true)
                }
                is ApiResult.Error -> {
                    onComplete(false)
                }
                else -> {
                    onComplete(false)
                }
            }
        }
    }

    fun cancelTask(taskId: Int, onComplete: (Boolean) -> Unit) {
        viewModelScope.launch {
            when (val result = APILayer.cancelTask(taskId)) {
                is ApiResult.Success -> {
                    onComplete(true)
                }
                is ApiResult.Error -> {
                    onComplete(false)
                }
                else -> {
                    onComplete(false)
                }
            }
        }
    }

    fun uncancelTask(taskId: Int, onComplete: (Boolean) -> Unit) {
        viewModelScope.launch {
            when (val result = APILayer.uncancelTask(taskId)) {
                is ApiResult.Success -> {
                    onComplete(true)
                }
                is ApiResult.Error -> {
                    onComplete(false)
                }
                else -> {
                    onComplete(false)
                }
            }
        }
    }

    fun markInProgress(taskId: Int, onComplete: (Boolean) -> Unit) {
        viewModelScope.launch {
            when (val result = APILayer.markInProgress(taskId)) {
                is ApiResult.Success -> {
                    onComplete(true)
                }
                is ApiResult.Error -> {
                    onComplete(false)
                }
                else -> {
                    onComplete(false)
                }
            }
        }
    }

    fun archiveTask(taskId: Int, onComplete: (Boolean) -> Unit) {
        viewModelScope.launch {
            when (val result = APILayer.archiveTask(taskId)) {
                is ApiResult.Success -> {
                    onComplete(true)
                }
                is ApiResult.Error -> {
                    onComplete(false)
                }
                else -> {
                    onComplete(false)
                }
            }
        }
    }

    fun unarchiveTask(taskId: Int, onComplete: (Boolean) -> Unit) {
        viewModelScope.launch {
            when (val result = APILayer.unarchiveTask(taskId)) {
                is ApiResult.Success -> {
                    onComplete(true)
                }
                is ApiResult.Error -> {
                    onComplete(false)
                }
                else -> {
                    onComplete(false)
                }
            }
        }
    }

    /**
     * Load completions for a specific task
     */
    fun loadTaskCompletions(taskId: Int) {
        viewModelScope.launch {
            _taskCompletionsState.value = ApiResult.Loading
            _taskCompletionsState.value = APILayer.getTaskCompletions(taskId)
        }
    }

    /**
     * Reset task completions state
     */
    fun resetTaskCompletionsState() {
        _taskCompletionsState.value = ApiResult.Idle
    }
}
