import { Router } from "express";
import type { RowDataPacket } from "mysql2";
import db from "../db.js";
import { parseJson } from "../utils/parseJson.js";

const router = Router();

// GET /api/game-talents
router.get("/", async (_req, res) => {
  try {
    const [rows] = await db.execute<RowDataPacket[]>(
      "SELECT * FROM game_talents ORDER BY source, name"
    );
    const parsed = rows.map((t) => ({
      ...t,
      effect: parseJson(t.effect),
    }));
    res.json(parsed);
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: "Internal server error" });
  }
});

export default router;
