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

const router = Router();

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

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

export default router;
