import type { AppConfig } from "@wrnexus/styles";

const config: AppConfig = {
  compatibilityDate: "2026-08-02",
  frameworkBehaviour: 1,
  // v0.8 defaults: explicit imports, strict template types, safe stores, and
  // automatic progressive navigation. Package plugins are discovered from the
  // installed packages above; add custom plugins to this array when needed.
  plugins: [],
  imports: { mode: "explicit", autoImport: true, aliases: { "@": "./app" } },
  types: {
    strict: true,
    noImplicitAny: true,
    strictNullChecks: true,
    checkTemplates: true,
    checkComponentProps: true,
    generateDeclarations: true,
  },
  functions: { legacyDefaultRuntime: "current" },
  stores: { strictMutations: true, persistence: true },
  compatibility: {
    legacyEmit: false,
    legacyEventProps: false,
    legacyComponentDiscovery: false,
    stringLayouts: false,
  },
  experimental: {},

  performance: {
    enforcement: "warn",
    analyze: true,
    budgets: {
      routeJsBytes: 50 * 1024,
      routeCssBytes: 25 * 1024,
      lcpMs: 2_500,
      inpMs: 200,
      cls: 0.1,
    },
  },
  observability: {
    enabled: true,
    serviceName: "web",
    serverTiming: true,
    sampleRate: 1,
    exporter: process.env.OTEL_EXPORTER_OTLP_ENDPOINT ? "otlp" : "none",
    endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
    webVitals: true,
  },
  tenancy: { mode: "domain", required: false, rootDomains: ["localhost"] },
  build: { cache: true, sourceMaps: true, report: true, adapter: "bun" },
  navigation: { mode: "auto" },
  devToolbar: { enabled: true, position: "bottom-center", openEditor: true },

  mobile: {
    enabled: true,
    appId: "com.example.web",
    appName: "web",
    userAgent: "WrNexusMobile",
    backgroundColor: "#0f172a",
    // layout: "mobile", // app/layouts/mobile.wrn
    // icon: "resources/icon.png",
  },

  // PWA support is enabled automatically. Override any install metadata here.
  pwa: {
    name: "web",
    shortName: "web",
    display: "standalone",
    themeColor: "#6366f1",
    backgroundColor: "#0f172a",
  },

  seo: {
    title: "web",
    titleTemplate: "%s | web",
    // Set WRNEXUS_PUBLIC_ORIGIN in production when TLS terminates at a proxy.
    canonicalBase: process.env.WRNEXUS_PUBLIC_ORIGIN,
    description: "An SSR-first WrNexus app.",
    robots: "index,follow",
    themeColor: "#6366f1",
  },

  styles: {
    entry: "app/styles/global.css",

    // Tailwind v4 build. Runs once at dev-serve time (cached; re-run on restart)
    // and at `wrnexus build`. `@tailwindcss/cli` writes to stdout, so we capture
    // and return the final CSS. Delete this hook to drop Tailwind — global.css is
    // still bundled and served as-is.
    process: async ({ entryPath, appRoot, mode }) => {
      const args = ["@tailwindcss/cli", "-i", entryPath!];
      if (mode === "production") args.push("--minify");
      return await Bun.$.cwd(appRoot)`bunx ${args}`.text();
    },
  },

  // Fonts — optimized preconnect, subsetted weights, font-display, and CSP.
  fonts: {
    sans: '"Plus Jakarta Sans", ui-sans-serif, system-ui, sans-serif',
    google: [{ family: "Plus Jakarta Sans", weights: [400, 500, 600, 700] }],
  },
  //
  //   // Or self-host (fastest, no third party) — drop files in public/fonts/:
  //   // local: [{ family: "Inter", src: "/fonts/inter.woff2", weight: "100 900", preload: true }],

  theme: { palette: "violet", default: "light" },
  i18n: { default: "en", locales: ["en"] },
  db: { driver: "sqlite", url: process.env.DATABASE_URL ?? "file:./dev.db" },
  databases: {},
  storage: {
    default: "public",
    stores: {
      public: {
        driver: "local",
        access: "public",
        dir: "uploads/public",
        maxBytes: 10_000_000,
        accept: ["image/*", "application/pdf"],
      },
      private: {
        driver: "local",
        access: "private",
        dir: "uploads/private",
        maxBytes: 10_000_000,
      },
    },
  },
  realtime: { scale: Boolean(process.env.REDIS_URL), redisUrl: process.env.REDIS_URL },
  port: Number(process.env.PORT ?? 3000),
  security: {
    cors: { enabled: false },
  },
  profiles: {
    development: {},
    test: {
      db: { driver: "sqlite", url: "file:./test.db" },
      observability: { exporter: "none", sampleRate: 0 },
    },
    staging: {
      seo: { robots: "noindex,nofollow" },
      performance: { enforcement: "error" },
      build: { sourceMaps: true, report: true },
    },
    production: {
      seo: { canonicalBase: process.env.WRNEXUS_PUBLIC_ORIGIN },
      performance: { enforcement: "error" },
      build: { sourceMaps: false, report: true },
      devToolbar: false,
    },
  },
};

export default config;
