/**
 * @swagger
 * tags:
 *   name: SolaceNet Audit Log
 *   description: Audit log queries - immutable audit trail for capability toggles
 */

import { Router } from 'express';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { auditLogService, AuditLogQuery } from './audit-log.service';

const router = Router();

/**
 * @swagger
 * /api/v1/solacenet/audit/toggles:
 *   get:
 *     summary: Query toggle audit logs
 *     description: Query audit logs for capability toggles
 *     tags: [SolaceNet Audit Log]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: query
 *         name: capabilityId
 *         schema:
 *           type: string
 *       - in: query
 *         name: actor
 *         schema:
 *           type: string
 *       - in: query
 *         name: action
 *         schema:
 *           type: string
 *           enum: [enabled, disabled, suspended, drain, kill_switch]
 *       - in: query
 *         name: fromDate
 *         schema:
 *           type: string
 *           format: date-time
 *       - in: query
 *         name: toDate
 *         schema:
 *           type: string
 *           format: date-time
 *       - in: query
 *         name: limit
 *         schema:
 *           type: number
 *           default: 100
 *       - in: query
 *         name: offset
 *         schema:
 *           type: number
 *           default: 0
 *     responses:
 *       200:
 *         description: List of audit logs
 */
router.get('/toggles', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const query: AuditLogQuery = {
      capabilityId: req.query.capabilityId as string | undefined,
      actor: req.query.actor as string | undefined,
      action: req.query.action as string | undefined,
      fromDate: req.query.fromDate ? new Date(req.query.fromDate as string) : undefined,
      toDate: req.query.toDate ? new Date(req.query.toDate as string) : undefined,
      limit: req.query.limit ? parseInt(req.query.limit as string, 10) : undefined,
      offset: req.query.offset ? parseInt(req.query.offset as string, 10) : undefined,
    };

    const logs = await auditLogService.queryToggleLogs(query);
    res.json(logs);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/solacenet/audit/decisions:
 *   get:
 *     summary: Query policy decision logs
 *     description: Query audit logs for policy decisions
 *     tags: [SolaceNet Audit Log]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: query
 *         name: capabilityId
 *         schema:
 *           type: string
 *       - in: query
 *         name: actor
 *         schema:
 *           type: string
 *       - in: query
 *         name: fromDate
 *         schema:
 *           type: string
 *           format: date-time
 *       - in: query
 *         name: toDate
 *         schema:
 *           type: string
 *           format: date-time
 *       - in: query
 *         name: limit
 *         schema:
 *           type: number
 *           default: 100
 *       - in: query
 *         name: offset
 *         schema:
 *           type: number
 *           default: 0
 *     responses:
 *       200:
 *         description: List of decision logs
 */
router.get('/decisions', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const query: AuditLogQuery = {
      capabilityId: req.query.capabilityId as string | undefined,
      actor: req.query.actor as string | undefined,
      fromDate: req.query.fromDate ? new Date(req.query.fromDate as string) : undefined,
      toDate: req.query.toDate ? new Date(req.query.toDate as string) : undefined,
      limit: req.query.limit ? parseInt(req.query.limit as string, 10) : undefined,
      offset: req.query.offset ? parseInt(req.query.offset as string, 10) : undefined,
    };

    const logs = await auditLogService.queryDecisionLogs(query);
    res.json(logs);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/solacenet/audit/{id}:
 *   get:
 *     summary: Get specific audit log entry
 *     description: Get details of a specific audit log entry
 *     tags: [SolaceNet Audit Log]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Audit log entry
 *       404:
 *         description: Audit log not found
 */
router.get('/:id', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const log = await auditLogService.getAuditLog(req.params.id);
    if (!log) {
      return res.status(404).json({ error: 'Audit log not found' });
    }
    res.json(log);
  } catch (error) {
    next(error);
  }
});

export default router;
