import path from "node:path";
import type { CommandResult } from "../../../lib/command-result";
import { runGenerateDoc } from "../../../lib/generate-doc";
import { MODULE_PATHS } from "../../../lib/paths";
import { MODULE_SCHEMAS } from "../../schemas";

export const MODULE_DOC_TYPES = ["feature", "command", "model", "query"] as const;

export type ModuleDocType = (typeof MODULE_DOC_TYPES)[number];

const DIR_MAP: Record<ModuleDocType, string> = {
  feature: MODULE_PATHS.docs.feature,
  command: MODULE_PATHS.docs.command,
  model: MODULE_PATHS.docs.model,
  query: MODULE_PATHS.docs.query,
};

export function resolveModuleDocPath(
  type: ModuleDocType,
  name: string | undefined,
  modulePath: string,
): string {
  if (!name) {
    throw new Error(`Name is required for doc type "${type}"`);
  }
  return path.join(modulePath, DIR_MAP[type], `${name}.md`);
}

export function runModuleGenerateDoc(
  type: ModuleDocType,
  name: string | undefined,
  modulePath: string,
  cwd: string,
): Promise<CommandResult> {
  return runGenerateDoc(resolveModuleDocPath(type, name, modulePath), MODULE_SCHEMAS[type], cwd);
}
