const express = require("express");
const requireAdmin = require("../../middleware/requireAdmin")
const router = express.Router();
router.use(requireAdmin);
const Class = require("../../models/Class");

router.post("/classes", requireAdmin, async (req, res) => {
  try {
    const { name, slug, batchId } = req.body;

    const newClass = await Class.create({
      name,
      slug,
      batchId
    });

    res.status(201).json(newClass);
  } catch (error) {
    res.status(500).json({
      message: "Failed to create class",
      error: error.message
    });
  }
});

router.get("/classes/:batchId", requireAdmin, async (req, res) => {
  try {
    const classes = await Class.find({
      batchId: req.params.batchId
    });

    res.json(classes);
  } catch (error) {
    res.status(500).json({
      message: "Failed to fetch classes",
      error: error.message
    });
  }
});

module.exports = router;
