import type { MetadataRoute } from "next";
import { site } from "@/content/site";
import { getDb } from "@/lib/db";
import { seedIfEmpty } from "@/lib/seed";
import { listProjects } from "@/lib/models";

export default function sitemap(): MetadataRoute.Sitemap {
  const base = site.url;
  const out: MetadataRoute.Sitemap = [
    { url: `${base}/`, changeFrequency: "weekly", priority: 1.0 },
    { url: `${base}/ueber-mich`, changeFrequency: "monthly", priority: 0.7 },
    { url: `${base}/projekte`, changeFrequency: "weekly", priority: 0.9 },
    { url: `${base}/kontakt`, changeFrequency: "yearly", priority: 0.4 },
    { url: `${base}/impressum`, changeFrequency: "yearly", priority: 0.2 },
    { url: `${base}/datenschutz`, changeFrequency: "yearly", priority: 0.2 },
    { url: `${base}/en`, changeFrequency: "weekly", priority: 1.0 },
    { url: `${base}/en/about`, changeFrequency: "monthly", priority: 0.7 },
    { url: `${base}/en/projects`, changeFrequency: "weekly", priority: 0.9 },
    { url: `${base}/en/contact`, changeFrequency: "yearly", priority: 0.4 },
    { url: `${base}/en/legal`, changeFrequency: "yearly", priority: 0.2 },
    { url: `${base}/en/privacy`, changeFrequency: "yearly", priority: 0.2 },
  ];
  try {
    const db = getDb();
    seedIfEmpty(db);
    const projects = listProjects(db, "de");
    for (const p of projects) {
      out.push({ url: `${base}/projekte/${p.slug}`, changeFrequency: "monthly", priority: 0.6 });
      out.push({ url: `${base}/en/projects/${p.slug}`, changeFrequency: "monthly", priority: 0.6 });
    }
  } catch {
    // DB noch nicht initialisiert (Build ohne ENV). Sitemap bleibt statisch.
  }
  return out;
}
