import { Box, Chip, Table, TableBody, TableCell, TableHead, TableRow, Typography } from '@mui/material'
import type { ConnectorRun } from '../../domain/dataSource'
import { CONNECTOR_RUN_STATUS_LABELS, CONNECTOR_RUN_STATUS_COLORS } from '../../domain/dataSource'

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

function formatDuration(start: string, end?: string): string {
  if (!end) return 'läuft...'
  const ms = new Date(end).getTime() - new Date(start).getTime()
  if (ms < 60_000) return `${Math.round(ms / 1000)}s`
  return `${Math.round(ms / 60_000)}m`
}

interface ConnectorRunTableProps {
  runs: ConnectorRun[]
  onSelectRun: (run: ConnectorRun) => void
}

export function ConnectorRunTable({ runs, onSelectRun }: ConnectorRunTableProps) {
  if (runs.length === 0) {
    return (
      <Typography variant="body2" sx={{ color: 'text.secondary', fontStyle: 'italic', py: 2 }}>
        Keine Import-Runs vorhanden.
      </Typography>
    )
  }

  return (
    <Box sx={{ overflowX: 'auto' }}>
      <Table size="small">
        <TableHead>
          <TableRow sx={{ '& th': { fontSize: '0.72rem', fontWeight: 600, color: '#64748b', borderBottom: '2px solid #e2e8f0' } }}>
            <TableCell>Gestartet</TableCell>
            <TableCell>Status</TableCell>
            <TableCell align="right">Erkannt</TableCell>
            <TableCell align="right">Normalisiert</TableCell>
            <TableCell align="right">Signale</TableCell>
            <TableCell align="right">Dauer</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {runs.map((run) => {
            const { bg, fg } = CONNECTOR_RUN_STATUS_COLORS[run.status]
            return (
              <TableRow
                key={run.id}
                hover
                onClick={() => onSelectRun(run)}
                sx={{ cursor: 'pointer', '& td': { fontSize: '0.78rem', py: 0.75 } }}
              >
                <TableCell>{formatDate(run.startedAt)}</TableCell>
                <TableCell>
                  <Chip
                    size="small"
                    label={CONNECTOR_RUN_STATUS_LABELS[run.status]}
                    sx={{ bgcolor: bg, color: fg, border: 'none', fontSize: '0.68rem', fontWeight: 600 }}
                  />
                </TableCell>
                <TableCell align="right">{run.itemsDetected}</TableCell>
                <TableCell align="right">{run.itemsNormalized}</TableCell>
                <TableCell align="right">
                  <Typography sx={{ fontSize: '0.78rem', color: run.signalsCreated > 0 ? '#7c3aed' : 'text.primary', fontWeight: run.signalsCreated > 0 ? 600 : 400 }}>
                    {run.signalsCreated}
                  </Typography>
                </TableCell>
                <TableCell align="right" sx={{ color: '#94a3b8' }}>
                  {formatDuration(run.startedAt, run.finishedAt)}
                </TableCell>
              </TableRow>
            )
          })}
        </TableBody>
      </Table>
    </Box>
  )
}
