import type { MetadataRoute } from "next";

import { getBlogPosts } from "@/lib/blog";
import { blogHref, pressHref } from "@/lib/blog-shared";
import { getPressPosts } from "@/lib/press";
import { allLocalizedUrls, absoluteUrl } from "@/lib/seo";
import { locales } from "@/i18n/config";

export default function sitemap(): MetadataRoute.Sitemap {
  const now = new Date();
  const pages = allLocalizedUrls().map(({ path, routeId }) => ({
    url: absoluteUrl(path),
    lastModified: now,
    changeFrequency: (routeId === "home" || routeId === "blog" || routeId === "press"
      ? "weekly"
      : "monthly") as "weekly" | "monthly",
    priority: routeId === "home" ? 1 : routeId === "blog" || routeId === "press" ? 0.8 : 0.7,
  }));

  const posts = getBlogPosts().flatMap((post) =>
    locales.map((locale) => ({
      url: absoluteUrl(blogHref(locale, post.slug)),
      lastModified: new Date(post.date),
      changeFrequency: "monthly" as const,
      priority: 0.6,
    })),
  );

  const press = getPressPosts().flatMap((post) =>
    locales.map((locale) => ({
      url: absoluteUrl(pressHref(locale, post.slug)),
      lastModified: new Date(post.date),
      changeFrequency: "monthly" as const,
      priority: 0.6,
    })),
  );

  return [...pages, ...posts, ...press];
}
