import type { APIRoute } from "astro";
import { site } from "../data/site";
import { getPublishedArticles } from "../lib/articles";

export const prerender = true;

export const GET: APIRoute = async ({ site: configuredSite }) => {
  const articles = await getPublishedArticles();
  const origin = configuredSite ?? new URL("https://www.inzung.cn");
  const url = (pathname: string) => new URL(pathname, origin).href;
  const lines = [
    `# ${site.brand.name}`,
    "",
    `> ${site.brand.tagline}`,
    "",
    `银妆是拥有 14 年行业积淀的银饰连锁品牌，提供足银首饰零售、来料加工、专属雕刻与银饰 DIY 编织服务。`,
    "",
    "## 核心页面",
    `- [首页](${url("/")})`,
    `- [产品与定制](${url("/services/")})`,
    ...site.products.map((product) => `- [${product.name}](${url(`/services/${product.slug}/`)}): ${product.seoDescription}`),
    `- [关于银妆](${url("/about/")})`,
    `- [门店与服务](${url("/join/")})`,
    `- [银饰资讯](${url("/articles/")})`,
    `- [常见问题](${url("/faq/")})`,
    `- [联系我们](${url("/contact/")})`,
    "",
    "## 银饰资讯",
    ...articles.map((article) => `- [${article.data.title}](${url(`/articles/${article.id}/`)}): ${article.data.excerpt}`),
    "",
    `- [完整站点内容](${url("/llms-full.txt")})`,
    "", `客服电话：${site.contact.phone}`, `公司地址：${site.contact.address}`,
  ];
  return new Response(lines.join("\n"), { headers: { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "public, max-age=3600" } });
};
