import { Box, Chip, Divider, Typography } from '@mui/material'
import type { UnifiedMatchResult } from '../../domain/unifiedResult'
import { UnifiedResultCard } from './UnifiedResultCard'
import { SCORE_THEME } from '../shared/scoreTheme'

interface Props {
  results: UnifiedMatchResult[]
  view?: 'list' | 'grid'
}

const TIER_CONFIG = [
  {
    key: 'gold'   as const,
    label: 'Top Matches',
    sublabel: '90–100 Punkte',
    filter: (r: UnifiedMatchResult) => r.matchScore >= 90,
  },
  {
    key: 'silver' as const,
    label: 'Starke Matches',
    sublabel: '80–89 Punkte',
    filter: (r: UnifiedMatchResult) => r.matchScore >= 80 && r.matchScore < 90,
  },
  {
    key: 'bronze' as const,
    label: 'Weitere Treffer',
    sublabel: 'unter 80 Punkte',
    filter: (r: UnifiedMatchResult) => r.matchScore < 80,
  },
]

interface TierHeaderProps {
  label: string
  sublabel: string
  tierKey: 'gold' | 'silver' | 'bronze'
  count: number
  isFirst: boolean
}

function TierHeader({ label, sublabel, tierKey, count, isFirst }: TierHeaderProps) {
  const theme = SCORE_THEME[tierKey]
  return (
    <Box sx={{ display: 'flex', alignItems: 'center', gap: 1.5, mt: isFirst ? 0 : 3, mb: 1.5 }}>
      <Box
        sx={{
          width: 4,
          height: 28,
          borderRadius: 2,
          background: theme.gradient,
          flexShrink: 0,
        }}
      />
      <Box>
        <Typography variant="subtitle1" sx={{ fontWeight: 700, lineHeight: 1.2, color: theme.text === '#7a4f00' ? '#92400e' : theme.text === '#334155' ? '#1e293b' : '#7c3d12' }}>
          {label}
        </Typography>
        <Typography variant="caption" color="text.secondary">{sublabel}</Typography>
      </Box>
      <Chip
        label={count}
        size="small"
        sx={{
          fontWeight: 700,
          fontSize: '0.75rem',
          background: theme.gradient,
          color: theme.text,
          border: `1px solid ${theme.border}`,
        }}
      />
      <Divider sx={{ flex: 1 }} />
    </Box>
  )
}

export function UnifiedResultFeed({ results, view = 'list' }: Props) {
  // All results — real properties and Schattenmarkt signals — are sorted into
  // the same score tiers so they appear in a single unified ranked list.
  const tiers = TIER_CONFIG.map(t => ({ ...t, items: results.filter(t.filter) }))
    .filter(t => t.items.length > 0)

  function renderCards(items: UnifiedMatchResult[]) {
    return view === 'grid' ? (
      <Box sx={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(320px, 1fr))', gap: 2 }}>
        {items.map(result => (
          <UnifiedResultCard key={result.matchId} result={result} view="grid" />
        ))}
      </Box>
    ) : (
      <Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
        {items.map(result => (
          <UnifiedResultCard key={result.matchId} result={result} view="list" />
        ))}
      </Box>
    )
  }

  return (
    <Box>
      {tiers.map((tier, idx) => (
        <Box key={tier.key}>
          <TierHeader
            label={tier.label}
            sublabel={tier.sublabel}
            tierKey={tier.key}
            count={tier.items.length}
            isFirst={idx === 0}
          />
          {renderCards(tier.items)}
        </Box>
      ))}
    </Box>
  )
}
