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.englishName}）完整站点内容`,
    "",
    `> ${site.brand.tagline}`,
    "",
    `官方网站：${url("/")}`,
    `企业主体：${site.brand.legalName}`,
    `客服电话：${site.contact.phone}`,
    `公司地址：${site.contact.address}`,
    "",
    "## 品牌与服务概览",
    "",
    site.home.subhead,
    "",
    ...site.about.story,
    "",
    "## 核心数据",
    "",
    ...site.home.stats.map((stat) => `- ${stat.label}：${stat.value}`),
    "",
    "## 产品与定制",
    "",
    `产品总览：${url("/services/")}`,
    "",
    ...site.products.flatMap((product) => [
      `### ${product.name}`,
      "",
      `页面：${url(`/services/${product.slug}/`)}`,
      "",
      product.intro,
      "",
      "特点：",
      ...product.features.map((feature) => `- ${feature}`),
      "",
      "规格与工艺：",
      ...product.specifications.map((spec) => `- ${spec.label}：${spec.value}`),
      "",
    ]),
    "## 服务承诺",
    "",
    ...site.promises.map((promise) => `- ${promise.title}：${promise.desc}`),
    "",
    "## 常见问题",
    "",
    `常见问题页面：${url("/faq/")}`,
    "",
    ...site.faq.flatMap((item) => [`### ${item.q}`, "", item.a, ""]),
    "## 银饰资讯全文",
    "",
    ...articles.flatMap((article) => [
      `### ${article.data.title}`,
      "",
      `页面：${url(`/articles/${article.id}/`)}`,
      `分类：${article.data.category}`,
      `作者：${article.data.author}`,
      `发布日期：${article.data.date.toISOString().slice(0, 10)}`,
      `更新时间：${article.data.updated.toISOString().slice(0, 10)}`,
      "",
      article.data.excerpt,
      "",
      article.body,
      "",
    ]),
    "## 联系方式",
    "",
    `联系页面：${url("/contact/")}`,
    `客服电话：${site.contact.phone}`,
    `公司地址：${site.contact.address}`,
    `抖音账号：${site.contact.douyin.join("、")}`,
    "",
  ];

  return new Response(lines.join("\n"), {
    headers: {
      "Content-Type": "text/plain; charset=utf-8",
      "Cache-Control": "public, max-age=3600",
    },
  });
};
