import { Router } from 'express';
import { authenticate, requireAuthenticated, requireTeacherOrAdmin } from '../middlewares/auth';
import {
  getUserConversations,
  createConversation,
  getConversation,
  saveMessage,
  endConversation,
  deleteConversation,
  updateConversation,
  getRecentConversations,
  getAllConversationsAdmin
} from '../controllers/conversationController';

const router = Router();

/**
 * @route   GET /api/conversations
 * @desc    Get user's conversations with pagination
 * @access  Private
 */
router.get('/', authenticate, requireAuthenticated, getUserConversations);

/**
 * @route   GET /api/conversations/recent
 * @desc    Get user's recent conversations for recent bots
 * @access  Private
 */
router.get('/recent', authenticate, requireAuthenticated, getRecentConversations);

/**
 * @route   GET /api/conversations/admin
 * @desc    List all conversations (admin/teacher) with pagination
 * @access  Private (Admin/Teacher)
 */
router.get('/admin', authenticate, requireTeacherOrAdmin, getAllConversationsAdmin);

/**
 * @route   POST /api/conversations
 * @desc    Create a new conversation (student only)
 * @access  Private (Student)
 */
router.post('/', authenticate, requireAuthenticated, createConversation);

/**
 * @route   GET /api/conversations/:id
 * @desc    Get conversation by ID with messages
 * @access  Private
 */
router.get('/:id', authenticate, requireAuthenticated, getConversation);

/**
 * @route   POST /api/conversations/:id/messages
 * @desc    Save a message to conversation
 * @access  Private
 */
router.post('/:id/messages', authenticate, requireAuthenticated, saveMessage);

/**
 * @route   PUT /api/conversations/:id/end
 * @desc    End a conversation
 * @access  Private
 */
router.put('/:id/end', authenticate, requireAuthenticated, endConversation);

/**
 * @route   PUT /api/conversations/:id
 * @desc    Update conversation (limited fields)
 * @access  Private
 */
router.put('/:id', authenticate, requireAuthenticated, updateConversation);

/**
 * @route   DELETE /api/conversations/:id
 * @desc    Delete conversation (admin/teacher only)
 * @access  Private (Admin/Teacher)
 */
router.delete('/:id', authenticate, requireAuthenticated, deleteConversation);

// (moved /admin above /:id to avoid dynamic route catching it)

export default router;

