// Operations API Routes

import { Router } from 'express';
import { roleManagementService } from './role-management.service';
import { credentialingService } from './credentialing.service';
import { crisisManagementService } from './crisis-management.service';

const router = Router();

/**
 * @swagger
 * tags:
 *   name: Operations
 *   description: Operational Management
 */

/**
 * @swagger
 * /api/operations/roles:
 *   get:
 *     summary: Get all roles
 *     description: Retrieve all operational roles
 *     tags: [Operations]
 *     security:
 *       - SovereignToken: []
 *     responses:
 *       200:
 *         description: List of roles
 */
router.get('/roles', async (req, res, next) => {
  try {
    const roles = await roleManagementService.getAllRoles();
    res.json(roles);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/operations/roles/:roleId:
 *   get:
 *     summary: Get role by ID
 */
router.get('/roles/:roleId', async (req, res, next) => {
  try {
    const role = await roleManagementService.getRole(req.params.roleId);
    if (!role) {
      return res.status(404).json({ error: 'Role not found' });
    }
    res.json(role);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/operations/credentials:
 *   post:
 *     summary: Issue credential
 */
router.post('/credentials', async (req, res, next) => {
  try {
    const { data, expiresInDays } = req.body;
    const credential = await credentialingService.issueCredential(data, expiresInDays);
    res.status(201).json(credential);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/operations/credentials/:employeeId:
 *   get:
 *     summary: Get credential
 */
router.get('/credentials/:employeeId', async (req, res, next) => {
  try {
    const credential = await credentialingService.getCredential(req.params.employeeId);
    if (!credential) {
      return res.status(404).json({ error: 'Credential not found' });
    }
    res.json(credential);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/operations/credentials/:employeeId/verify:
 *   get:
 *     summary: Verify credential
 */
router.get('/credentials/:employeeId/verify', async (req, res, next) => {
  try {
    const result = await credentialingService.verifyCredential(req.params.employeeId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/operations/crisis/protocols:
 *   get:
 *     summary: Get crisis protocols
 */
router.get('/crisis/protocols', async (req, res, next) => {
  try {
    const crisisType = req.query.crisisType as string;
    if (crisisType) {
      const protocol = await crisisManagementService.getProtocolByType(crisisType);
      res.json(protocol);
    } else {
      // Get all active protocols
      const protocols = await Promise.all([
        crisisManagementService.getProtocolByType('fx_collapse'),
        crisisManagementService.getProtocolByType('default_event'),
        crisisManagementService.getProtocolByType('liquidity_freeze'),
      ]);
      res.json(protocols.filter((p) => p !== null));
    }
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/operations/crisis/activate:
 *   post:
 *     summary: Activate crisis protocol
 */
router.post('/crisis/activate', async (req, res, next) => {
  try {
    const { crisisType, context } = req.body;
    const result = await crisisManagementService.activateCrisisProtocol(crisisType, context);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/operations/crisis/active:
 *   get:
 *     summary: Get active crises
 */
router.get('/crisis/active', async (req, res, next) => {
  try {
    const crises = await crisisManagementService.getActiveCrises();
    res.json(crises);
  } catch (error) {
      return next(error);
  }
});

export default router;

