import { Chip, Tooltip } from '@mui/material'
import { CheckCircle, Clock, AlertTriangle } from 'lucide-react'
import { FreshnessStatus } from '../../domain/enums'
import { FRESHNESS_LABELS } from '../../lib/constants'
import type { FreshnessStatus as FreshnessStatusType } from '../../domain/enums'

interface FreshnessIndicatorProps {
  freshness: FreshnessStatusType
  lastUpdated?: string
  size?: 'small' | 'medium'
}

const CONFIG: Record<FreshnessStatusType, { color: string; icon: typeof CheckCircle }> = {
  [FreshnessStatus.FRESH]:    { color: '#1a7a4a', icon: CheckCircle },
  [FreshnessStatus.STALE]:    { color: '#d97706', icon: Clock },
  [FreshnessStatus.OUTDATED]: { color: '#c0392b', icon: AlertTriangle },
}

export function FreshnessIndicator({ freshness, lastUpdated, size = 'small' }: FreshnessIndicatorProps) {
  const { color, icon: Icon } = CONFIG[freshness] ?? CONFIG[FreshnessStatus.OUTDATED]
  const label = FRESHNESS_LABELS[freshness] ?? freshness

  const chip = (
    <Chip
      size={size}
      icon={<Icon size={11} color={color} />}
      label={label}
      sx={{
        bgcolor: `${color}18`,
        color,
        fontWeight: 600,
        fontSize: size === 'small' ? '0.7rem' : '0.8125rem',
        border: `1px solid ${color}40`,
        '& .MuiChip-icon': { color },
      }}
    />
  )

  if (!lastUpdated) return chip

  return (
    <Tooltip title={`Zuletzt aktualisiert: ${new Date(lastUpdated).toLocaleDateString('de-CH')}`} arrow>
      {chip}
    </Tooltip>
  )
}
