import type { MetadataRoute } from "next";
import { extractDomain } from "../lib/utils/domain";

// Per-tenant robots: built from the request's own domain so each franchisee
// site points crawlers at its own sitemap.
export const dynamic = "force-dynamic";

export default async function robots(): Promise<MetadataRoute.Robots> {
  let domain = "web.youngengineers.org";
  try {
    domain = await extractDomain();
  } catch {
    // Headers unavailable (e.g. build) — fall back to the default host.
  }

  const baseUrl = `https://${domain}`;

  return {
    rules: {
      userAgent: "*",
      allow: "/",
      // Transactional / utility routes have no SEO value.
      disallow: ["/pay/", "/thank-you/", "/form-submission/", "/api/"],
    },
    sitemap: `${baseUrl}/sitemap.xml`,
    host: baseUrl,
  };
}
