import { Chip, Tooltip } from '@mui/material'
import { dataQualityHex } from '../../lib/utils'
import type { DataQuality } from '../../domain/property'

interface DataQualityBadgeProps {
  quality: DataQuality
  showLabel?: boolean
  size?: 'small' | 'medium'
}

export function DataQualityBadge({ quality, showLabel = false, size = 'small' }: DataQualityBadgeProps) {
  const pct = Math.round(quality.score * 100)
  const hex = dataQualityHex(quality.score)
  const hasCritical = quality.missingCriticalFields.length > 0

  const levelLabel = quality.qualityLevel
    ? { HIGH: 'Hoch', MEDIUM: 'Mittel', LOW: 'Niedrig', INCOMPLETE: 'Unvollständig' }[quality.qualityLevel]
    : null

  const tooltipText = hasCritical
    ? `${quality.missingCriticalFields.length} Pflichtfeld(er) fehlen`
    : `Datenqualität: ${pct}%`

  return (
    <Tooltip title={tooltipText} arrow>
      <Chip
        size={size}
        label={showLabel && levelLabel ? `${pct}% · ${levelLabel}` : `${pct}%`}
        sx={{
          bgcolor: `${hex}18`,
          color: hex,
          fontWeight: 700,
          fontSize: size === 'small' ? '0.7rem' : '0.8125rem',
          border: `1px solid ${hex}40`,
          cursor: 'default',
        }}
      />
    </Tooltip>
  )
}
