import { Box, Card, Chip, Divider, IconButton, Typography } from '@mui/material'
import { X } from 'lucide-react'
import { MatchScoreDisplay } from './MatchScoreDisplay'
import type { MatchCardViewModel } from './MatchCardViewModel'

const RESULT_TYPE_META: Record<string, { label: string; color: string }> = {
  VERIFIED_PORTFOLIO:  { label: 'Verified', color: '#1e3a5f' },
  EXTERNAL_MARKET:     { label: 'Extern',   color: '#d97706' },
  FUTURE_AVAILABILITY: { label: 'Signal',   color: '#7c3aed' },
}

interface Props {
  vm: MatchCardViewModel
  onRemove?: () => void
}

export function MatchCardCompareMini({ vm, onRemove }: Props) {
  const rt = RESULT_TYPE_META[vm.resultType] ?? { label: vm.resultType, color: '#64748b' }
  const topReason = vm.reasons[0]

  return (
    <Card sx={{ p: 1.5, minWidth: 180, maxWidth: 220, position: 'relative', flexShrink: 0 }}>
      {onRemove && (
        <IconButton
          size="small"
          onClick={onRemove}
          sx={{ position: 'absolute', top: 4, right: 4, p: 0.25 }}
        >
          <X size={14} />
        </IconButton>
      )}

      <Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 0.5, pr: 2 }}>
        <MatchScoreDisplay score={vm.matchScore} size="sm" />
        <Chip
          label={rt.label}
          size="small"
          sx={{ bgcolor: rt.color, color: 'white', fontSize: 10, height: 18 }}
        />
      </Box>

      <Typography variant="caption" sx={{ fontWeight: 600, display: 'block' }} noWrap>
        {vm.title}
      </Typography>
      <Typography variant="caption" color="text.secondary" noWrap>
        {vm.locationLabel}
      </Typography>

      {topReason && (
        <>
          <Divider sx={{ my: 0.75 }} />
          <Typography
            variant="caption"
            color="text.secondary"
            sx={{
              display: '-webkit-box',
              overflow: 'hidden',
              WebkitLineClamp: 2,
              WebkitBoxOrient: 'vertical',
            }}
          >
            {topReason.explanation}
          </Typography>
        </>
      )}
    </Card>
  )
}
