import { z } from "zod";
import { defineCommand, arg } from "politty";
import { executeCommand } from "../../lib/command-result";
import { runProgressLog } from "./log";
import { runProgressSchema } from "./schema-cmd";

const cwd = process.cwd();

const logCommand = defineCommand({
  name: "log",
  description: "Append a validated entry to the progress log",
  args: z.object({
    json: arg(z.string(), {
      description: "JSON payload (ProgressInput schema)",
    }),
    "dry-run": arg(z.boolean().default(false), {
      description: "Validate without writing",
    }),
  }),
  run: (args) => {
    return executeCommand(() => {
      return runProgressLog(args.json, args["dry-run"], cwd);
    });
  },
});

const schemaCommand = defineCommand({
  name: "schema",
  description: "Output JSON Schema for progress log input",
  args: z.object({}),
  run: () => {
    return executeCommand(() => {
      return runProgressSchema();
    });
  },
});

export const progressCommand = defineCommand({
  name: "progress",
  description: "Audit log for app skill sessions",
  subCommands: {
    log: logCommand,
    schema: schemaCommand,
  },
});
