import type {
  AgentRunId,
  ApprovalId,
  AttemptId,
  ChangeSetId,
  ConnectionId,
  CredentialReferenceId,
  DecisionId,
  FactoryRunId,
  GoalId,
  PlanId,
  PlanRevisionId,
  PrincipalId,
  ProjectId,
  ProjectVisionVersionId,
  ProviderId,
  ProviderReference,
  RepositoryId,
  ReviewId,
  ReviewFindingId,
  VerificationEvidenceId,
  TaskId,
  WorkspaceId,
} from "@awp/contracts";
import type { CandidateManifest } from "./candidate.js";

export interface RevisionedEntity {
  readonly revision: number;
}
export type ProjectStatus = "draft" | "onboarding" | "active" | "paused" | "archived";
export interface Project extends RevisionedEntity {
  readonly id: ProjectId;
  readonly name: string;
  readonly repositoryUrl: string;
  readonly requiredChecks: readonly string[];
  readonly status: ProjectStatus;
}
export interface ProjectVisionVersion {
  readonly id: ProjectVisionVersionId;
  readonly projectId: ProjectId;
  readonly sequence: number;
  readonly summary: string;
  readonly supersedesId?: ProjectVisionVersionId;
}
export type GoalStatus = "draft" | "active" | "future" | "parked" | "achieved" | "abandoned";
export type GoalReadiness = "not-ready" | "ready" | "blocked";
export interface Goal extends RevisionedEntity {
  readonly id: GoalId;
  readonly projectId: ProjectId;
  readonly title: string;
  readonly description?: string;
  readonly status: GoalStatus;
  readonly priority?: number;
  readonly targetDate?: string;
  readonly successCriteria: readonly string[];
  readonly readiness?: GoalReadiness;
  readonly readinessReason?: string;
}
export type PlanStatus =
  "draft" | "ready" | "approved" | "queued" | "active" | "paused" | "completed" | "cancelled";
export interface Plan extends RevisionedEntity {
  readonly id: PlanId;
  readonly projectId: ProjectId;
  readonly title: string;
  readonly status: PlanStatus;
}
export interface PlanRevision {
  readonly id: PlanRevisionId;
  readonly planId: PlanId;
  readonly projectId: ProjectId;
  readonly sequence: number;
  readonly title: string;
  readonly projectVisionVersionId?: ProjectVisionVersionId;
  readonly goalIds: readonly GoalId[];
}
export type TaskStatus =
  | "planned"
  | "ready"
  | "dispatched"
  | "queued"
  | "executing"
  | "waiting"
  | "blocked"
  | "review"
  | "correction"
  | "completed"
  | "cancelled"
  | "failed";
export interface Task extends RevisionedEntity {
  readonly id: TaskId;
  readonly projectId: ProjectId;
  readonly planRevisionId: PlanRevisionId;
  readonly title: string;
  readonly position?: number;
  readonly status: TaskStatus;
  readonly dependencyIds: readonly TaskId[];
  readonly goalIds?: readonly GoalId[];
}
export type FactoryRunStatus =
  | "planned"
  | "queued"
  | "starting"
  | "running"
  | "waiting"
  | "blocked"
  | "cancelling"
  | "cancelled"
  | "failed"
  | "retryable"
  | "resolving"
  | "completed";
export interface FactoryRun extends RevisionedEntity {
  readonly id: FactoryRunId;
  readonly projectId: ProjectId;
  readonly planRevisionId: PlanRevisionId;
  readonly taskId?: TaskId;
  readonly accountId?: string;
  readonly model?: string;
  readonly status: FactoryRunStatus;
  readonly reason?: string;
}
export type AgentRunStatus =
  "planned" | "queued" | "active" | "waiting" | "completed" | "cancelled" | "failed";
export type AgentRunRole = "coder" | "reviewer";
export interface AgentRun extends RevisionedEntity {
  readonly id: AgentRunId;
  readonly factoryRunId: FactoryRunId;
  readonly taskId: TaskId;
  readonly agentPrincipalId: PrincipalId;
  readonly role: AgentRunRole;
  readonly status: AgentRunStatus;
  readonly reason?: string;
}
export interface Workspace extends RevisionedEntity {
  readonly id: WorkspaceId;
  readonly projectId: ProjectId;
  readonly checkpointDigest?: string;
  readonly checkpointedAt?: string;
  readonly checkpointSource?: string;
  readonly checkpointCollectedAt?: string;
  readonly cleanedAt?: string;
}
export type AttemptStatus = "created" | "provisioning" | "running" | "checkpointing" | "terminal";
export type AttemptSelectionKind = "initial" | "retry" | "fallback";
export interface AttemptSelectionProvenance {
  readonly kind: AttemptSelectionKind;
  readonly reason: string;
  readonly previousAttemptId?: AttemptId;
}
export interface AttemptToolCall {
  readonly name: string;
  readonly summary: string;
}
export interface Attempt extends RevisionedEntity {
  readonly id: AttemptId;
  readonly agentRunId: AgentRunId;
  readonly workspaceId: WorkspaceId;
  readonly status: AttemptStatus;
  readonly providerId?: ProviderId;
  readonly accountId?: string;
  readonly model?: string;
  readonly reason?: string;
  readonly toolCalls?: readonly AttemptToolCall[];
  readonly selection: AttemptSelectionProvenance;
  readonly providerReference?: ProviderReference;
}
export type ChangeSetStatus =
  | "collecting"
  | "candidate-ready"
  | "publishing"
  | "published"
  | "reviewing"
  | "verifying"
  | "ready-to-merge"
  | "merged"
  | "changes-requested"
  | "publication-failed"
  | "superseded"
  | "cancelled";
export interface ChangeSet {
  readonly id: ChangeSetId;
  readonly projectId: ProjectId;
  readonly repositoryId?: RepositoryId;
  readonly taskId: TaskId;
  readonly producerAttemptId: AttemptId;
  readonly baseIdentity: string;
  readonly candidateDigest: string;
  readonly candidateManifest: CandidateManifest;
  readonly diff: string;
  readonly repositoryKey?: string;
  readonly publicationReference?: ProviderReference;
  readonly targetReference?: string;
  readonly targetRevision?: string;
  readonly mergeReference?: ProviderReference;
  readonly resultingRevision?: string;
  readonly revision: number;
  readonly status: ChangeSetStatus;
}
export type ReviewStatus = "requested" | "reviewing" | "submitted";
export type ReviewDisposition = "approved" | "changes-requested" | "blocked" | "superseded";
export interface Review {
  readonly id: ReviewId;
  readonly changeSetId: ChangeSetId;
  readonly candidateDigest: string;
  readonly reviewerPrincipalId: PrincipalId;
  readonly reviewerAgentRunId?: AgentRunId;
  readonly status: ReviewStatus;
  readonly disposition?: ReviewDisposition;
}
export type VerificationEvidenceState = "passed" | "failed" | "stale" | "missing";
export type VerificationEvidenceReference =
  | {
      readonly kind: "provider-observation";
      readonly providerReference: ProviderReference;
    }
  | {
      readonly kind: "workspace-checkpoint";
      readonly workspaceId: WorkspaceId;
      readonly checkpointDigest: string;
      readonly observedAt: string;
    };
export interface VerificationEvidence {
  readonly id: VerificationEvidenceId;
  readonly changeSetId: ChangeSetId;
  readonly candidateDigest: string;
  readonly name: string;
  readonly state: VerificationEvidenceState;
  readonly source: string;
  readonly observedAt: string;
  readonly required: boolean;
  readonly reference?: VerificationEvidenceReference;
  readonly details?: Readonly<Record<string, unknown>>;
}
export type ReviewFindingSeverity = "blocking" | "warning" | "recommendation" | "info";
export interface ReviewFinding {
  readonly id: ReviewFindingId;
  readonly changeSetId: ChangeSetId;
  readonly candidateDigest: string;
  readonly severity: ReviewFindingSeverity;
  readonly summary: string;
  readonly source?: string;
  readonly resolved: boolean;
}
export interface Decision extends RevisionedEntity {
  readonly id: DecisionId;
  readonly projectId?: ProjectId;
}
export interface Approval extends RevisionedEntity {
  readonly id: ApprovalId;
  readonly decisionId?: DecisionId;
}
export interface ProjectConnectionBinding extends RevisionedEntity {
  readonly projectId: ProjectId;
  readonly connectionId: ConnectionId;
  readonly credentialReferenceId: CredentialReferenceId;
  readonly capabilities: readonly string[];
  readonly resources: readonly string[];
}
