import { NextResponse } from "next/server";
import crypto from "crypto";

const BASE_URL = "https://llmhosts.com";

const PAGES = [
  {
    id: "550e8400-e29b-41d4-a716-446655440001",
    slug: "",
    title: "LLMHosts — Your GPU, Real AI Infrastructure, From Anywhere",
    description:
      "Turn your home GPU into production AI infrastructure. OpenAI + Anthropic compatible proxy with intelligent routing, verified cache, and remote access.",
    url: `${BASE_URL}/`,
    type: "landing",
    datePublished: "2026-01-15",
    dateModified: "2026-02-22",
  },
  {
    id: "550e8400-e29b-41d4-a716-446655440002",
    slug: "pricing",
    title: "LLMHosts Pricing — Free, Pro, and Team Plans",
    description:
      "Compare LLMHosts plans. Free tier for personal use, Pro at $29/mo with priority routing, Team at $99/mo with GPU pooling.",
    url: `${BASE_URL}/pricing`,
    type: "pricing",
    datePublished: "2026-01-15",
    dateModified: "2026-02-22",
  },
  {
    id: "550e8400-e29b-41d4-a716-446655440003",
    slug: "docs",
    title: "LLMHosts Documentation — Setup, API, and Configuration",
    description:
      "Get started with LLMHosts. Installation, CLI commands, API reference, router configuration, and deployment guides.",
    url: `${BASE_URL}/docs`,
    type: "documentation",
    datePublished: "2026-01-15",
    dateModified: "2026-02-22",
  },
  {
    id: "550e8400-e29b-41d4-a716-446655440004",
    slug: "contact",
    title: "Contact LLMHosts — Support and Partnerships",
    description:
      "Get in touch with the LLMHosts team for support, partnerships, enterprise inquiries, or feedback.",
    url: `${BASE_URL}/contact`,
    type: "contact",
    datePublished: "2026-01-15",
    dateModified: "2026-02-22",
  },
  {
    id: "550e8400-e29b-41d4-a716-446655440005",
    slug: "legal/privacy",
    title: "Privacy Policy — LLMHosts",
    description: "LLMHosts privacy policy covering data collection, usage, and protection.",
    url: `${BASE_URL}/legal/privacy`,
    type: "legal",
    datePublished: "2026-01-15",
    dateModified: "2026-02-22",
  },
  {
    id: "550e8400-e29b-41d4-a716-446655440006",
    slug: "legal/terms",
    title: "Terms of Service — LLMHosts",
    description: "LLMHosts terms of service governing use of the platform and CLI tool.",
    url: `${BASE_URL}/legal/terms`,
    type: "legal",
    datePublished: "2026-01-15",
    dateModified: "2026-02-22",
  },
];

export async function GET(request: Request) {
  const { searchParams } = new URL(request.url);
  const updatedSince = searchParams.get("updated_since");

  let filtered = PAGES;
  if (updatedSince) {
    const since = new Date(updatedSince);
    filtered = PAGES.filter((p) => new Date(p.dateModified) >= since);
  }

  const body = JSON.stringify({
    data: filtered,
    meta: {
      total: filtered.length,
      generated: new Date().toISOString(),
    },
  });

  const etag = `"${crypto.createHash("md5").update(body).digest("hex")}"`;
  const ifNoneMatch = request.headers.get("if-none-match");
  if (ifNoneMatch === etag) {
    return new Response(null, { status: 304 });
  }

  return new NextResponse(body, {
    headers: {
      "Content-Type": "application/json",
      ETag: etag,
      "Last-Modified": new Date().toUTCString(),
      "Cache-Control": "public, max-age=3600, s-maxage=3600",
      "X-Robots-Tag": "noindex",
    },
  });
}
