// Omni-Sovereign Settlement Matrix API Routes

import { Router } from 'express';
import { ossmMatrixService } from './ossm-matrix.service';
import { ossmSettlementService } from './ossm-settlement.service';
import { ossmMergeService } from './ossm-merge.service';
import { ossmCoordinationService } from './ossm-coordination.service';

const router = Router();

/**
 * @swagger
 * /api/v1/ossm/matrices:
 *   get:
 *     summary: Get active matrices
 */
router.get('/matrices', async (req, res, next) => {
  try {
    const matrices = await ossmMatrixService.getActiveMatrices();
    res.json(matrices);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/ossm/matrices:
 *   post:
 *     summary: Create matrix
 */
router.post('/matrices', async (req, res, next) => {
  try {
    const matrix = await ossmMatrixService.createMatrix(req.body);
    res.status(201).json(matrix);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/ossm/coordinates:
 *   post:
 *     summary: Create settlement coordinate
 */
router.post('/coordinates', async (req, res, next) => {
  try {
    const coordinate = await ossmSettlementService.createCoordinate(req.body);
    res.status(201).json(coordinate);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/ossm/coordinates/:coordinateId/synchronize:
 *   post:
 *     summary: Synchronize coordinate
 */
router.post('/coordinates/:coordinateId/synchronize', async (req, res, next) => {
  try {
    const coordinate = await ossmSettlementService.synchronizeCoordinate(req.params.coordinateId);
    res.json(coordinate);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/ossm/merge:
 *   post:
 *     summary: Merge state
 */
router.post('/merge', async (req, res, next) => {
  try {
    const state = await ossmMergeService.mergeState(req.body);
    res.status(201).json(state);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/ossm/coordination/:matrixId:
 *   post:
 *     summary: Coordinate settlement
 */
router.post('/coordination/:matrixId', async (req, res, next) => {
  try {
    const result = await ossmCoordinationService.coordinateSettlement(req.params.matrixId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/ossm/coordination/:matrixId/status:
 *   get:
 *     summary: Get coordination status
 */
router.get('/coordination/:matrixId/status', async (req, res, next) => {
  try {
    const status = await ossmCoordinationService.getCoordinationStatus(req.params.matrixId);
    res.json(status);
  } catch (error) {
      return next(error);
  }
});

export default router;

