import { Alert, Box, Button, CircularProgress, FormControlLabel, Switch, Typography } from '@mui/material'
import { Save } from 'lucide-react'
import { NeedCardPreview } from './NeedCardPreview'
import type { ParseNeedResult, ParsedNeedCriteria, WeightingKey } from '../../domain/needBuilder'

interface Props {
  criteria: ParsedNeedCriteria
  weights: Record<WeightingKey, number>
  parseResult: ParseNeedResult
  needTitle: string
  overallConfidence: number
  isSaving: boolean
  isAnonymous: boolean
  onNeedTitleChange: (t: string) => void
  onAnonymousChange: (v: boolean) => void
  onBack: () => void
  onSave: () => void
}

export function AISearchSavePreview({
  criteria,
  weights,
  parseResult,
  needTitle,
  overallConfidence,
  isSaving,
  isAnonymous,
  onNeedTitleChange,
  onAnonymousChange,
  onBack,
  onSave,
}: Props) {
  return (
    <Box sx={{ maxWidth: 1200, mx: 'auto' }}>
      <Alert severity="success" sx={{ mb: 3 }}>
        Dieses Suchprofil wird als aktiver Bedarf gespeichert und erscheint automatisch im Match Center der Verwaltung.
      </Alert>
      <NeedCardPreview
        criteria={criteria}
        weights={weights}
        confidenceByField={parseResult.confidenceByField}
        missingFields={parseResult.missingFields}
        needTitle={needTitle}
        onNeedTitleChange={onNeedTitleChange}
      />

      {/* Anonymity option */}
      <Box sx={{
        maxWidth: 720, mx: 'auto', mt: 2,
        p: 1.5, borderRadius: 1.5, border: '1px solid',
        borderColor: isAnonymous ? '#7c3aed' : '#e8e7e4',
        bgcolor: isAnonymous ? '#f5f3ff' : '#f8fafc',
        transition: 'all 0.15s',
      }}>
        <FormControlLabel
          control={
            <Switch
              size="small"
              checked={isAnonymous}
              onChange={e => onAnonymousChange(e.target.checked)}
              sx={{
                '& .MuiSwitch-switchBase.Mui-checked': { color: '#7c3aed' },
                '& .MuiSwitch-switchBase.Mui-checked + .MuiSwitch-track': { bgcolor: '#7c3aed' },
              }}
            />
          }
          label={
            <Box>
              <Typography variant="body2" sx={{ fontWeight: 700, color: isAnonymous ? '#6d28d9' : 'text.primary' }}>
                Anonymes Suchprofil
              </Typography>
              <Typography variant="caption" sx={{ color: isAnonymous ? '#7c3aed' : 'text.secondary' }}>
                Firmenname wird nicht an Vermieter übermittelt — Verwalter sehen nur Branche und Flächenbedarf
              </Typography>
            </Box>
          }
        />
      </Box>

      <Box sx={{ maxWidth: 720, mx: 'auto', display: 'flex', justifyContent: 'space-between', mt: 2 }}>
        <Button variant="outlined" onClick={onBack} disabled={isSaving}>
          ← Zurück
        </Button>
        <Button
          variant="contained"
          onClick={onSave}
          disabled={isSaving}
          endIcon={isSaving ? <CircularProgress size={16} color="inherit" /> : <Save size={16} />}
          sx={{ bgcolor: '#152642', '&:hover': { bgcolor: '#162d4a' }, textTransform: 'none' }}
        >
          {isSaving
            ? 'Wird gespeichert…'
            : overallConfidence < 0.6
              ? 'Als Entwurf speichern'
              : 'Suchprofil speichern & Matching starten'}
        </Button>
      </Box>
    </Box>
  )
}
