import { Router } from "express";
import db from "../db.js";

const router = Router({ mergeParams: true });

router.get("/", (req, res) => {
  const { campaignId } = req.params;
  const rolls = db
    .prepare(
      "SELECT * FROM roll_log WHERE campaign_id = ? ORDER BY created_at DESC LIMIT 50",
    )
    .all(campaignId) as Array<Record<string, unknown>>;

  const parsed = rolls.map((r) => ({
    ...r,
    rolls: JSON.parse(r.rolls as string),
    advantage: r.advantage === 1,
    disadvantage: r.disadvantage === 1,
    nat20: r.nat20 === 1,
  }));

  res.json(parsed);
});

export default router;
