import type { AuthConfig } from "@wrnexus/auth";
import type { AppConfig } from "@wrnexus/styles";
import { auth } from "./app/lib/auth.ts";

const config = {
  seo: {
    title: "WRNexus Auth Showcase",
    description: "Complete authentication, MFA, passkey, recovery, session, and security examples.",
    canonicalBase: "http://localhost:3000",
    robots: "noindex,nofollow",
  },
  theme: { palette: "amber", default: "dark" },
  auth: {
    engine: auth,
    migrations: false,
    routes: true,
    middleware: true,
    components: true,
    baseUrl: "http://localhost:3000",
  },
  styles: {
    entry: "app/styles/global.css",

    process: async ({ entryPath, appRoot, mode }) => {
      if (!entryPath) {
        throw new Error("Auth showcase stylesheet entry was not resolved.");
      }

      const args = ["@tailwindcss/cli", "-i", entryPath];

      if (mode === "production") {
        args.push("--minify");
      }

      const result = await Bun.$.cwd(appRoot)`bunx ${args}`.nothrow().quiet();

      const css = result.stdout.toString();

      const diagnostics = result.stderr.toString().trim();

      if (result.exitCode !== 0) {
        throw new Error(diagnostics || css || "Tailwind CSS processing failed.");
      }

      const doneLine = diagnostics
        .split(/\r?\n/)
        .map((line) => line.trim())
        .filter(Boolean)
        .reverse()
        .find((line) => /^done in\b/i.test(line));

      console.log(doneLine ? `  ✓ Tailwind CSS: ${doneLine}` : "  ✓ Tailwind CSS: compiled");

      return css;
    },

    failureMode: "throw",
  },
} satisfies AppConfig & { auth: AuthConfig };

export default config;
