package com.anypreta.bountyboard.ui.consultant

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.RateReview
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.anypreta.bountyboard.data.AppContainer
import com.anypreta.bountyboard.data.model.Task
import com.anypreta.bountyboard.di.LocalAppContainer
import com.anypreta.bountyboard.di.containerVMFactory
import com.anypreta.bountyboard.ui.components.EmptyState
import com.anypreta.bountyboard.ui.components.ScreenState
import com.anypreta.bountyboard.ui.components.TaskCard
import com.anypreta.bountyboard.ui.theme.BB
import kotlinx.coroutines.launch

@Composable
fun ReviewsScreen(onOpenTask: (String) -> Unit) {
    val container = LocalAppContainer.current
    val vm: ReviewsViewModel =
        viewModel(factory = containerVMFactory(container) { ReviewsViewModel(it) })
    val p = BB.colors

    Column(Modifier.fillMaxWidth()) {
        Text(
            "Tasks awaiting your review",
            style = MaterialTheme.typography.bodySmall,
            color = p.muted,
            modifier = Modifier.padding(horizontal = 16.dp, vertical = 12.dp),
        )

        ScreenState(loading = vm.loading, error = vm.error, onRetry = { vm.load() }) {
            if (vm.tasks.isEmpty()) {
                EmptyState(
                    title = "Nothing to review",
                    subtitle = "Tasks submitted by developers for review will appear here.",
                    icon = Icons.Outlined.RateReview,
                )
            } else {
                LazyColumn(
                    contentPadding = PaddingValues(16.dp),
                    verticalArrangement = Arrangement.spacedBy(14.dp),
                ) {
                    items(vm.tasks, key = { it.id }) { task ->
                        TaskCard(
                            task = task,
                            onClick = { onOpenTask(task.id) },
                            showStatus = true,
                        )
                    }
                }
            }
        }
    }
}

class ReviewsViewModel(private val container: AppContainer) : ViewModel() {

    var loading by mutableStateOf(true)
        private set
    var error by mutableStateOf<String?>(null)
        private set
    var tasks by mutableStateOf<List<Task>>(emptyList())
        private set

    init {
        load()
    }

    fun load() {
        loading = true; error = null
        viewModelScope.launch {
            try {
                tasks = container.repo.call { reviews() }.tasks
            } catch (e: Exception) {
                error = e.message
            } finally {
                loading = false
            }
        }
    }
}
