import fs from "node:fs/promises";
import path from "node:path";
import { pathToFileURL } from "node:url";
import openapiTS, { astToString } from "openapi-typescript";

const rootDir = path.resolve(import.meta.dirname, "../../..");
const specPath = path.resolve(rootDir, "apps/backend/openapi/bookra.openapi.yaml");
const outputPath = path.resolve(import.meta.dirname, "../src/generated/types.ts");

const ast = await openapiTS(pathToFileURL(specPath), {
  alphabetize: true,
});
const schema = astToString(ast);

await fs.mkdir(path.dirname(outputPath), { recursive: true });
await fs.writeFile(outputPath, `${schema}\n`, "utf8");
console.log(`Generated API types to ${outputPath}`);
