import express from 'express';
import * as messageController from '../controllers/message.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 message routes require authentication
router.use(authenticate);

// Get all conversations
router.get('/conversations', messageController.getConversations);

// Get all CRM users available for chat
router.get('/users', messageController.getChatUsers);

// Get unread message count
router.get('/unread-count', messageController.getUnreadCount);

// Get messages with specific user
router.get(
  '/conversation/:userId',
  validateParams(z.object({ userId: z.string().uuid() })),
  messageController.getConversation
);

// Send a message
router.post(
  '/send',
  validateBody(z.object({
    receiverId: z.string().uuid('Neplatný formát user ID'),
    content: z.string().min(1, 'Správa nemôže byť prázdna').max(5000, 'Správa je príliš dlhá'),
  })),
  messageController.sendMessage
);

// Delete conversation
router.delete(
  '/conversation/:userId',
  validateParams(z.object({ userId: z.string().uuid() })),
  messageController.deleteConversation
);

export default router;
