import express from 'express';
import { wrapAsync } from '@/api/http/request';
import permissions from '@/middlewares/check-permissions';
import { FEATURE_PERMISSIONS } from '@/shared/constants/product-permissions';
import * as directMessages from '@/api/controllers/direct_messages.controller';

const router = express.Router();
router.use(permissions.checkPermissions(FEATURE_PERMISSIONS.READ_PARENT_COMM));

/**
 * @openapi
 * tags:
 *   - name: DirectMessages
 *     description: >
 *       1:1 conversations between staff (teacher / office_manager) and guardians,
 *       discovered through a shared student. Access is membership-based — a user
 *       only sees conversations they participate in.
 */

/**
 * @openapi
 * /api/direct_messages/contacts:
 *   get:
 *     tags: [DirectMessages]
 *     summary: People the current user can start/continue a chat with (via a student)
 *     responses:
 *       200: { description: Contacts. }
 */
router.get('/contacts', wrapAsync(directMessages.contacts));

/**
 * @openapi
 * /api/direct_messages/conversations:
 *   get:
 *     tags: [DirectMessages]
 *     summary: The current user's conversations (one per counterpart + student)
 *     responses:
 *       200: { description: Conversations. }
 */
router.get('/conversations', wrapAsync(directMessages.conversations));

/**
 * @openapi
 * /api/direct_messages/thread/{otherUserId}:
 *   get:
 *     tags: [DirectMessages]
 *     summary: Messages between the current user and another user for one student (marks read)
 *     responses:
 *       200: { description: Thread. }
 */
router.get('/thread/:otherUserId', wrapAsync(directMessages.thread));

/**
 * @openapi
 * /api/direct_messages/send:
 *   post:
 *     tags: [DirectMessages]
 *     summary: Send a message to a contact
 *     responses:
 *       200: { description: The created message. }
 *       403: { $ref: '#/components/responses/ForbiddenError' }
 */
router.post('/send', wrapAsync(directMessages.send));

export default router;
