/**
 * Next.js config for the app-basic test fixture.
 *
 * This config serves both Vitest integration tests (app-router.test.ts)
 * and Playwright E2E tests (ON-12, ON-13, ON-15).
 *
 * IMPORTANT: The Vitest test previously wrote a temporary next.config.mjs.
 * Since next.config.ts takes priority over .mjs, this file must include
 * all redirects/rewrites/headers that those tests expect.
 */
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  // Default is false — trailing slashes are stripped (redirects /about/ → /about)
  // trailingSlash: false,

  async redirects() {
    return [
      // Used by Vitest: app-router.test.ts
      {
        source: "/old-about",
        destination: "/about",
        permanent: true,
      },
      // Used by Vitest: app-router.test.ts (permanent: false → 307)
      {
        source: "/old-blog/:slug",
        destination: "/blog/:slug",
        permanent: false,
      },
      // Used by E2E: config-redirect.spec.ts (permanent → 308)
      {
        source: "/config-redirect-source",
        destination: "/about",
        permanent: true,
      },
      // Used by E2E: config-redirect.spec.ts (non-permanent → 307)
      {
        source: "/config-redirect-query",
        destination: "/about?from=config",
        permanent: false,
      },
      // Used by E2E: config-redirect.spec.ts — has cookie condition
      {
        source: "/has-cookie-redirect",
        destination: "/about",
        permanent: false,
        has: [{ type: "cookie", key: "redirect-me" }],
      },
      // Used by E2E: config-redirect.spec.ts — missing cookie condition
      {
        source: "/missing-cookie-redirect",
        destination: "/about",
        permanent: false,
        missing: [{ type: "cookie", key: "stay-here" }],
      },
    ];
  },

  async rewrites() {
    return {
      beforeFiles: [
        // Used by Vitest: app-router.test.ts
        { source: "/rewrite-about", destination: "/about" },
      ],
      afterFiles: [
        // Used by Vitest: app-router.test.ts
        { source: "/after-rewrite-about", destination: "/about" },
        // Used by E2E: config-redirect.spec.ts
        { source: "/config-rewrite", destination: "/" },
      ],
      fallback: [],
    };
  },

  async headers() {
    return [
      // Used by Vitest: app-router.test.ts
      {
        source: "/api/(.*)",
        headers: [{ key: "X-Custom-Header", value: "vinext-app" }],
      },
      // Used by Vitest: app-router.test.ts
      {
        source: "/about",
        headers: [{ key: "X-Page-Header", value: "about-page" }],
      },
      // Used by E2E: config-redirect.spec.ts (catch-all for e2e header test)
      {
        source: "/(.*)",
        headers: [{ key: "X-E2E-Header", value: "vinext-e2e" }],
      },
    ];
  },
};

export default nextConfig;
