import { defineConfig } from "orval";

const backendUrl =
  process.env.VITE_BACKEND_URL || 'http://localhost:8000';

// může se hodit pokud nechceme při buildu generovat klienta (nechat false pro produkci nebo vynechat)
const SKIP_ORVAL = process.env.SKIP_ORVAL === "true";

if (SKIP_ORVAL) {
  console.log("[ORVAL] Generation skipped.");
  process.exit(0);
}

export default defineConfig({
  // Public endpoints (all endpoints tagged with "public")
  public: {
    input: { 
      target: `${backendUrl}/api/schema/`,
      filters: {
        mode: "include",
        tags: ["public"],
      },
    },
    output: {
      target: "api/generated/public/index.ts",
      schemas: "api/generated/public/models",
      mode: "tags",
      clean: true,
      client: "react-query",
      httpClient: "axios",
      tsconfig: "./tsconfig.app.json",
      override: {
        mutator: {
          path: "api/publicClient.ts",
          name: "publicMutator",
        },
        formUrlEncoded: true,
        useDates: true,
      },
    },
    hooks: {
      afterAllFilesWrite: 'prettier --write',
    }
  },
  
  // All private endpoints (organized by tags, shared models folder)
  private: {
    input: { 
      target: `${backendUrl}/api/schema/`,
      filters: {
        mode: "exclude",
        tags: ["public"],  // Exclude public endpoints, include everything else
      },
    },
    output: {
      target: "api/generated/private",
      schemas: "api/generated/private/models",
      mode: "tags-split",  // Split by tag into separate files
      clean: true,
      client: "react-query",
      httpClient: "axios",
      tsconfig: "./tsconfig.app.json",
      override: {
        mutator: {
          path: "api/privateClient.ts",
          name: "privateMutator",
        },
        formUrlEncoded: true,
        useDates: true,
        query: {
          useQuery: true,
          useInfinite: false,  // Disable infinite queries to avoid page param errors
        },
      },
    },
    hooks: {
      afterAllFilesWrite: 'prettier --write',
    }
  },
});
