import type { MetadataRoute } from "next";
import { site } from "@/content/site";
import { BLOG_POSTS } from "@/content/blog";
import { BLOG_CATEGORIES_LIST } from "@/lib/blog-categories-data";
import { AUTHORS_DATA } from "@/lib/authors-data";
import { industries } from "@/content/industries";
import { blogService } from "@/lib/blog-service";

const BASE_URL = site.url;

// Official Next.js App Router sitemap convention (app/sitemap.ts → /sitemap.xml).
// Only public, indexable routes are listed.
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const now = new Date();

  const staticRoutes: {
    path: string;
    priority: number;
    changeFrequency: MetadataRoute.Sitemap[number]["changeFrequency"];
  }[] = [
    { path: "", priority: 1.0, changeFrequency: "daily" },
    { path: "/voice-ai", priority: 0.9, changeFrequency: "monthly" },
    { path: "/whatsapp-ai", priority: 0.9, changeFrequency: "monthly" },
    { path: "/about", priority: 0.8, changeFrequency: "monthly" },
    { path: "/industries", priority: 0.9, changeFrequency: "weekly" },
    { path: "/blog", priority: 0.9, changeFrequency: "daily" },
    { path: "/author", priority: 0.5, changeFrequency: "weekly" },
    { path: "/contact", priority: 0.7, changeFrequency: "monthly" },
    { path: "/privacy", priority: 0.3, changeFrequency: "yearly" },
    { path: "/terms", priority: 0.3, changeFrequency: "yearly" },
    { path: "/disclaimer", priority: 0.3, changeFrequency: "yearly" },
  ];

  const staticEntries: MetadataRoute.Sitemap = staticRoutes.map((r) => ({
    url: `${BASE_URL}${r.path}`,
    lastModified: now,
    changeFrequency: r.changeFrequency,
    priority: r.priority,
  }));

  // Only canonical industry slugs
  const industryEntries: MetadataRoute.Sitemap = industries.map((industry) => ({
    url: `${BASE_URL}/industries/${industry.slug}`,
    lastModified: now,
    changeFrequency: "monthly",
    priority: 0.7,
  }));

  // Authoritative blog posts from MongoDB (Strictly only PUBLISHED posts)
  let publishedList: { slug: string; publishedDate: string }[] = [];
  try {
    const mongoPosts = await blogService.getPublishedPosts({ limit: 1000 });
    if (mongoPosts && mongoPosts.length > 0) {
      publishedList = mongoPosts.map((p) => ({
        slug: p.slug,
        publishedDate: p.publishedDate,
      }));
    } else {
      publishedList = BLOG_POSTS.map((p) => ({
        slug: p.slug,
        publishedDate: p.publishedDate,
      }));
    }
  } catch {
    publishedList = BLOG_POSTS.map((p) => ({
      slug: p.slug,
      publishedDate: p.publishedDate,
    }));
  }

  const blogEntries: MetadataRoute.Sitemap = publishedList.map((post) => ({
    url: `${BASE_URL}/blog/${post.slug}`,
    lastModified: post.publishedDate ? new Date(post.publishedDate) : now,
    changeFrequency: "weekly",
    priority: 0.7,
  }));

  // Category pages are only meaningful once posts exist — otherwise they are
  // thin/empty listings and must not be advertised as indexable in the sitemap.
  const categoryEntries: MetadataRoute.Sitemap =
    publishedList.length > 0
      ? BLOG_CATEGORIES_LIST.map((cat) => ({
          url: `${BASE_URL}/blog/category/${cat.slug}`,
          lastModified: now,
          changeFrequency: "weekly" as const,
          priority: 0.6,
        }))
      : [];

  const authorEntries: MetadataRoute.Sitemap = AUTHORS_DATA.map((author) => ({
    url: `${BASE_URL}/author/${author.slug}`,
    lastModified: now,
    changeFrequency: "monthly",
    priority: 0.5,
  }));

  return [
    ...staticEntries,
    ...industryEntries,
    ...blogEntries,
    ...categoryEntries,
    ...authorEntries,
  ];
}
