import type { MetadataRoute } from "next";
import { prisma } from "@/lib/prisma";

const staticPaths = [
  "",
  "/o-clube",
  "/historia",
  "/elenco",
  "/noticias",
  "/socio-torcedor",
  "/socio-torcedor/cadastro",
  "/jogos",
  "/galeria",
  "/documentos",
  "/contato",
  "/parceiros",
];

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const base = process.env.NEXT_PUBLIC_SITE_URL || "https://www.fluminensedefeira.com.br";
  const entries: MetadataRoute.Sitemap = staticPaths.map((p) => ({
    url: `${base}${p || "/"}`,
    lastModified: new Date(),
    changeFrequency: p === "" ? ("daily" as const) : ("weekly" as const),
    priority: p === "" ? 1 : 0.8,
  }));

  try {
    const news = await prisma.news.findMany({
      where: { published: true },
      select: { slug: true, updatedAt: true },
    });
    for (const n of news) {
      entries.push({
        url: `${base}/noticias/${n.slug}`,
        lastModified: n.updatedAt,
        changeFrequency: "weekly",
        priority: 0.7,
      });
    }
  } catch {
    /* sem DB em build */
  }

  return entries;
}
