// Quantum-Temporal Arbitration Engine API Routes

import { Router } from 'express';
import { qtaeDetectionService } from './qtae-detection.service';
import { qtaeResolutionService } from './qtae-resolution.service';
import { qtaeAffirmationService } from './qtae-affirmation.service';
import { qtaeNotificationService } from './qtae-notification.service';

const router = Router();

/**
 * @swagger
 * /api/v1/qtae/arbitrations:
 *   post:
 *     summary: Create arbitration
 */
router.post('/arbitrations', async (req, res, next) => {
  try {
    const arbitration = await qtaeDetectionService.createArbitration(req.body);
    res.status(201).json(arbitration);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/qtae/contradictions/detect:
 *   post:
 *     summary: Detect contradiction
 */
router.post('/contradictions/detect', async (req, res, next) => {
  try {
    const event = await qtaeDetectionService.detectContradiction(req.body);
    res.status(201).json(event);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/qtae/contradictions/unresolved:
 *   get:
 *     summary: Get unresolved contradictions
 */
router.get('/contradictions/unresolved', async (req, res, next) => {
  try {
    const contradictions = await qtaeDetectionService.getUnresolvedContradictions();
    res.json(contradictions);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/qtae/rollbacks:
 *   post:
 *     summary: Create rollback
 */
router.post('/rollbacks', async (req, res, next) => {
  try {
    const rollback = await qtaeResolutionService.createRollback(req.body);
    res.status(201).json(rollback);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/qtae/rollbacks/:rollbackId/execute:
 *   post:
 *     summary: Execute rollback
 */
router.post('/rollbacks/:rollbackId/execute', async (req, res, next) => {
  try {
    const rollback = await qtaeResolutionService.executeRollback(req.params.rollbackId);
    res.json(rollback);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/qtae/arbitrations/:arbitrationId/rollback:
 *   post:
 *     summary: Rollback to consistent state
 */
router.post('/arbitrations/:arbitrationId/rollback', async (req, res, next) => {
  try {
    const result = await qtaeResolutionService.rollbackToConsistentState(req.params.arbitrationId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/qtae/decisions:
 *   post:
 *     summary: Create decision
 */
router.post('/decisions', async (req, res, next) => {
  try {
    const decision = await qtaeAffirmationService.createDecision(req.body);
    res.status(201).json(decision);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/qtae/arbitrations/:arbitrationId/affirm:
 *   post:
 *     summary: Affirm finality
 */
router.post('/arbitrations/:arbitrationId/affirm', async (req, res, next) => {
  try {
    const decision = await qtaeAffirmationService.affirmFinality(req.params.arbitrationId);
    res.json(decision);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/qtae/notifications/msa/:decisionId:
 *   post:
 *     summary: Notify MSA
 */
router.post('/notifications/msa/:decisionId', async (req, res, next) => {
  try {
    const result = await qtaeNotificationService.notifyMSA(req.params.decisionId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

