import { Chip } from '@mui/material'
import { CheckCircle2, AlertTriangle, XCircle } from 'lucide-react'
import type { DataQualityLevel } from '../../domain/enums'
import { DS_COLORS, scoreToDataQualityLevel } from '../../lib/ds'
import { DATA_QUALITY_LABELS } from '../../lib/constants'

interface DataQualityBadgeProps {
  level?: DataQualityLevel
  score?: number
  showScore?: boolean
  size?: 'small' | 'medium'
}

const ICONS: Record<DataQualityLevel, React.ElementType> = {
  HIGH:       CheckCircle2,
  MEDIUM:     AlertTriangle,
  LOW:        XCircle,
  INCOMPLETE: XCircle,
}

export function DataQualityBadge({ level, score, showScore = false, size = 'small' }: DataQualityBadgeProps) {
  const resolved: DataQualityLevel =
    level ?? (score !== undefined ? scoreToDataQualityLevel(score) : 'LOW')
  const { bg, fg } = DS_COLORS.dataQuality[resolved]
  const Icon = ICONS[resolved]
  const scoreLabel = showScore && score !== undefined ? ` ${Math.round(score * 100)}%` : ''
  const label = `${DATA_QUALITY_LABELS[resolved] ?? resolved}${scoreLabel}`
  return (
    <Chip
      size={size}
      label={label}
      icon={<Icon size={11} color={fg} />}
      aria-label={`Datenqualität: ${label}`}
      sx={{
        bgcolor: bg,
        color: fg,
        border: 'none',
        fontWeight: 600,
        fontSize: '0.7rem',
        '& .MuiChip-icon': { ml: 0.5 },
      }}
    />
  )
}
