import express from 'express';
import * as adminController from '../controllers/admin.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { requireAdmin, requireTeamLeaderOrAdmin } 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();

/**
 * All admin routes require authentication
 */
router.use(authenticate);

// Zoznam všetkých userov (admin + team_leader)
router.get('/users', requireTeamLeaderOrAdmin, adminController.getAllUsers);

// All other routes require admin role
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
);

// Update user details
router.patch(
  '/users/:userId',
  validateParams(z.object({ userId: z.string().uuid() })),
  validateBody(z.object({
    firstName: z.string().max(100).optional(),
    lastName: z.string().max(100).optional(),
  })),
  adminController.updateUser
);

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

// Reset user password
router.post(
  '/users/:userId/reset-password',
  validateParams(z.object({ userId: z.string().uuid() })),
  adminController.resetUserPassword
);

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

/**
 * Server status
 */

// Server status (CPU, RAM, Disk, Network, Uptime)
router.get('/server-status', adminController.getServerStatus);

/**
 * Notifications
 */

// Manually trigger event notifications (for testing)
router.post('/trigger-notifications', adminController.triggerNotifications);

export default router;
