import { Alert, Box, Chip, Divider, Paper, Typography } from '@mui/material'
import { CheckCircle2, ChevronRight, ShieldCheck, XCircle } from 'lucide-react'
import type { GateEvaluation } from '../../domain/signalPipeline'
import { GateStatus, GATE_STATUS_LABELS, GATE_STATUS_COLORS } from '../../domain/signalPipeline'

interface SensitivityGatePanelProps {
  gate: GateEvaluation
}

export function SensitivityGatePanel({ gate }: SensitivityGatePanelProps) {
  const { bg, fg } = GATE_STATUS_COLORS[gate.status]

  const confidentialCheck = gate.checks.find(c => c.label === 'Nicht CONFIDENTIAL' && !c.passed)
  const restrictedCheck = gate.checks.find(c => c.label === 'Nicht RESTRICTED' && !c.passed)

  return (
    <Paper variant="outlined" sx={{ mb: 1.5, borderRadius: 1.5 }}>
      <Box sx={{ p: 1.5, display: 'flex', alignItems: 'center', gap: 1 }}>
        <ShieldCheck size={14} color="#64748b" />
        <Typography variant="subtitle2" sx={{ fontWeight: 600, fontSize: '0.8rem' }}>
          Sensitivitäts-Gate
        </Typography>
        <Box sx={{ ml: 'auto' }}>
          <Chip
            size="small"
            label={GATE_STATUS_LABELS[gate.status]}
            sx={{ bgcolor: bg, color: fg, border: 'none', fontSize: '0.7rem', fontWeight: 600 }}
          />
        </Box>
      </Box>
      <Divider />
      <Box sx={{ p: 1.5 }}>
        {gate.checks.map((check, i) => (
          <Box key={i} sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 0.5 }}>
            {check.passed
              ? <CheckCircle2 size={13} color="#15803d" />
              : <XCircle size={13} color="#dc2626" />
            }
            <Typography variant="caption">
              {check.label}
              {check.value && (
                <Box component="span" sx={{ color: 'text.secondary', ml: 0.5 }}>
                  {check.value}
                </Box>
              )}
            </Typography>
          </Box>
        ))}

        {gate.status === GateStatus.FAILED && (
          <Box sx={{ mt: 1 }}>
            {confidentialCheck && (
              <Alert severity="error" sx={{ py: 0.5, fontSize: '0.75rem', '& .MuiAlert-message': { py: 0.5 } }}>
                Sensitivitätsstufe CONFIDENTIAL: Dieses Signal darf nicht im Demand Feed erscheinen.
                Mieter- oder Vertragsidentitäten sind schützenswert und nur intern zugänglich.
              </Alert>
            )}
            {restrictedCheck && (
              <Alert severity="error" sx={{ py: 0.5, fontSize: '0.75rem', '& .MuiAlert-message': { py: 0.5 } }}>
                Sensitivitätsstufe RESTRICTED: Zugriff auf dieses Signal ist rollenbasiert eingeschränkt.
                Nur autorisierte Nutzer mit entsprechender Berechtigung dürfen es einsehen.
              </Alert>
            )}
          </Box>
        )}

        {gate.reason && (
          <Typography
            variant="caption"
            sx={{ color: 'text.secondary', mt: 1, display: 'block' }}
          >
            {gate.reason}
          </Typography>
        )}
        {gate.nextAction && gate.status !== GateStatus.PASSED && (
          <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.25, mt: 0.5 }}>
            <ChevronRight size={12} color="#1d4ed8" />
            <Typography variant="caption" sx={{ color: '#1d4ed8', display: 'block' }}>
              {gate.nextAction}
            </Typography>
          </Box>
        )}
      </Box>
    </Paper>
  )
}
