import express from 'express';
import * as adminController from '../controllers/admin.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { requireAdmin } from '../middlewares/auth/roleMiddleware.js';
import { validateBody, validateParams } from '../middlewares/security/validateInput.js';
import { createUserSchema, changeRoleSchema } from '../validators/auth.validators.js';
import { z } from 'zod';

const router = express.Router();

/**
 * Routes accessible to all authenticated users
 */
router.use(authenticate);

// Zoznam všetkých userov (dostupné pre všetkých autentifikovaných používateľov)
router.get('/users', adminController.getAllUsers);

/**
 * Admin-only routes
 */
router.use(requireAdmin);

/**
 * User management
 */

// Vytvorenie nového usera
router.post('/users', validateBody(createUserSchema), adminController.createUser);

// Získanie konkrétneho usera
router.get(
  '/users/:userId',
  validateParams(z.object({ userId: z.string().uuid() })),
  adminController.getUser
);

// Zmena role usera
router.patch(
  '/users/:userId/role',
  validateParams(z.object({ userId: z.string().uuid() })),
  validateBody(z.object({ role: z.enum(['admin', 'member']) })),
  adminController.changeUserRole
);

// Zmazanie usera
router.delete(
  '/users/:userId',
  validateParams(z.object({ userId: z.string().uuid() })),
  adminController.deleteUser
);

export default router;
