import { selectTaskProcess, type SelectTaskProcessInput, type TaskProcessSelection } from '@/lib/task-process';
import { buildTaskTimelineCards } from '@/lib/task-timeline';
import type { AppLocale } from '@/lib/i18n/core';
import type { BackendTask, TaskTimelineCard } from '@/types';

export type BuildTaskTimelineViewInput = Omit<SelectTaskProcessInput, 'task'> & {
  task: BackendTask | null;
  locale?: AppLocale | string;
};

export type TaskTimelineView = {
  process: TaskProcessSelection;
  cards: TaskTimelineCard[];
};

export function buildTaskTimelineView({
  task,
  liveRuns,
  liveEvents,
  liveArtifacts,
  locale,
}: BuildTaskTimelineViewInput): TaskTimelineView | null {
  if (!task) return null;

  const process = selectTaskProcess({
    task,
    liveRuns,
    liveEvents,
    liveArtifacts,
  });
  return {
    process,
    cards: buildTaskTimelineCards({
      task,
      processRuns: process.runs,
      processEvents: process.events,
      processArtifacts: process.artifacts,
      locale,
    }),
  };
}
