import { formatDistanceToNow } from 'date-fns';
import type { Task } from '@/lib/types';
import { StatusPill } from '@/components/ui/status-pill';

type TaskFeedProps = {
  tasks: Task[];
};

const taskLabels: Record<Task['task_type'], string> = {
  sync_filings: 'Sync filings',
  refresh_prices: 'Refresh prices',
  analyze_filing: 'Analyze filing',
  portfolio_insights: 'Portfolio insights',
  index_search: 'Index search'
};

export function TaskFeed({ tasks }: TaskFeedProps) {
  if (tasks.length === 0) {
    return <p className="text-sm text-[color:var(--terminal-muted)]">No recent tasks.</p>;
  }

  return (
    <ul>
      {tasks.slice(0, 8).map((task) => (
        <li key={task.id} className="flex items-center justify-between gap-3 border-b border-[color:var(--line-weak)] py-3 last:border-b-0 last:pb-0 first:pt-0">
          <div>
            <p className="text-sm text-[color:var(--terminal-bright)]">{taskLabels[task.task_type]}</p>
            <p className="text-xs text-[color:var(--terminal-muted)]">
              {formatDistanceToNow(new Date(task.created_at), { addSuffix: true })}
            </p>
          </div>
          <StatusPill status={task.status} />
        </li>
      ))}
    </ul>
  );
}
