// Gap Audit API Routes

import { Router } from 'express';
import { gapAuditEngineService } from './gap-audit-engine.service';
import { moduleGeneratorService } from './module-generator.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';

const router = Router();

/**
 * @route POST /api/gap-audit/execute
 * @desc Execute gap audit
 */
router.post(
  '/execute',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await gapAuditEngineService.executeGapAudit(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gap-audit/:auditId
 * @desc Get audit by ID
 */
router.get(
  '/:auditId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const audit = await gapAuditEngineService.getAudit(req.params.auditId);
      if (!audit) {
        return res.status(404).json({ error: 'Audit not found' });
      }
      res.json(audit);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gap-audit/history
 * @desc Get audit history
 */
router.get(
  '/history',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const limit = parseInt(req.query.limit as string) || 100;
      const history = await gapAuditEngineService.getAuditHistory(limit);
      res.json(history);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gap-audit/modules
 * @desc Get generated modules
 */
router.get(
  '/modules',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const gapType = req.query.gapType as string | undefined;
      const modules = await moduleGeneratorService.getGeneratedModules(gapType);
      res.json(modules);
    } catch (error) {
      return next(error);
    }
  }
);

export default router;

