package wang.yaojia.webterm.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import wang.yaojia.webterm.designsystem.Spacing
import wang.yaojia.webterm.designsystem.WebTermTheme
import wang.yaojia.webterm.session.GateState
import wang.yaojia.webterm.viewmodels.GateDecision
import wang.yaojia.webterm.wire.GateKind

/**
 * # PlanGateSheet (A22) — the plan-gate three-way approval bottom sheet.
 *
 * The Android analogue of the iOS ExitPlanMode gate (public/tabs.ts:334-350): a Material 3
 * [ModalBottomSheet] with three actions —
 *  - **批准** → approve + review (`Approve(mode="default")`, the default action),
 *  - **批准并自动接受** → approve + auto-accept edits (`Approve(mode="acceptEdits")`),
 *  - **继续计划** → keep planning (wires to `reject`).
 *
 * `mode` is the **TOP-LEVEL** `approve.mode` wire key (plan §1/§4.1) — carried by the resolved
 * [GateState.Affordance.clientMessage] via [GateViewModel.decide], never hand-built here. The
 * server-supplied [detail][GateState.detail] renders as **inert [Text]** (untrusted, §8).
 *
 * ### Epoch capture (stale-guard seam)
 * Each action hands back this gate's [epoch][GateState.epoch] so the tap is bound to the gate on
 * screen; [GateViewModel.decide] drops it if the live gate has moved on. Dismissing the sheet
 * (scrim/back) resolves NOTHING — the gate stays held until an explicit decision.
 *
 * Sheet presentation / detents are device-QA; the wired decisions + epoch capture are the contract.
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
public fun PlanGateSheet(
    gate: GateState,
    onDecide: (GateDecision, Int) -> Unit,
    onDismiss: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
    ModalBottomSheet(onDismissRequest = onDismiss, sheetState = sheetState, modifier = modifier) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = Spacing.lg16, vertical = Spacing.md12),
            verticalArrangement = Arrangement.spacedBy(Spacing.sm8),
        ) {
            Text(text = "批准执行计划", style = MaterialTheme.typography.titleMedium)
            gate.detail?.let { detail ->
                // INERT: untrusted plan text rendered verbatim, no linkify/markdown (§8).
                Text(
                    text = detail,
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    maxLines = 4,
                    overflow = TextOverflow.Ellipsis,
                )
            }
            Button(
                onClick = { onDecide(GateDecision.APPROVE, gate.epoch) },
                modifier = Modifier.fillMaxWidth(),
            ) { Text(text = "批准") }
            OutlinedButton(
                onClick = { onDecide(GateDecision.ACCEPT_EDITS, gate.epoch) },
                modifier = Modifier.fillMaxWidth(),
            ) { Text(text = "批准并自动接受") }
            TextButton(
                onClick = { onDecide(GateDecision.REJECT, gate.epoch) },
                modifier = Modifier.fillMaxWidth(),
            ) { Text(text = "继续计划") }
        }
    }
}

// ── Preview ─────────────────────────────────────────────────────────────────────

@OptIn(ExperimentalMaterial3Api::class)
@Preview(name = "PlanGateSheet")
@Composable
private fun PlanGateSheetPreview() {
    WebTermTheme {
        PlanGateSheet(
            gate = GateState(kind = GateKind.PLAN, detail = "Refactor the auth module into 3 files.", epoch = 5),
            onDecide = { _, _ -> },
            onDismiss = {},
        )
    }
}
