export type PrdStatus = 'draft' | 'review' | 'approved' | 'archived';

export interface PrdTemplateSection {
  id: string;
  title: string;
  fields: string[];
}

export interface PrdTemplate {
  id: string;
  name: string;
  sections: PrdTemplateSection[];
  fields: string[];
}

export interface PrdSection {
  id: string;
  title: string;
  fields: Record<string, string>;
}

/**
 * Mission ↔ PRD linkage recorded on the PRD document (the YAML authority).
 *
 * `missionVersion` is the mission-side revision marker available to the CLI
 * (the gateway exposes `updatedAt` for missions — there is no numeric mission
 * version yet). `prdVersion` snapshots the PRD content version at link time.
 */
export interface PrdMissionLinkage {
  missionId: string;
  missionVersion: string;
  prdVersion: number;
  requirementIds: string[];
  linkedAt: string;
}

export interface PrdDocument {
  id: string;
  title: string;
  status: PrdStatus;
  projectPath: string;
  template: string;
  /** Content revision counter. Bumped by updates and accepted imports. */
  version: number;
  sections: PrdSection[];
  missions: PrdMissionLinkage[];
  createdAt: string;
  updatedAt: string;
}

export interface CreatePrdOptions {
  name: string;
  projectPath: string;
  template?: string;
  interactive?: boolean;
}

// ── PrdService surface (single authority entry point) ─────────────────────────

export interface PrdServiceOptions {
  projectPath: string;
}

export interface PrdCreateInput {
  name: string;
  template?: string;
}

export interface PrdSectionPatch {
  id: string;
  fields: Record<string, string>;
}

export interface PrdUpdateInput {
  /** Defaults to the most recently updated PRD. */
  id?: string;
  sections: PrdSectionPatch[];
}

export interface PrdLinkMissionInput {
  /** Defaults to the most recently updated PRD. */
  prdId?: string;
  missionId: string;
  missionVersion: string;
  requirementIds?: string[];
}

export interface PrdPlanForMissionInput extends PrdLinkMissionInput {
  name: string;
  template?: string;
}

export interface PrdExportInput {
  /** Defaults to the most recently updated PRD. */
  id?: string;
  /** Override the generated-view output path. */
  outPath?: string;
}

export interface PrdExportResult {
  filePath: string;
  content: string;
}

/** Discriminated result of a non-conflicting import. */
export type PrdImportResult =
  | { kind: 'created'; document: PrdDocument }
  | { kind: 'identical'; document: PrdDocument };

export interface PrdImportInput {
  /** Path to a YAML-serialized PRD document (NOT the generated Markdown view). */
  filePath: string;
}
