import { v } from "convex/values";

export const ARTIFACT_PATHS = [
  "project.md",
  "business.md",
  "design.md",
  "agent.md",
  "work.md",
  "steps.md",
  "artifacts.md",
  "signals.md",
  "agent-manager.md",
  "context.md",
  "card.md",
] as const;

export type ArtifactPath = (typeof ARTIFACT_PATHS)[number];

export const artifactPath = v.union(
  v.literal("project.md"),
  v.literal("business.md"),
  v.literal("design.md"),
  v.literal("agent.md"),
  v.literal("work.md"),
  v.literal("steps.md"),
  v.literal("artifacts.md"),
  v.literal("signals.md"),
  v.literal("agent-manager.md"),
  v.literal("context.md"),
  v.literal("card.md")
);

interface ProjectSeed {
  readonly defaultBranch: string;
  readonly description?: string;
  readonly name: string;
  readonly repoName: string;
  readonly repoOwner: string;
  readonly repoUrl: string;
}

export const createInitialArtifacts = (
  project: ProjectSeed
): readonly { path: ArtifactPath; content: string }[] => {
  const repository = `${project.repoOwner}/${project.repoName}`;
  const purpose = project.description ?? `Maintain and improve ${repository}.`;

  return [
    {
      content: `# ${project.name}\n\nRepository: [${repository}](${project.repoUrl})\n\n## Project knowledge\n\n- [Business](business.md)\n- [Design](design.md)\n- [Agent](agent.md)\n\n## Delivery\n\n- [Work](work.md)\n- [Steps](steps.md)\n- [Artifacts](artifacts.md)\n`,
      path: "project.md",
    },
    {
      content: `# Business\n\n## Purpose\n\n${purpose}\n\n## Source of truth\n\nThe connected GitHub repository and its project issues define the current product work.\n`,
      path: "business.md",
    },
    {
      content: `# Design\n\n## Repository boundary\n\nWork targets \`${repository}\` on \`${project.defaultBranch}\`. Preserve its established architecture and conventions before introducing new patterns.\n\n## Decision record\n\nRecord issue-specific architecture decisions here when they affect later work.\n`,
      path: "design.md",
    },
    {
      content: `# Agent\n\n## Role\n\nOwn one project issue at a time. Read the project artifacts before editing code, ask a focused question when blocked, and support completion claims with command output.\n\n## Guardrails\n\n- Keep issue work isolated in its AgentOS workspace.\n- Update work.md, steps.md, artifacts.md, and context.md as facts change.\n- Never mark work complete without a relevant runtime check.\n`,
      path: "agent.md",
    },
    {
      content:
        "# Work\n\nNo issue has entered the agent queue yet. New issues are appended here by the control plane.\n",
      path: "work.md",
    },
    {
      content:
        "# Steps\n\n1. Read the issue and project artifacts.\n2. Inspect the repository context.\n3. Ask for missing decisions only when work cannot continue safely.\n4. Implement the smallest complete change.\n5. Run the relevant command or scenario.\n6. Publish changed artifacts and the final signal.\n",
      path: "steps.md",
    },
    {
      content:
        "# Artifacts\n\nThis file indexes concrete outputs from issue runs, including changed paths, command results, and preview links.\n",
      path: "artifacts.md",
    },
    {
      content:
        "# Signals\n\nThe agent manager emits `issue.queued`, `agent.started`, `agent.needs-input`, `agent.completed`, and `agent.failed`. Signals are durable project events and drive the web status.\n",
      path: "signals.md",
    },
    {
      content:
        "# Agent manager\n\n## State machine\n\n`open -> queued -> working -> completed`\n\nA blocked run moves from `working` to `needs-input`; a resumed run returns to `queued`. An unrecoverable run moves to `failed`.\n\nEach issue owns one Flue agent identity and one AgentOS actor key, so conversation and workspace state remain isolated.\n",
      path: "agent-manager.md",
    },
    {
      content: `# Context\n\n- Provider: GitHub\n- Repository: ${repository}\n- URL: ${project.repoUrl}\n- Default branch: ${project.defaultBranch}\n\nIssue-specific facts belong below this repository context.\n`,
      path: "context.md",
    },
    {
      content:
        "# Project card\n\nThe web project card is the interface between UI and packages. It reads `projects.list`, `project-artifacts.list`, and `project-issues.list`; it writes through `projects.connectGitHub`, `project-issues.create`, and `project-issues.begin`. The issue-scoped Flue agent reads `agent-workspace.get`, publishes with `agent-workspace.updateArtifact`, and reports state with `agent-workspace.setStatus`. Filesystem and shell operations are executed by the AgentOS sandbox adapter through durable daemon commands.\n",
      path: "card.md",
    },
  ];
};
