import { readFileSync } from "node:fs";
import { defineConfig } from "astro/config";

const siteConfig = JSON.parse(
  readFileSync(new URL("./src/config/site.json", import.meta.url)),
);
import mdx from "@astrojs/mdx";
import react from "@astrojs/react";
import tailwindcss from "@tailwindcss/vite";

const site =
  process.env.SITE_URL || process.env.PUBLIC_SITE_URL || siteConfig.url;

export default defineConfig({
  site,
  // astroagent overrides outDir for isolated preview builds (PREVIEW_OUT);
  // falls back to the live output for normal/cron builds.
  outDir: process.env.PREVIEW_OUT || "../public",
  // For previews, PREVIEW_BASE prefixes asset paths (/_preview/<id>) so the
  // preview is self-contained and doesn't pull _astro/fonts from the live site.
  base: process.env.PREVIEW_BASE || undefined,
  integrations: [mdx(), react()],
  vite: {
    plugins: [tailwindcss()],
    resolve: {
      alias: {
        // shadcn / 21st.dev convention — paste components in unmodified.
        "@": new URL("./src", import.meta.url).pathname,
        // Those components import useTheme from "next-themes", which is a
        // Next.js package. Point it at a local shim that drives the existing
        // class-based theme instead. See src/lib/next-themes.ts.
        "next-themes": new URL("./src/lib/next-themes.ts", import.meta.url).pathname,
      },
    },
    build: {
      emptyOutDir: false,
    },
  },
});