import { defineConfig } from "@hey-api/openapi-ts";
import { config } from "dotenv";
import { existsSync } from "node:fs";
import { resolve } from "node:path";

const envPath = resolve(process.cwd(), ".env");
if (existsSync(envPath)) {
  config({ path: envPath });
}

const envLocalPath = resolve(process.cwd(), ".env.local");
if (existsSync(envLocalPath)) {
  config({ path: envLocalPath });
}

const apiBaseUrl =
  process.env.API_BASE_URL ||
  process.env.NEXT_PUBLIC_API_BASE_URL ||
  "http://localhost:8081";

const openApiUrl =
  process.env.OPENAPI_URL ||
  process.env.REACT_APP_OPENAPI_URL ||
  `${apiBaseUrl.replace(/\/$/, "")}/brapi/v2/openapi.json`;

export default defineConfig({
  client: "@hey-api/client-fetch",
  input: openApiUrl,
  output: "./src/lib/api",
  schemas: {
    name: "types.gen.ts"
  },
  services: {
    name: "sdk.gen.ts"
  },
  clientName: "client.gen.ts"
});
