import { Router } from "express";
import {
  getDashboard,
  getCustomerLoyalty,
  getRewards,
  createReward,
  updateReward,
  deleteReward,
  redeemReward,
  adjustPoints,
  getTiers
} from "../controllers/loyaltyController.js";
import { requirePermission } from "../middleware/auth.js";

const router = Router();

// Public-to-authenticated
router.get("/dashboard",         getDashboard);
router.get("/tiers",             getTiers);
router.get("/rewards",           getRewards);
router.get("/customer/:id",      getCustomerLoyalty);

// Actions
router.post("/redeem",           redeemReward);
router.post("/adjust",           requirePermission("loyalty", "edit"), adjustPoints);

// Rewards CRUD (admin only)
router.post("/rewards",          requirePermission("loyalty", "create"), createReward);
router.put("/rewards/:id",       requirePermission("loyalty", "edit"), updateReward);
router.delete("/rewards/:id",    requirePermission("loyalty", "delete"), deleteReward);

export default router;
