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://qiyouxueedu.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" } });
};
