/**
 * /llms.txt — خلاصه ساختاریافته سایت برای مدل‌های زبانی و موتورهای پاسخ هوش مصنوعی (GEO).
 * قالب پیشنهادی llmstxt.org: معرفی کوتاه، سپس فهرست لینک‌های مهم با توضیح یک‌خطی.
 */
import { formatToman } from "@/lib/format";
import { site } from "@/lib/site";
import { getBestsellers, getBrands, getCategoryIndex } from "@/server/services/catalog";
import { getShippingSettings } from "@/server/services/settings";
import { getAllArticleRefs } from "@/server/services/magazine";

export const revalidate = 3600;

export async function GET() {
  const [cats, brands, best, shipping, articles] = await Promise.all([getCategoryIndex(), getBrands(), getBestsellers(15), getShippingSettings(), getAllArticleRefs()]);
  const abs = (p: string) => new URL(encodeURI(p), site.url).toString();
  const lines = [
    `# ${site.name} (${site.nameEn})`,
    "",
    `> ${site.description}`,
    "",
    "فروشگاه آنلاین ایرانی محصولات آرایشی، مراقبت پوست، مو، عطر و بدن. قیمت‌ها به تومان است. همه محصولات اصل و با ضمانت اصالت کالا هستند.",
    `ارسال به سراسر ایران با ${shipping.methods.map((m) => m.name).join("، ")}؛ ارسال رایگان برای خریدهای بالای ${formatToman(shipping.freeShippingThreshold)}.`,
    `پشتیبانی: ${site.phone} (${site.supportHours})`,
    "",
    "## دسته‌بندی‌ها",
    ...cats.all.map((c) => `- [${c.name}](${abs(cats.href(c.id))})${c.description ? `: ${c.description}` : ""}`),
    "",
    "## برندها",
    ...brands.map((b) => `- [${b.name} (${b.nameEn})](${abs(`/brand/${b.slug}`)}): ${b.description}`),
    "",
    "## پرفروش‌ترین محصولات",
    ...best.map((p) => `- [${p.name} — ${p.brand.name}](${abs(`/p/${p.id}/${p.slug}`)}): از ${formatToman(p.price)}، امتیاز ${p.rating.toFixed(1)} از ۵`),
    ...(articles.length ? ["", "## مجله زیبایی", ...articles.slice(0, 30).map((a) => `- [${a.title}](${abs(`/mag/${a.slug}`)}): ${a.excerpt}`)] : []),
    "",
    "## صفحات راهنما",
    `- [پیگیری سفارش](${abs("/track")})`,
    `- [قوانین و شرایط](${abs("/terms")})`,
    `- [حریم خصوصی](${abs("/privacy")})`,
    `- [تماس با ما](${abs("/contact")})`,
    "",
    "## Optional",
    `- [نقشه کامل سایت](${abs("/sitemap.xml")})`,
  ];
  return new Response(lines.join("\n"), { headers: { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "public, max-age=3600" } });
}
