import type { MetadataRoute } from "next";
import { articlesFallback } from "@/data/articles";
import { productsList, solutionsList } from "@/data/pages";
import { portfoliosFallback } from "@/data/portfolios";
import { SITE_URL } from "@/lib/seo";

const staticRoutes: Array<{
  path: string;
  changeFrequency: MetadataRoute.Sitemap[number]["changeFrequency"];
  priority: number;
}> = [
  { path: "/", changeFrequency: "weekly", priority: 1 },
  { path: "/products", changeFrequency: "weekly", priority: 0.9 },
  { path: "/solutions", changeFrequency: "weekly", priority: 0.9 },
  { path: "/portfolios", changeFrequency: "weekly", priority: 0.8 },
  { path: "/articles", changeFrequency: "weekly", priority: 0.8 },
  { path: "/documents", changeFrequency: "monthly", priority: 0.7 },
  { path: "/catalog", changeFrequency: "monthly", priority: 0.7 },
  { path: "/about", changeFrequency: "monthly", priority: 0.7 },
  { path: "/contact", changeFrequency: "monthly", priority: 0.7 },
  { path: "/ims-policy", changeFrequency: "yearly", priority: 0.5 },
];

export default function sitemap(): MetadataRoute.Sitemap {
  const lastModified = new Date();

  const pages = staticRoutes.map((route) => ({
    url: `${SITE_URL}${route.path === "/" ? "" : route.path}`,
    lastModified,
    changeFrequency: route.changeFrequency,
    priority: route.priority,
  }));

  const products = productsList.map((item) => ({
    url: `${SITE_URL}${item.href}`,
    lastModified,
    changeFrequency: "monthly" as const,
    priority: 0.8,
  }));

  const solutions = solutionsList.map((item) => ({
    url: `${SITE_URL}${item.href}`,
    lastModified,
    changeFrequency: "monthly" as const,
    priority: 0.75,
  }));

  const articles = articlesFallback.map((item) => ({
    url: `${SITE_URL}/articles/${item.slug}`,
    lastModified: item.date ? new Date(item.date) : lastModified,
    changeFrequency: "monthly" as const,
    priority: 0.6,
  }));

  const portfolios = portfoliosFallback.map((item) => ({
    url: `${SITE_URL}/portfolios/${item.slug}`,
    lastModified,
    changeFrequency: "monthly" as const,
    priority: 0.6,
  }));

  return [...pages, ...products, ...solutions, ...articles, ...portfolios];
}
