import { NextRequest, NextResponse } from "next/server";

// Old/removed URLs (migrated from previous site) that must return HTTP 410 Gone
// so search engines drop them from the index. Paths are stored WITHOUT a
// trailing slash; the request path is normalized the same way before lookup.
const GONE_PATHS = new Set<string>([
  "/service/clutch-replacement",
  "/service/how-much-oil-my-car-need",
  "/thank-you",
  "/faq",
  // NOTE: "/team" intentionally excluded — it is a live fruxinfo page.
  "/header/header-landing",
  "/portfolio/vintage",
  "/service/timing-belt",
  "/book-appointment",
  "/connect-with-me",
  "/portfolio/luxury",
  "/our-works",
  "/service/brake-repair",
  "/portfolio-category/engine",
  "/service/steering-repair",
  "/wishlist",
  "/header/header-default-sticky-2",
  "/service-category/car-advice",
  "/service/batteries",
  "/portfolio/premium-detailing",
  "/service/car-suspension-repair-guide",
  "/services-1",
  "/cancel-appointment",
  "/home-8-one-page",
  "/category/store",
  "/service/detailing-service",
  "/service-category/service-grid-2",
  "/service/steering",
  "/portfolio/ultra-luxury",
  "/service/battery-repair-2",
  "/service/tire-repair-2",
  "/service/engine-repair-2",
  "/service/on-top-of-car-maintenance",
  "/offers",
  "/service/cooling-system",
  "/contacts",
  "/service/diagnostic-service",
  "/tag/tire",
  "/service/maintenance-service",
  "/shop",
  "/order-confirmation",
  "/book-your-service",
  "/service/brake-service",
  "/service/car-diagnostics",
  "/home-8",
  "/header/header-three-sticky",
  "/portfolio/3-0-ceramic",
  "/header/header-landing-sticky",
  "/tag/rotation",
  "/service/repair-service",
  "/portfolio/15-off-diesel-oil-change-car",
  "/my-bookings",
  "/header/header-fifth-sticky",
  "/home-6-one-page",
  "/header/header-fourth",
  "/header/header-fourth-sticky",
  "/footer",
  "/my-account",
  "/portfolio-category/works",
  "/blog-standard-left-sidebar",
  "/header/header-default",
  "/service-category/service-grid-1",
  "/footer/footer-default",
  "/shop-ori",
  "/header/header-three",
  "/header/header-fifth",
]);

function normalize(pathname: string): string {
  // Drop a single trailing slash so "/faq" and "/faq/" both match.
  if (pathname.length > 1 && pathname.endsWith("/")) {
    return pathname.slice(0, -1);
  }
  return pathname;
}

export function middleware(request: NextRequest) {
  const path = normalize(request.nextUrl.pathname);

  if (GONE_PATHS.has(path)) {
    return new NextResponse(
      `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex">
<title>410 Gone</title>
</head>
<body>
<h1>410 Gone</h1>
<p>This page has been permanently removed and is no longer available.</p>
</body>
</html>`,
      {
        status: 410,
        headers: { "Content-Type": "text/html; charset=utf-8" },
      }
    );
  }

  return NextResponse.next();
}

export const config = {
  // Run on all paths except Next.js internals and static assets.
  matcher: ["/((?!_next/static|_next/image|favicon.ico|.*\\..*).*)"],
};
