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

export const prerender = false;

export const GET: APIRoute = async ({ site: configuredSite }) => {
  const articles = await getPublishedArticles();
  const origin = configuredSite ?? new URL("https://www.zhiyintec.com");
  const url = (pathname: string) => new URL(pathname, origin).href;
  const lines = [
    `# ${site.brand.name}`,
    "",
    `> ${site.brand.tagline}`,
    "",
    site.about.position,
    "",
    "## 核心页面",
    `- [首页](${url("/")})`,
    `- [产品服务](${url("/services/")})`,
    ...site.services.map((service) => `- [${service.name}](${url(`/services/${service.slug}/`)}): ${service.seoDescription}`),
    `- [关于智引未来](${url("/about/")})`,
    `- [服务方法](${url("/methodology/")})`,
    `- [洞察中心](${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.email}`, `公司地址：${site.contact.address}`,
  ];
  return new Response(lines.join("\n"), { headers: { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "public, max-age=3600" } });
};
