import { Box, Chip, CircularProgress, Tooltip, Typography } from '@mui/material'
import {
  AlertTriangle,
  Bookmark,
  BookmarkCheck,
  Bot,
  Building2,
  CheckCircle,
  ClipboardList,
  Edit,
  FileText,
  GitMerge,
  Radar,
  Search,
  ServerCog,
  TrendingUp,
  User,
  XCircle,
} from 'lucide-react'
import { usePropertyActivityLog } from '../../hooks/useActivityLog'
import type { ActivityCategory, ActivityEventType } from '../../services/governanceService'
import { DS_TEXT, DS_BG, DS_BORDER } from '../../lib/ds'

const EVENT_LABELS: Record<ActivityEventType, string> = {
  PROPERTY_CREATED: 'Objekt erstellt',
  PROPERTY_UPDATED: 'Objekt aktualisiert',
  MATCH_APPROVED: 'Match genehmigt',
  MATCH_REJECTED: 'Match abgelehnt',
  SIGNAL_VERIFIED: 'Signal verifiziert',
  NEED_CREATED: 'Bedarf registriert',
  REVIEW_REQUESTED: 'Überprüfung angefordert',
  AI_PARSE_COMPLETED: 'KI-Analyse abgeschlossen',
  AI_OUTPUT_REVIEWED: 'KI-Output geprüft',
  MATCH_GENERATED: 'Matches generiert',
  FUTURE_SIGNAL_DETECTED: 'Zukunftssignal erkannt',
  FUTURE_SIGNAL_CONVERTED: 'Signal konvertiert',
  SHORTLIST_CREATED: 'Shortlist erstellt',
  SHORTLIST_FINALIZED: 'Shortlist finalisiert',
  DECISION_BRIEF_CREATED: 'Entscheidungsbriefing erstellt',
  SOURCE_CRAWLED: 'Quelle gecrawlt',
  DATA_QUALITY_FLAGGED: 'Datenqualität markiert',
  REVIEW_COMPLETED: 'Prüfung abgeschlossen',
}

const EVENT_COLORS: Record<ActivityEventType, string> = {
  PROPERTY_CREATED: '#152642',
  PROPERTY_UPDATED: '#152642',
  MATCH_APPROVED: '#1a7a4a',
  MATCH_REJECTED: '#c0392b',
  SIGNAL_VERIFIED: '#7c3aed',
  NEED_CREATED: '#0891b2',
  REVIEW_REQUESTED: '#d97706',
  AI_PARSE_COMPLETED: '#0891b2',
  AI_OUTPUT_REVIEWED: '#1a7a4a',
  MATCH_GENERATED: '#0891b2',
  FUTURE_SIGNAL_DETECTED: '#7c3aed',
  FUTURE_SIGNAL_CONVERTED: '#7c3aed',
  SHORTLIST_CREATED: '#0f766e',
  SHORTLIST_FINALIZED: '#0f766e',
  DECISION_BRIEF_CREATED: '#0f766e',
  SOURCE_CRAWLED: '#6366f1',
  DATA_QUALITY_FLAGGED: '#ea580c',
  REVIEW_COMPLETED: '#1a7a4a',
}

const CATEGORY_META: Record<ActivityCategory, { label: string; color: string }> = {
  SUCHE:        { label: 'Suche',        color: '#0891b2' },
  MATCHING:     { label: 'Matching',     color: '#1a7a4a' },
  INTELLIGENCE: { label: 'Intelligence', color: '#7c3aed' },
  REVIEW:       { label: 'Review',       color: '#d97706' },
  GOVERNANCE:   { label: 'Governance',   color: '#152642' },
}

function getEventIcon(type: ActivityEventType) {
  const s = 12
  switch (type) {
    case 'PROPERTY_CREATED': return <Building2 size={s} color="white" />
    case 'PROPERTY_UPDATED': return <Edit size={s} color="white" />
    case 'MATCH_APPROVED': return <CheckCircle size={s} color="white" />
    case 'MATCH_REJECTED': return <XCircle size={s} color="white" />
    case 'SIGNAL_VERIFIED': return <TrendingUp size={s} color="white" />
    case 'NEED_CREATED': return <Search size={s} color="white" />
    case 'REVIEW_REQUESTED': return <ClipboardList size={s} color="white" />
    case 'AI_PARSE_COMPLETED':
    case 'AI_OUTPUT_REVIEWED':
    case 'MATCH_GENERATED':
    case 'DECISION_BRIEF_CREATED': return <Bot size={s} color="white" />
    case 'FUTURE_SIGNAL_DETECTED':
    case 'FUTURE_SIGNAL_CONVERTED': return <Radar size={s} color="white" />
    case 'SHORTLIST_CREATED': return <Bookmark size={s} color="white" />
    case 'SHORTLIST_FINALIZED': return <BookmarkCheck size={s} color="white" />
    case 'SOURCE_CRAWLED': return <ServerCog size={s} color="white" />
    case 'DATA_QUALITY_FLAGGED': return <AlertTriangle size={s} color="white" />
    case 'REVIEW_COMPLETED': return <GitMerge size={s} color="white" />
    default: return <FileText size={s} color="white" />
  }
}

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

function formatTime(iso: string): string {
  return new Date(iso).toLocaleTimeString('de-CH', { hour: '2-digit', minute: '2-digit' })
}

function dateKey(iso: string): string {
  return iso.slice(0, 10)
}

interface ActivityEvent {
  id: string
  type: ActivityEventType
  category: ActivityCategory
  entityId: string
  entityType: string
  performedBy: string
  isAiAction: boolean
  organizationId: string
  notes?: string
  createdAt: string
  propertyId?: string
}

function groupByDate(events: ActivityEvent[]) {
  const map = new Map<string, ActivityEvent[]>()
  for (const e of events) {
    const k = dateKey(e.createdAt)
    if (!map.has(k)) map.set(k, [])
    map.get(k)!.push(e)
  }
  return [...map.entries()]
    .sort(([a], [b]) => b.localeCompare(a))
    .map(([key, evts]) => ({ key, label: formatDate(evts[0].createdAt), events: evts }))
}

function EventRow({ event, isLast }: { event: ActivityEvent; isLast: boolean }) {
  const color = EVENT_COLORS[event.type] ?? DS_TEXT.muted
  const catMeta = CATEGORY_META[event.category]

  return (
    <Box sx={{ display: 'flex', gap: 1.25, position: 'relative' }}>
      {!isLast && (
        <Box sx={{ position: 'absolute', left: 12, top: 28, bottom: -4, width: 2, bgcolor: DS_BORDER.default, zIndex: 0 }} />
      )}
      <Box
        sx={{
          width: 26, height: 26, borderRadius: '50%', bgcolor: color,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          flexShrink: 0, zIndex: 1, boxShadow: '0 0 0 3px white', mt: 0.25,
        }}
      >
        {getEventIcon(event.type)}
      </Box>
      <Box sx={{ flex: 1, minWidth: 0, pb: isLast ? 0 : 1.75 }}>
        <Box sx={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: 1 }}>
          <Box sx={{ flex: 1, minWidth: 0 }}>
            <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5, flexWrap: 'wrap' }}>
              <Typography variant="body2" sx={{ fontWeight: 600, fontSize: '0.775rem' }}>
                {EVENT_LABELS[event.type]}
              </Typography>
              <Chip
                label={catMeta.label}
                size="small"
                sx={{ height: 14, fontSize: '0.6rem', bgcolor: catMeta.color + '18', color: catMeta.color, fontWeight: 600 }}
              />
              {event.isAiAction && (
                <Tooltip title="KI-System">
                  <Chip
                    label="KI"
                    size="small"
                    sx={{ height: 14, fontSize: '0.6rem', bgcolor: DS_BG.subtle, color: '#6366f1', fontWeight: 700, cursor: 'default' }}
                  />
                </Tooltip>
              )}
            </Box>
            {event.notes && (
              <Typography variant="caption" color="text.secondary" sx={{ display: 'block', mt: 0.2, lineHeight: 1.45, fontSize: '0.72rem' }}>
                {event.notes}
              </Typography>
            )}
            <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.4, mt: 0.4 }}>
              {event.isAiAction
                ? <Bot size={10} color="#6366f1" />
                : <User size={10} color={DS_TEXT.disabled} />
              }
              <Typography variant="caption" sx={{ color: DS_TEXT.disabled, fontSize: '0.68rem' }}>
                {event.isAiAction ? 'KI-System' : event.performedBy}
              </Typography>
            </Box>
          </Box>
          <Typography variant="caption" sx={{ color: DS_TEXT.disabled, fontSize: '0.68rem', flexShrink: 0, pt: 0.25 }}>
            {formatTime(event.createdAt)}
          </Typography>
        </Box>
      </Box>
    </Box>
  )
}

interface PropertyActivityLogPanelProps {
  propertyId: string
}

export function PropertyActivityLogPanel({ propertyId }: PropertyActivityLogPanelProps) {
  const { data: events = [], isLoading } = usePropertyActivityLog(propertyId)

  if (isLoading) {
    return (
      <Box sx={{ display: 'flex', justifyContent: 'center', py: 4 }}>
        <CircularProgress size={20} />
      </Box>
    )
  }

  if (events.length === 0) {
    return (
      <Box sx={{ py: 4, textAlign: 'center' }}>
        <Typography variant="body2" color="text.secondary" sx={{ fontSize: '0.85rem' }}>
          Noch keine Aktivitäten für dieses Objekt.
        </Typography>
      </Box>
    )
  }

  const grouped = groupByDate(events)

  return (
    <Box>
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 2 }}>
        <Typography variant="caption" sx={{ color: DS_TEXT.muted, fontWeight: 600, fontSize: '0.72rem', textTransform: 'uppercase', letterSpacing: 0.4 }}>
          {events.length} Ereignisse · Nur für Verwaltung
        </Typography>
      </Box>

      {grouped.map(({ key, label, events: dayEvents }) => (
        <Box key={key} sx={{ mb: 2.5 }}>
          <Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 1.25 }}>
            <Typography
              variant="caption"
              sx={{ fontWeight: 700, fontSize: '0.7rem', color: DS_TEXT.muted, textTransform: 'uppercase', letterSpacing: 0.4, whiteSpace: 'nowrap' }}
            >
              {label}
            </Typography>
            <Box sx={{ flex: 1, height: 1, bgcolor: DS_BORDER.default }} />
            <Typography variant="caption" sx={{ color: DS_TEXT.disabled, fontSize: '0.65rem', whiteSpace: 'nowrap' }}>
              {dayEvents.length}×
            </Typography>
          </Box>
          {dayEvents.map((evt, idx) => (
            <EventRow key={evt.id} event={evt} isLast={idx === dayEvents.length - 1} />
          ))}
        </Box>
      ))}
    </Box>
  )
}
