import type { FactoryRunId, GoalId, PlanId, ProjectId, TaskId } from "@awp/contracts";
import {
  buildQueue,
  type QueueEntry,
  type FactoryRun,
  type Goal,
  type Plan,
  type Project,
  type Task,
} from "@awp/domain";
export interface I1GoalReadModel {
  readonly id: GoalId;
  readonly title: string;
  readonly status: Goal["status"];
}
export interface I1PlanReadModel {
  readonly id: PlanId;
  readonly title: string;
  readonly status: Plan["status"];
  readonly goalIds: readonly GoalId[];
}
export interface I1FactoryRunReadModel {
  readonly id: FactoryRunId;
  readonly status: FactoryRun["status"];
  readonly reason?: string;
}
export interface I1ProjectOverviewReadModel {
  readonly project: {
    readonly id: ProjectId;
    readonly name: string;
    readonly status: Project["status"];
  };
  readonly goals: readonly I1GoalReadModel[];
  readonly plans: readonly I1PlanReadModel[];
  readonly queue: readonly QueueEntry[];
  readonly factoryRuns: readonly I1FactoryRunReadModel[];
}
export function projectOverviewReadModel(input: {
  project: Project;
  goals: readonly Goal[];
  plans: readonly Plan[];
  planGoalIds: ReadonlyMap<PlanId, readonly GoalId[]>;
  tasks: readonly Task[];
  queueOrder: readonly TaskId[];
  factoryRuns: readonly FactoryRun[];
}): I1ProjectOverviewReadModel {
  return {
    project: { id: input.project.id, name: input.project.name, status: input.project.status },
    goals: input.goals.map((g) => ({ id: g.id, title: g.title, status: g.status })),
    plans: input.plans.map((p) => ({
      id: p.id,
      title: p.title,
      status: p.status,
      goalIds: input.planGoalIds.get(p.id) ?? [],
    })),
    queue: buildQueue(input.tasks, input.queueOrder),
    factoryRuns: input.factoryRuns.map((r) =>
      r.reason === undefined
        ? { id: r.id, status: r.status }
        : { id: r.id, status: r.status, reason: r.reason },
    ),
  };
}
