import type { MetadataRoute } from "next";
import { getBlogPosts, getPublicProperties } from "@/lib/db-content";
import { getSiteUrl } from "@/lib/site";

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const baseUrl = getSiteUrl();
  const [properties, blogPosts] = await Promise.all([
    getPublicProperties(),
    getBlogPosts(),
  ]);
  const staticRoutes = [
    "",
    "/about",
    "/blog",
    "/contact",
    "/evaluate",
    "/faq",
    "/properties",
  ];

  return [
    ...staticRoutes.map((route) => ({
      url: `${baseUrl}${route || "/"}`,
      lastModified: new Date(),
      changeFrequency: route === "" ? ("weekly" as const) : ("monthly" as const),
      priority: route === "" ? 1 : 0.8,
    })),
    ...properties.map((property) => ({
      url: `${baseUrl}/properties/${property.id}`,
      lastModified: new Date(property.createdAt),
      changeFrequency: "weekly" as const,
      priority: 0.9,
    })),
    ...blogPosts.map((post) => ({
      url: `${baseUrl}/blog/${post.slug}`,
      lastModified: new Date(post.publishedAt),
      changeFrequency: "monthly" as const,
      priority: 0.7,
    })),
  ];
}
