import { z } from "zod";
import { defineCommand, arg } from "politty";
import { executeCommand, success, silentFailure } from "../lib/command-result";
import { runMockStart } from "./start";
import { runMockValidate } from "./validate";

const startCommand = defineCommand({
  name: "start",
  description: "Start mock API servers with reverse proxy",
  args: z.object({
    mocksRoot: arg(z.string().default("./mocks"), {
      description: "Path to mocks directory",
    }),
    port: arg(z.coerce.number().default(3000), {
      alias: "p",
      description: "Reverse proxy port",
    }),
    filter: arg(z.array(z.string()).default([]), {
      positional: true,
      description: "Filter by provider or provider/scenario",
    }),
  }),
  run: (args) => {
    return executeCommand(async () => {
      const exitCode = await runMockStart(args.mocksRoot, args.filter, args.port);
      return exitCode === 0 ? success() : silentFailure(exitCode);
    });
  },
});

const validateCommand = defineCommand({
  name: "validate",
  description: "Validate mock scenario configs",
  args: z.object({
    mocksRoot: arg(z.string().default("./mocks"), {
      description: "Path to mocks directory",
    }),
    paths: arg(z.array(z.string()).default([]), {
      positional: true,
      description: "Specific scenario paths to validate",
    }),
  }),
  run: (args) => {
    return executeCommand(async () => {
      const exitCode = await runMockValidate(args.mocksRoot, args.paths);
      return exitCode === 0 ? success() : silentFailure(exitCode);
    });
  },
});

export const mockCommand = defineCommand({
  name: "mock",
  description: "Mock API server management",
  subCommands: {
    start: startCommand,
    validate: validateCommand,
  },
});
