import { Router } from "express";
import { prisma } from "@stamp/shared";

const router = Router();

router.get("/", async (_req, res) => {
  const articles = await prisma.article.findMany({
    where: { enabled: true },
    orderBy: { sortOrder: "asc" },
    select: { id: true, title: true, subtitle: true, coverImage: true, sortOrder: true },
  });
  res.json({ success: true, data: articles });
});

router.get("/:id", async (req, res) => {
  const article = await prisma.article.findUnique({
    where: { id: req.params.id },
    select: {
      id: true,
      title: true,
      subtitle: true,
      body: true,
      coverImage: true,
      caption: true,
      sortOrder: true,
      enabled: true,
    },
  });
  if (!article || !article.enabled) {
    res.status(404).json({ success: false, error: { code: "NOT_FOUND", message: "文章不存在" } });
    return;
  }
  const { enabled: _, ...data } = article;
  res.json({ success: true, data });
});

export default router;
