import type { ParsedTransition } from "./parse-model-doc";

export function generateLifecycle(modelName: string, transitions: ParsedTransition[]): string {
  const camelName = modelName.charAt(0).toLowerCase() + modelName.slice(1);
  const stateType = `${modelName}Status`;

  const transitionsObj = transitions
    .map((t) => {
      const fromArray = t.from.map((f) => `"${f}"`).join(", ");
      return `    ${t.operation}: { from: [${fromArray}], to: "${t.to}" },`;
    })
    .join("\n");

  return `// @generated — do not edit
import { defineLifecycle } from "@tailor-platform/erp-kit/core";

export const ${camelName}Lifecycle = defineLifecycle({
${transitionsObj}
});

export type ${stateType} = typeof ${camelName}Lifecycle.states[number];
`;
}
