import { Router } from "express";
import { Role } from "@prisma/client";
import { requireAuth, requireRole } from "../../middleware/auth";
import { userController } from "./users.controller";

const router = Router();

// All user management endpoints require authentication
router.use(requireAuth);

// List users (admins and managers)
router.get(
  "/",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.listUsers
);

// User login activity logs (admins and managers)
router.get(
  "/logs",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.getUserLogs
);

// Per-user login activities (admins and managers)
router.get(
  "/:id/logs",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.getUserLoginActivities
);

// Resend invitation email (admins and managers)
router.post(
  "/:id/invite",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.resendInvitation
);

// Get a single user (admins and managers)
router.get(
  "/:id",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.getUser
);

// Create user (admins and managers)
router.post(
  "/",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.createUser
);

// Update user (admins and managers)
router.put(
  "/:id",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.updateUser
);

// Toggle lock (admins and managers)
router.put(
  "/:id/lock",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.toggleLock
);

// Delete single user (admins and managers)
router.delete(
  "/:id",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.deleteUser
);

// Bulk delete (admins and managers)
router.delete(
  "/",
  requireRole([Role.ADMIN, Role.MANAGER]),
  userController.deleteUsers
);

export default router;
