package com.anypreta.bountyboard.ui.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ErrorOutline
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.anypreta.bountyboard.ui.theme.BB

/**
 * Standard state wrapper: shows a centered loader, an error with retry, or content.
 * Pass [empty] = true to show an empty placeholder via [emptyContent].
 */
@Composable
fun ScreenState(
    loading: Boolean,
    error: String?,
    modifier: Modifier = Modifier,
    onRetry: (() -> Unit)? = null,
    content: @Composable () -> Unit,
) {
    Box(modifier.fillMaxSize()) {
        when {
            loading -> LoadingBox()
            error != null -> Column(
                Modifier.fillMaxSize().padding(24.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
            ) {
                EmptyState(
                    title = "Something went wrong",
                    subtitle = error,
                    icon = Icons.Outlined.ErrorOutline,
                    action = if (onRetry != null) {
                        { BBButton("Retry", onClick = onRetry, style = BBButtonStyle.Primary, small = true) }
                    } else null,
                )
            }
            else -> content()
        }
    }
}

/** Scrollable padded column used by most form/detail screens. */
@Composable
fun ScreenColumn(
    modifier: Modifier = Modifier,
    padding: PaddingValues = PaddingValues(16.dp),
    spacing: Dp = 14.dp,
    scroll: Boolean = true,
    content: @Composable androidx.compose.foundation.layout.ColumnScope.() -> Unit,
) {
    val base = modifier.fillMaxSize()
    Column(
        modifier = if (scroll) base.verticalScroll(rememberScrollState()).padding(padding) else base.padding(padding),
        verticalArrangement = Arrangement.spacedBy(spacing),
        content = content,
    )
}
