import type {
  FactoryRunId,
  GoalId,
  PlanId,
  PlanRevisionId,
  ProjectId,
  TaskId,
} from "@awp/contracts";

export type ProjectStatus = "draft" | "onboarding" | "active" | "paused" | "archived";
export type GoalStatus = "draft" | "active" | "future" | "parked" | "achieved" | "abandoned";
export type PlanStatus =
  "draft" | "ready" | "approved" | "queued" | "active" | "paused" | "completed" | "cancelled";
export type FactoryRunStatus =
  | "planned"
  | "queued"
  | "starting"
  | "running"
  | "waiting"
  | "blocked"
  | "cancelling"
  | "cancelled"
  | "failed"
  | "retryable"
  | "resolving"
  | "completed";
export type QueueReadiness =
  | "RUNNING"
  | "READY"
  | "BLOCKED_DEPENDENCY"
  | "BLOCKED_POLICY"
  | "WAITING_USER"
  | "PAUSED"
  | "DONE"
  | "CANCELLED";

/**
 * Serialized shape of Lane A's C1 I1ProjectOverviewReadModel.
 * tests/ui/c1-contract.test.ts locks this shape and fixture to @awp/application.
 * The web package intentionally cannot import @awp/application because the C0
 * architecture gate allows apps/web to consume contracts only.
 */
export interface C1ProjectOverviewPayload {
  readonly project: {
    readonly id: ProjectId;
    readonly name: string;
    readonly repositoryUrl: string;
    readonly requiredChecks: readonly string[];
    readonly status: ProjectStatus;
  };
  readonly goals: readonly {
    readonly id: GoalId;
    readonly title: string;
    readonly status: GoalStatus;
  }[];
  readonly plans: readonly {
    readonly id: PlanId;
    readonly title: string;
    readonly status: PlanStatus;
    readonly goalIds: readonly GoalId[];
  }[];
  readonly queue: readonly {
    readonly taskId: TaskId;
    readonly planRevisionId: PlanRevisionId;
    readonly goalIds: readonly GoalId[];
    readonly position: number;
    readonly readinessState: QueueReadiness;
    readonly unsatisfiedDependencyIds: readonly TaskId[];
    readonly policyBlocked?: boolean;
    readonly waitingUser?: boolean;
    readonly paused?: boolean;
  }[];
  readonly factoryRuns: readonly {
    readonly id: FactoryRunId;
    readonly status: FactoryRunStatus;
    readonly reason?: string;
  }[];
}

/** Exact serialized subset of Lane A's deterministic I1 Task fixture. */
export interface C1TaskFixturePayload {
  readonly id: TaskId;
  readonly projectId: ProjectId;
  readonly planRevisionId: PlanRevisionId;
  readonly title: string;
  readonly status:
    | "planned"
    | "ready"
    | "dispatched"
    | "queued"
    | "executing"
    | "waiting"
    | "blocked"
    | "review"
    | "correction"
    | "completed"
    | "cancelled"
    | "failed";
  readonly dependencyIds: readonly TaskId[];
  readonly goalIds?: readonly GoalId[];
  readonly revision: number;
}

export type SurfaceState = "ready" | "loading" | "error" | "stale" | "permission" | "empty";
export type FactoryLifecycleState =
  "active" | "recovery" | "loading" | "error" | "stale" | "permission";
export type ReviewLifecycleState =
  "changes-requested" | "ready-to-merge" | "merged" | "loading" | "error" | "stale" | "permission";

export interface UiRoute {
  readonly kind: "project" | "queue" | "factory-run" | "review" | "not-found";
  readonly id?: string;
  readonly tab?: string;
  readonly state?: string;
  readonly task?: string;
  readonly file?: string;
  readonly finding?: string;
}

export interface RenderContext {
  readonly project: C1ProjectOverviewPayload;
  readonly tasks: readonly C1TaskFixturePayload[];
  readonly route: UiRoute;
}
