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-items
router.get("/", async (_req, res) => {
  try {
    const [rows] = await db.execute<RowDataPacket[]>(
      "SELECT * FROM game_items ORDER BY type, name"
    );
    const parsed = rows.map((item) => ({
      ...item,
      effects: parseJson(item.effects),
      properties: parseJson(item.properties),
    }));
    res.json(parsed);
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: "Internal server error" });
  }
});

export default router;
