/**
 * Evolution API Routes
 * 
 * API endpoints for Evolution API instance management
 * All routes are tenant-scoped and require authentication
 * 
 * @module routes/evolution
 */

const express = require('express');
const router = express.Router();
const EvolutionInstanceController = require('../controllers/EvolutionInstanceController');
const EvolutionWebhookController = require('../controllers/EvolutionWebhookController');
const { requireAuth, requireAdmin } = require('../middleware/auth');
const { apiLimiter } = require('../middleware/security');

// ============================================
// Tenant Routes (require authentication)
// ============================================

/**
 * @swagger
 * /api/tenant/whatsapp/instances:
 *   get:
 *     summary: List all instances for tenant
 *     tags: [Evolution API]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Instances retrieved successfully
 *       500:
 *         description: Server error
 */
router.get('/instances', requireAdmin, EvolutionInstanceController.listInstances);

/**
 * @swagger
 * /api/tenant/whatsapp/connected-instances:
 *   get:
 *     summary: List connected instances for conversations dropdown
 *     tags: [Evolution API]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Connected instances retrieved successfully
 *       500:
 *         description: Server error
 */
router.get('/connected-instances', EvolutionInstanceController.listConnectedInstances);

/**
 * @swagger
 * /api/tenant/whatsapp/instances:
 *   post:
 *     summary: Create new instance
 *     tags: [Evolution API]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - instance_name
 *             properties:
 *               instance_name:
 *                 type: string
 *                 example: "Sales Support"
 *               instance_type:
 *                 type: string
 *                 enum: [whatsapp_web, whatsapp_business]
 *                 example: "whatsapp_web"
 *               phone_number:
 *                 type: string
 *                 example: "5511999999999"
 *               preferences:
 *                 type: object
 *     responses:
 *       200:
 *         description: Instance created successfully
 *       400:
 *         description: Invalid input
 *       403:
 *         description: Instance limit reached
 *       500:
 *         description: Server error
 */
router.post('/instances', requireAdmin, apiLimiter, EvolutionInstanceController.createInstance);

/**
 * @swagger
 * /api/tenant/whatsapp/instances/{id}/status:
 *   get:
 *     summary: Get instance status
 *     tags: [Evolution API]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: Status retrieved successfully
 *       404:
 *         description: Instance not found
 *       500:
 *         description: Server error
 */
router.get('/instances/:id/status', requireAdmin, EvolutionInstanceController.getInstanceStatus);

/**
 * @swagger
 * /api/tenant/whatsapp/instances/{id}/connect:
 *   post:
 *     summary: Connect instance (generate QR code)
 *     tags: [Evolution API]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: Connection initiated successfully
 *       404:
 *         description: Instance not found
 *       500:
 *         description: Server error
 */
router.post('/instances/:id/connect', requireAdmin, apiLimiter, EvolutionInstanceController.connectInstance);

/**
 * @swagger
 * /api/tenant/whatsapp/instances/{id}/disconnect:
 *   post:
 *     summary: Disconnect instance
 *     tags: [Evolution API]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: Disconnected successfully
 *       404:
 *         description: Instance not found
 *       500:
 *         description: Server error
 */
router.post('/instances/:id/disconnect', requireAdmin, apiLimiter, EvolutionInstanceController.disconnectInstance);

/**
 * @swagger
 * /api/tenant/whatsapp/instances/{id}:
 *   delete:
 *     summary: Delete instance
 *     tags: [Evolution API]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: Instance deleted successfully
 *       404:
 *         description: Instance not found
 *       500:
 *         description: Server error
 */
router.delete('/instances/:id', requireAdmin, apiLimiter, EvolutionInstanceController.deleteInstance);

/**
 * @swagger
 * /api/tenant/whatsapp/instances/{id}/qr:
 *   get:
 *     summary: Get QR code for instance
 *     tags: [Evolution API]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: QR code retrieved successfully
 *       404:
 *         description: QR code not available
 *       500:
 *         description: Server error
 */
router.get('/instances/:id/qr', requireAdmin, EvolutionInstanceController.getQRCode);

/**
 * @swagger
 * /api/tenant/whatsapp/instances/{id}/settings:
 *   get:
 *     summary: Get instance settings
 *     tags: [Evolution API Instances]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: Settings retrieved successfully
 *       404:
 *         description: Instance not found
 *       500:
 *         description: Server error
 */
router.get('/instances/:id/settings', requireAdmin, EvolutionInstanceController.getInstanceSettings);

/**
 * @swagger
 * /api/tenant/whatsapp/instances/{id}/settings:
 *   put:
 *     summary: Update instance settings
 *     tags: [Evolution API Instances]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               rejectCall:
 *                 type: boolean
 *               alwaysOnline:
 *                 type: boolean
 *               readMessages:
 *                 type: boolean
 *               readStatus:
 *                 type: boolean
 *               syncFullHistory:
 *                 type: boolean
 *               groupsIgnore:
 *                 type: boolean
 *     responses:
 *       200:
 *         description: Settings updated successfully
 *       404:
 *         description: Instance not found
 *       500:
 *         description: Server error
 */
router.put('/instances/:id/settings', requireAdmin, apiLimiter, EvolutionInstanceController.updateInstanceSettings);

/**
 * @swagger
 * /api/tenant/whatsapp/sync-profile-pictures:
 *   post:
 *     summary: Sync profile pictures for all conversations
 *     tags: [Evolution API]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Profile pictures sync started
 *       401:
 *         description: Unauthorized
 *       500:
 *         description: Server error
 */
router.post('/sync-profile-pictures', requireAdmin, apiLimiter, EvolutionInstanceController.syncProfilePictures);

// ============================================
// Webhook Routes (no authentication - verified by Evolution API)
// ============================================

/**
 * @swagger
 * /api/webhooks/evolution/{tenantId}:
 *   post:
 *     summary: Receive webhook from Evolution API
 *     tags: [Evolution API Webhooks]
 *     parameters:
 *       - in: path
 *         name: tenantId
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *     responses:
 *       200:
 *         description: Webhook processed successfully
 */
// Note: This route will be registered in server.js under /api/webhooks/evolution/:tenantId

module.exports = router;
