package com.hermes.app.feature.session

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.hermes.app.R
import com.hermes.app.core.designsystem.HermesCard
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import com.hermes.app.core.designsystem.HermesColors
import com.hermes.app.core.designsystem.HermesPrimaryButton
import com.hermes.app.core.designsystem.HermesSectionHeader
import com.hermes.app.core.localization.HermesLocalizationStore

@Composable
fun SessionView(
    uiState: SessionUiState,
    localizationStore: HermesLocalizationStore,
    onRetry: () -> Unit,
    modifier: Modifier = Modifier,
) {
    HermesCard(modifier = modifier) {
        Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
            HermesSectionHeader(
                title = localizationStore.string(uiState.localeCode, R.string.session_title),
                subtitle = localizationStore.string(uiState.localeCode, R.string.session_subtitle),
            )

            when {
                uiState.isLoading && !uiState.hasSession -> SessionLoadingState(
                    title = uiState.statusText,
                    subtitle = localizationStore.string(uiState.localeCode, R.string.session_note),
                )

                !uiState.hasSession && uiState.bannerMessage != null -> SessionErrorState(
                    message = uiState.bannerMessage,
                    retryText = localizationStore.string(uiState.localeCode, R.string.common_retry),
                    onRetry = onRetry,
                )

                else -> {
                    SessionStatusBadge(
                        text = uiState.statusText,
                        warning = uiState.bannerMessage != null,
                    )

                    if (uiState.bannerMessage != null) {
                        SessionBanner(
                            message = uiState.bannerMessage,
                        )
                    }

                    SessionRow(
                        label = localizationStore.string(uiState.localeCode, R.string.session_id_label),
                        value = uiState.sessionId ?: "--",
                    )
                    SessionRow(
                        label = localizationStore.string(uiState.localeCode, R.string.session_user_id_label),
                        value = uiState.userId ?: "--",
                    )
                    SessionRow(
                        label = localizationStore.string(uiState.localeCode, R.string.session_locale_label),
                        value = localizationStore.localeName(uiState.localeCode),
                    )
                    SessionRow(
                        label = localizationStore.string(uiState.localeCode, R.string.session_started_label),
                        value = uiState.startedAt ?: "--",
                    )
                    SessionRow(
                        label = localizationStore.string(uiState.localeCode, R.string.session_variant_label),
                        value = uiState.experimentVariant ?: "--",
                    )

                    Text(
                        text = localizationStore.string(uiState.localeCode, R.string.session_note),
                        style = MaterialTheme.typography.bodyMedium,
                        color = HermesColors.textSecondary,
                    )
                }
            }
        }
    }
}

@Composable
private fun SessionLoadingState(
    title: String,
    subtitle: String,
) {
    Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
        Text(text = title, style = MaterialTheme.typography.bodyLarge, color = HermesColors.textPrimary)
        Text(text = subtitle, style = MaterialTheme.typography.bodyMedium, color = HermesColors.textSecondary)
    }
}

@Composable
private fun SessionErrorState(
    message: String,
    retryText: String,
    onRetry: () -> Unit,
) {
    Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
        Text(text = message, style = MaterialTheme.typography.bodyMedium, color = HermesColors.warning)
        HermesPrimaryButton(text = retryText, onClick = onRetry)
    }
}

@Composable
private fun SessionStatusBadge(
    text: String,
    warning: Boolean,
) {
    Text(
        text = text,
        style = MaterialTheme.typography.labelMedium,
        color = if (warning) HermesColors.warning else HermesColors.accent,
    )
}

@Composable
private fun SessionBanner(
    message: String,
) {
    Text(
        text = message,
        style = MaterialTheme.typography.bodySmall,
        color = HermesColors.warning,
    )
}

@Composable
private fun SessionRow(
    label: String,
    value: String,
) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
    ) {
        Text(text = label, style = MaterialTheme.typography.bodyMedium, color = HermesColors.textSecondary)
        Text(text = value, style = MaterialTheme.typography.bodyMedium, color = HermesColors.textPrimary)
    }
}
