import { createElement } from 'react'
import type { DataTableColumn } from './internal/DataTable'
import { PlanTableRow, type PlanTableItem } from './PlanTableRow'
import type { ResolveAction } from './resolve-action'
import { planStatusCategory } from './StatusChip'

export type PlanTableColumnKey = 'title' | 'status' | 'attempts' | 'tasks' | 'created' | 'updated' | 'runningFor' | 'agents' | 'actions'
export const STATUS_SEVERITY = { 'needs-you': 5, failed: 4, running: 3, queued: 2, completed: 1, unknown: 0 }

function numericDate(value: string | undefined): number | null {
  if (!value) return null
  const parsed = Date.parse(value)
  return Number.isFinite(parsed) ? parsed : null
}

export function planTableColumns(handlers: { selectedRunId: string | null; onOpen(item: PlanTableItem): void; onAbandon(item: PlanTableItem): void; onResolve(item: PlanTableItem, action: ResolveAction): void }): DataTableColumn<PlanTableItem>[] {
  const cell = (column: PlanTableColumnKey) => (item: PlanTableItem) => createElement(PlanTableRow, { item, column, selected: handlers.selectedRunId === item.runId, onOpen: handlers.onOpen, onAbandon: handlers.onAbandon, onResolve: handlers.onResolve })
  return [
    { id: 'title', header: 'Plan', minWidth: 260, sortable: true, sortValue: (item) => (item.title || item.slug).toLowerCase(), searchValue: (item) => `${item.title} ${item.slug}`, cell: cell('title') },
    { id: 'status', header: 'Status', minWidth: 120, sortable: true, sortValue: (item) => STATUS_SEVERITY[planStatusCategory(item.status, item.needsYou)], searchValue: (item) => `${item.status} ${item.needsYou ? 'needs you' : ''}`, cell: cell('status') },
    { id: 'attempts', header: 'Attempts', minWidth: 90, sortable: true, sortValue: (item) => item.attempts ?? 1, searchValue: (item) => String(item.attempts ?? 1), cell: cell('attempts') },
    { id: 'tasks', header: 'Tasks', minWidth: 80, sortable: true, sortValue: (item) => item.tasksTotal > 0 ? item.tasksCompleted / item.tasksTotal : 0, searchValue: (item) => `${item.tasksCompleted}/${item.tasksTotal}`, cell: cell('tasks') },
    { id: 'created', header: 'Created', minWidth: 110, sortable: true, sortValue: (item) => numericDate(item.createdDate), searchValue: (item) => item.createdDate ?? '', cell: cell('created') },
    { id: 'updated', header: 'Last active', minWidth: 110, sortable: true, sortValue: (item) => item.updatedAtMs, searchValue: (item) => item.updatedAtMs === null ? '' : new Date(item.updatedAtMs).toISOString(), cell: cell('updated') },
    { id: 'runningFor', header: 'Running for', minWidth: 110, sortable: true, sortValue: (item) => item.runningStartedAtMs, searchValue: (item) => item.runningStartedAtMs === null ? '' : new Date(item.runningStartedAtMs).toISOString(), cell: cell('runningFor') },
    { id: 'agents', header: 'Agents', minWidth: 90, sortable: true, sortValue: (item) => item.activeAgents, searchValue: (item) => `${item.activeAgents} ${item.activeAgents === 1 ? 'agent' : 'agents'}`, cell: cell('agents') },
    { id: 'actions', header: 'Actions', minWidth: 120, cell: cell('actions') },
  ]
}

export const PLAN_TABLE_COLUMNS = planTableColumns({ selectedRunId: null, onOpen: () => {}, onAbandon: () => {}, onResolve: () => {} })
