import { Box, Chip, Typography } from '@mui/material'
import type { AssistantContext } from '../../domain/assistant'
import { DS_ACCENT, DS_SLATE } from '../../lib/ds'

const PAGE_LABELS: Record<string, string> = {
  '/supply/properties':         'Startseite',
  '/supply/match-center':       'Eingehende Bedarfe',
  '/supply/data-quality':       'Datenpflege',
  '/supply/future-availability':'Marktchancen',
  '/demand/ai-search':          'Flächensuche',
  '/demand/results':            'Ergebnisse',
  '/demand/compare':            'Vergleich',
  '/demand/shortlists':         'Shortlists',
  '/ops/review-queue':          'Review Queue',
  '/ops/ai-monitoring':         'AI Monitoring',
  '/ops/governance':            'Governance',
}

function resolvePageLabel(route: string): string {
  for (const [path, label] of Object.entries(PAGE_LABELS)) {
    if (route.startsWith(path)) return label
  }
  return route.split('/').filter(Boolean).pop()?.replace(/-/g, ' ') ?? 'Seite'
}

const ENTITY_LABELS: Record<string, string> = {
  PROPERTY: 'Objekt', NEED: 'Gesuch', MATCH: 'Match',
  SIGNAL: 'Signal', AI_OUTPUT: 'AI-Output',
}

interface Props {
  context: AssistantContext
}

export function AssistantContextSummary({ context }: Props) {
  const pageLabel = resolvePageLabel(context.currentRoute)

  return (
    <Box sx={{ px: 2, py: 1, bgcolor: DS_SLATE[50], borderBottom: '1px solid #e2e8f0' }}>
      <Box sx={{ display: 'flex', gap: 0.75, flexWrap: 'wrap', alignItems: 'center' }}>
        <Chip
          label={pageLabel}
          size="small"
          sx={{ bgcolor: DS_ACCENT.cyan.bg, color: DS_ACCENT.cyan.dark, fontWeight: 600, fontSize: '0.7rem', height: 20 }}
        />
        {context.selectedEntityType && context.selectedEntityId && (
          <Chip
            label={`${ENTITY_LABELS[context.selectedEntityType] ?? context.selectedEntityType}: ${context.selectedEntityId}`}
            size="small"
            sx={{ bgcolor: DS_SLATE[100], color: DS_SLATE[600], fontWeight: 500, fontSize: '0.65rem', height: 20, fontFamily: 'monospace' }}
          />
        )}
        {context.visibleScores?.quality !== undefined && (
          <Chip
            label={`Qualität: ${context.visibleScores.quality}%`}
            size="small"
            sx={{
              bgcolor: context.visibleScores.quality >= 70 ? '#dcfce7' : '#fef3c7',
              color: context.visibleScores.quality >= 70 ? '#166534' : '#92400e',
              fontWeight: 600, fontSize: '0.65rem', height: 20,
            }}
          />
        )}
        {context.visibleScores?.matchScore !== undefined && (
          <Chip
            label={`Match: ${context.visibleScores.matchScore}%`}
            size="small"
            sx={{ bgcolor: DS_ACCENT.violet.tint, color: DS_ACCENT.violet.dark, fontWeight: 600, fontSize: '0.65rem', height: 20 }}
          />
        )}
      </Box>
      {context.visibleMissingData && context.visibleMissingData.length > 0 && (
        <Typography variant="caption" color="text.secondary" sx={{ display: 'block', mt: 0.5, fontSize: '0.65rem' }}>
          Fehlende Daten: {context.visibleMissingData.slice(0, 3).join(', ')}
        </Typography>
      )}
    </Box>
  )
}
