import { Box, Chip, Typography } from '@mui/material'
import {
  Building2, FileText, Newspaper, TrendingUp, FileCheck2,
  HardHat, Database, CalendarClock, Search, PenLine,
} from 'lucide-react'
import type { LucideIcon } from 'lucide-react'
import {
  MARKET_SIGNAL_SOURCE_LABELS,
  SIGNAL_PROCESSING_STATUS_LABELS,
  SIGNAL_PROCESSING_STATUS_COLORS,
  MarketSignalSourceCategory,
} from '../../domain/marketSignal'
import type { MarketSignal } from '../../domain/marketSignal'
import { SensitivityLevel } from '../../domain/enums'
import { SignalConfidenceBadge } from './SignalConfidenceBadge'

const SOURCE_ICONS: Record<string, LucideIcon> = {
  PUBLIC_LISTING_PLATFORM: Building2,
  BUILDING_PERMIT_REGISTER: FileText,
  COMPANY_NEWS: Newspaper,
  JOB_GROWTH_SIGNAL: TrendingUp,
  COMMERCIAL_REGISTER: FileCheck2,
  INFRASTRUCTURE_PROJECT: HardHat,
  PORTFOLIO_IMPORT: Database,
  LEASE_EXPIRY_DATA: CalendarClock,
  USER_DEMAND_SIGNAL: Search,
  MANUAL_ANALYST_SIGNAL: PenLine,
}

const SENSITIVITY_COLORS: Record<string, string> = {
  [SensitivityLevel.INTERNAL]: '#f59e0b',
  [SensitivityLevel.CONFIDENTIAL]: '#ef4444',
  [SensitivityLevel.RESTRICTED]: '#7c3aed',
}

function formatDate(iso: string): string {
  return new Date(iso).toLocaleDateString('de-CH', { day: '2-digit', month: '2-digit', year: '2-digit' })
}

interface MarketSignalCardProps {
  signal: MarketSignal
  selected: boolean
  onClick: () => void
}

export function MarketSignalCard({ signal, selected, onClick }: MarketSignalCardProps) {
  const Icon = SOURCE_ICONS[signal.sourceCategory] ?? Building2
  const { bg, fg } = SIGNAL_PROCESSING_STATUS_COLORS[signal.processingStatus]
  const sensitivityColor = SENSITIVITY_COLORS[signal.sensitivityLevel]

  return (
    <Box
      onClick={onClick}
      sx={{
        px: 1.5,
        py: 1.25,
        borderBottom: '1px solid #f1f5f9',
        borderLeft: selected ? '3px solid #1e3a5f' : '3px solid transparent',
        bgcolor: selected ? 'rgba(30,58,95,0.04)' : 'transparent',
        cursor: 'pointer',
        transition: 'background-color 0.12s ease',
        '&:hover': { bgcolor: selected ? 'rgba(30,58,95,0.06)' : 'rgba(0,0,0,0.02)' },
      }}
    >
      {/* Row 1: source + sensitivity */}
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.75, mb: 0.5 }}>
        <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
          <Icon size={12} color="#64748b" />
          <Typography sx={{ fontSize: '0.65rem', color: '#64748b' }}>
            {MARKET_SIGNAL_SOURCE_LABELS[signal.sourceCategory as MarketSignalSourceCategory]}
          </Typography>
        </Box>
        {sensitivityColor && (
          <Box
            sx={{
              width: 6,
              height: 6,
              borderRadius: '50%',
              bgcolor: sensitivityColor,
              flexShrink: 0,
            }}
          />
        )}
      </Box>

      {/* Row 2: title */}
      <Typography
        sx={{
          fontSize: '0.8125rem',
          fontWeight: 500,
          color: '#1e293b',
          overflow: 'hidden',
          textOverflow: 'ellipsis',
          display: '-webkit-box',
          WebkitLineClamp: 2,
          WebkitBoxOrient: 'vertical',
          lineHeight: 1.4,
          mb: 0.5,
        }}
      >
        {signal.title}
      </Typography>

      {/* Row 3: location + date */}
      <Typography sx={{ fontSize: '0.7rem', color: '#94a3b8', mb: 0.5 }}>
        {signal.location} · {formatDate(signal.detectedAt)}
      </Typography>

      {/* Row 4: status + confidence */}
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.75, flexWrap: 'wrap' }}>
        <Chip
          size="small"
          label={SIGNAL_PROCESSING_STATUS_LABELS[signal.processingStatus]}
          sx={{
            bgcolor: bg, color: fg, border: 'none', fontWeight: 600,
            fontSize: '0.65rem', height: 18,
          }}
        />
        <SignalConfidenceBadge score={signal.confidenceScore} />
      </Box>
    </Box>
  )
}
