import { Alert, Box, Button, CircularProgress } 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
  onNeedTitleChange: (t: string) => void
  onBack: () => void
  onSave: () => void
}

export function AISearchSavePreview({
  criteria,
  weights,
  parseResult,
  needTitle,
  overallConfidence,
  isSaving,
  onNeedTitleChange,
  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}
      />
      <Box sx={{ maxWidth: 720, mx: 'auto', display: 'flex', justifyContent: 'space-between', mt: 3 }}>
        <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: '#1e3a5f', '&:hover': { bgcolor: '#162d4a' }, textTransform: 'none' }}
        >
          {isSaving
            ? 'Wird gespeichert…'
            : overallConfidence < 0.6
              ? 'Als Entwurf speichern'
              : 'Suchprofil speichern & Matching starten'}
        </Button>
      </Box>
    </Box>
  )
}
