import express from 'express';
import * as groupController from '../controllers/group.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { validateBody, validateParams } from '../middlewares/security/validateInput.js';
import { z } from 'zod';

const router = express.Router();

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

// Create a new group
router.post(
  '/',
  validateBody(
    z.object({
      name: z.string().min(1, 'Názov skupiny je povinný').max(100, 'Názov je príliš dlhý'),
      memberIds: z.array(z.string().uuid()).min(1, 'Vyberte aspoň jedného člena'),
    })
  ),
  groupController.createGroup
);

// Get all groups for current user
router.get('/', groupController.getUserGroups);

// Get group details
router.get(
  '/:groupId',
  validateParams(z.object({ groupId: z.string().uuid() })),
  groupController.getGroupDetails
);

// Update group name
router.patch(
  '/:groupId',
  validateParams(z.object({ groupId: z.string().uuid() })),
  validateBody(z.object({ name: z.string().min(1).max(100) })),
  groupController.updateGroupName
);

// Delete group
router.delete(
  '/:groupId',
  validateParams(z.object({ groupId: z.string().uuid() })),
  groupController.deleteGroup
);

// Get group messages
router.get(
  '/:groupId/messages',
  validateParams(z.object({ groupId: z.string().uuid() })),
  groupController.getGroupMessages
);

// Send message to group
router.post(
  '/:groupId/messages',
  validateParams(z.object({ groupId: z.string().uuid() })),
  validateBody(
    z.object({
      content: z.string().min(1, 'Správa nemôže byť prázdna').max(5000, 'Správa je príliš dlhá'),
    })
  ),
  groupController.sendGroupMessage
);

// Add member to group
router.post(
  '/:groupId/members',
  validateParams(z.object({ groupId: z.string().uuid() })),
  validateBody(z.object({ userId: z.string().uuid() })),
  groupController.addGroupMember
);

// Remove member from group
router.delete(
  '/:groupId/members/:userId',
  validateParams(
    z.object({
      groupId: z.string().uuid(),
      userId: z.string().uuid(),
    })
  ),
  groupController.removeGroupMember
);

export default router;
