import { Router } from "express";
import { requireAccess } from "../middleware/requireAccess";

const router = Router();

// CREATE (uses payload values)
router.post(
  "/projects",
  requireAccess((req) => ({
    userId: req.body.userId,
    resourceName: req.body.resourceName,
    action: req.body.action,
  })),
  async (req, res) => {
    res.json({ message: "Project created" });
  }
);

// READ (if you still want body-based auth)
router.get(
  "/projects/:name",
  requireAccess((req) => ({
    userId: req.body.userId,
    resourceName: req.body.resourceName ?? req.params.name,
    action: req.body.action ?? "read",
  })),
  async (req, res) => {
    res.json({ name: req.params.name });
  }
);

// UPDATE (body-based)
router.put(
  "/projects/:name",
  requireAccess((req) => ({
    userId: req.body.userId,
    resourceName: req.body.resourceName ?? req.params.name,
    action: req.body.action ?? "update",
  })),
  async (req, res) => {
    res.json({ message: "Project updated" });
  }
);

// DELETE (body-based)
router.delete(
  "/projects/:name",
  requireAccess((req) => ({
    userId: req.body.userId,
    resourceName: req.body.resourceName ?? req.params.name,
    action: req.body.action ?? "delete",
  })),
  async (req, res) => {
    res.json({ message: "Project deleted" });
  }
);

export default router;
