// Supra-Constitutional DBIS Charter API Routes

import { Router } from 'express';
import { scdcCharterService } from './scdc-charter.service';
import { scdcAuthorityService } from './scdc-authority.service';
import { scdcTemporalIntegrityService } from './scdc-temporal-integrity.service';
import { scdcAIMandateService } from './scdc-ai-mandate.service';

const router = Router();

/**
 * @swagger
 * /api/v1/scdc/charter:
 *   get:
 *     summary: Get active charter
 */
router.get('/charter', async (req, res, next) => {
  try {
    const charter = await scdcCharterService.getActiveCharter();
    if (!charter) {
      return res.status(404).json({ error: 'No active charter found' });
    }
    res.json(charter);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/charter:
 *   post:
 *     summary: Create charter
 */
router.post('/charter', async (req, res, next) => {
  try {
    const charter = await scdcCharterService.createCharter(req.body);
    res.status(201).json(charter);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/charter/:charterId:
 *   get:
 *     summary: Get charter by ID
 */
router.get('/charter/:charterId', async (req, res, next) => {
  try {
    const charter = await scdcCharterService.getCharter(req.params.charterId);
    if (!charter) {
      return res.status(404).json({ error: 'Charter not found' });
    }
    res.json(charter);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/articles:
 *   post:
 *     summary: Create charter article
 */
router.post('/articles', async (req, res, next) => {
  try {
    const article = await scdcCharterService.createArticle(req.body);
    res.status(201).json(article);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/articles/:charterId:
 *   get:
 *     summary: Get articles for charter
 */
router.get('/articles/:charterId', async (req, res, next) => {
  try {
    const articles = await scdcCharterService.getArticles(req.params.charterId);
    res.json(articles);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/authority/scope:
 *   get:
 *     summary: Get authority scope
 */
router.get('/authority/scope', async (req, res, next) => {
  try {
    const scope = await scdcAuthorityService.getAuthorityScope();
    res.json(scope);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/authority/verify:
 *   post:
 *     summary: Verify settlement supremacy
 */
router.post('/authority/verify', async (req, res, next) => {
  try {
    const verified = await scdcAuthorityService.verifySettlementSupremacy(req.body);
    res.json({ verified });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/temporal-integrity/check:
 *   post:
 *     summary: Perform temporal integrity check
 */
router.post('/temporal-integrity/check', async (req, res, next) => {
  try {
    const check = await scdcTemporalIntegrityService.performIntegrityCheck(req.body);
    res.status(201).json(check);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/temporal-integrity/unresolved:
 *   get:
 *     summary: Get unresolved integrity checks
 */
router.get('/temporal-integrity/unresolved', async (req, res, next) => {
  try {
    const checks = await scdcTemporalIntegrityService.getUnresolvedChecks();
    res.json(checks);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/ai-actions:
 *   post:
 *     summary: Create AI autonomous action
 */
router.post('/ai-actions', async (req, res, next) => {
  try {
    const action = await scdcAIMandateService.createAIAction(req.body);
    res.status(201).json(action);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/scdc/ai-actions/:actionId/execute:
 *   post:
 *     summary: Execute AI action
 */
router.post('/ai-actions/:actionId/execute', async (req, res, next) => {
  try {
    await scdcAIMandateService.executeAction(req.params.actionId);
    res.json({ message: 'Action executed' });
  } catch (error) {
      return next(error);
  }
});

export default router;

