package com.tt.honeyDue.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tt.honeyDue.models.NotificationPreference
import com.tt.honeyDue.models.UpdateNotificationPreferencesRequest
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 NotificationPreferencesViewModel : ViewModel() {

    private val _preferencesState = MutableStateFlow<ApiResult<NotificationPreference>>(ApiResult.Idle)
    val preferencesState: StateFlow<ApiResult<NotificationPreference>> = _preferencesState

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

    fun loadPreferences() {
        viewModelScope.launch {
            _preferencesState.value = ApiResult.Loading
            val result = APILayer.getNotificationPreferences()
            _preferencesState.value = when (result) {
                is ApiResult.Success -> ApiResult.Success(result.data)
                is ApiResult.Error -> result
                else -> ApiResult.Error("Unknown error")
            }
        }
    }

    fun updatePreference(
        taskDueSoon: Boolean? = null,
        taskOverdue: Boolean? = null,
        taskCompleted: Boolean? = null,
        taskAssigned: Boolean? = null,
        residenceShared: Boolean? = null,
        warrantyExpiring: Boolean? = null,
        dailyDigest: Boolean? = null,
        emailTaskCompleted: Boolean? = null,
        taskDueSoonHour: Int? = null,
        taskOverdueHour: Int? = null,
        warrantyExpiringHour: Int? = null,
        dailyDigestHour: Int? = null
    ) {
        viewModelScope.launch {
            _updateState.value = ApiResult.Loading
            val request = UpdateNotificationPreferencesRequest(
                taskDueSoon = taskDueSoon,
                taskOverdue = taskOverdue,
                taskCompleted = taskCompleted,
                taskAssigned = taskAssigned,
                residenceShared = residenceShared,
                warrantyExpiring = warrantyExpiring,
                dailyDigest = dailyDigest,
                emailTaskCompleted = emailTaskCompleted,
                taskDueSoonHour = taskDueSoonHour,
                taskOverdueHour = taskOverdueHour,
                warrantyExpiringHour = warrantyExpiringHour,
                dailyDigestHour = dailyDigestHour
            )
            val result = APILayer.updateNotificationPreferences(request)
            _updateState.value = when (result) {
                is ApiResult.Success -> {
                    // Update the preferences state with the new values
                    _preferencesState.value = ApiResult.Success(result.data)
                    ApiResult.Success(result.data)
                }
                is ApiResult.Error -> result
                else -> ApiResult.Error("Unknown error")
            }
        }
    }

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