import { Box, Chip, Typography } from '@mui/material'
import { ShortlistStatusBadge } from './ShortlistStatusBadge'
import { useShortlistStore } from '../../stores/shortlistStore'
import type { Shortlist } from '../../domain/shortlist'

interface Props {
  shortlist: Shortlist
}

export function ShortlistCard({ shortlist }: Props) {
  const { selectedShortlistId, setSelectedShortlist } = useShortlistStore()
  const isSelected = selectedShortlistId === shortlist.id

  const updatedDate = new Date(shortlist.updatedAt).toLocaleDateString('de-CH', { day: '2-digit', month: '2-digit', year: 'numeric' })

  return (
    <Box
      onClick={() => setSelectedShortlist(isSelected ? null : shortlist.id)}
      sx={{
        p: 1.5,
        cursor: 'pointer',
        borderBottom: '1px solid #f1f5f9',
        borderLeft: isSelected ? '3px solid #1e3a5f' : '3px solid transparent',
        bgcolor: isSelected ? '#eff6ff' : 'transparent',
        '&:hover': { bgcolor: isSelected ? '#eff6ff' : '#f8fafc' },
      }}
    >
      <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', mb: 0.5, gap: 0.5 }}>
        <Typography variant="body2" sx={{ fontWeight: 600, lineHeight: 1.3, flex: 1 }}>
          {shortlist.title}
        </Typography>
        <Chip
          label={shortlist.items.length}
          size="small"
          sx={{ bgcolor: '#e2e8f0', color: '#475569', fontSize: 10, height: 18, minWidth: 22, flexShrink: 0 }}
        />
      </Box>
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.75, flexWrap: 'wrap' }}>
        <ShortlistStatusBadge status={shortlist.status} />
        <Typography variant="caption" color="text.secondary">{updatedDate}</Typography>
      </Box>
    </Box>
  )
}
