import { MetadataRoute } from "next";
import { getAllPosts } from "@/lib/blog";

const BASE_URL = process.env.NEXT_PUBLIC_SITE_URL ?? "https://meezi.ir";
const LOCALES = ["fa", "en"] as const;

const STATIC_ROUTES: {
  path: string;
  priority: number;
  changeFreq: MetadataRoute.Sitemap[number]["changeFrequency"];
}[] = [
  { path: "",               priority: 1.0, changeFreq: "weekly"  },
  { path: "/features",      priority: 0.9, changeFreq: "monthly" },
  { path: "/pricing",       priority: 0.9, changeFreq: "monthly" },
  { path: "/solutions",     priority: 0.8, changeFreq: "monthly" },
  { path: "/demo",          priority: 0.9, changeFreq: "monthly" },
  { path: "/tour",          priority: 0.7, changeFreq: "monthly" },
  { path: "/about",         priority: 0.7, changeFreq: "monthly" },
  { path: "/blog",          priority: 0.8, changeFreq: "daily"   },
  { path: "/printer-guide", priority: 0.8, changeFreq: "monthly" },
  { path: "/docs",          priority: 0.7, changeFreq: "monthly" },
  { path: "/contact",       priority: 0.6, changeFreq: "monthly" },
  { path: "/careers",       priority: 0.5, changeFreq: "monthly" },
  { path: "/privacy",       priority: 0.4, changeFreq: "yearly"  },
  { path: "/terms",         priority: 0.4, changeFreq: "yearly"  },
  { path: "/status",        priority: 0.3, changeFreq: "daily"   },
];

export default function sitemap(): MetadataRoute.Sitemap {
  const entries: MetadataRoute.Sitemap = [];
  const now = new Date();

  for (const locale of LOCALES) {
    for (const route of STATIC_ROUTES) {
      entries.push({
        url: `${BASE_URL}/${locale}${route.path}`,
        lastModified: now,
        changeFrequency: route.changeFreq,
        priority: route.priority,
      });
    }

    const posts = getAllPosts(locale);
    for (const post of posts) {
      entries.push({
        url: `${BASE_URL}/${locale}/blog/${post.slug}`,
        lastModified: new Date(post.date),
        changeFrequency: "monthly",
        priority: 0.7,
      });
    }
  }

  return entries;
}
