// Constitution API Routes

import { Router } from 'express';
import { constitutionService } from './constitution.service';
import { governanceService } from './governance.service';
import { disputeResolutionService } from './dispute-resolution.service';

const router = Router();

/**
 * @swagger
 * tags:
 *   name: Constitution
 *   description: DBIS Constitutional Framework
 */

/**
 * @swagger
 * /api/constitution/articles:
 *   get:
 *     summary: Get all constitution articles
 *     description: Retrieve all articles from the DBIS Constitutional Framework
 *     tags: [Constitution]
 *     security:
 *       - SovereignToken: []
 *     responses:
 *       200:
 *         description: List of constitution articles
 */
router.get('/articles', async (req, res, next) => {
  try {
    const articles = await constitutionService.getAllArticles();
    res.json(articles);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/constitution/articles/:articleNumber:
 *   get:
 *     summary: Get article by number
 */
router.get('/articles/:articleNumber', async (req, res, next) => {
  try {
    const article = await constitutionService.getArticle(req.params.articleNumber);
    if (!article) {
      return res.status(404).json({ error: 'Article not found' });
    }
    res.json(article);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/constitution/legal-personality:
 *   get:
 *     summary: Check legal personality status
 */
router.get('/legal-personality', async (req, res, next) => {
  try {
    const status = await constitutionService.checkLegalPersonality();
    res.json(status);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/constitution/governance/bodies:
 *   get:
 *     summary: Get governance bodies
 */
router.get('/governance/bodies', async (req, res, next) => {
  try {
    const bodyType = req.query.bodyType as string;
    if (bodyType) {
      const body = await governanceService.getGovernanceBody(bodyType);
      res.json(body);
    } else {
      // Get all bodies
      const bodies = await Promise.all([
        governanceService.getGovernanceBody('BoardOfGovernors'),
        governanceService.getGovernanceBody('MSC'),
        governanceService.getGovernanceBody('CAA'),
        governanceService.getGovernanceBody('SCC'),
      ]);
      res.json(bodies);
    }
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/constitution/governance/voting-weight:
 *   get:
 *     summary: Calculate voting weight for sovereign bank
 */
router.get('/governance/voting-weight/:sovereignBankId', async (req, res, next) => {
  try {
    const weight = await governanceService.calculateVotingWeight(req.params.sovereignBankId);
    res.json(weight);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/constitution/governance/proposals:
 *   post:
 *     summary: Create proposal
 */
router.post('/governance/proposals', async (req, res, next) => {
  try {
    const proposal = await governanceService.createProposal(req.body);
    res.status(201).json(proposal);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/constitution/governance/votes:
 *   post:
 *     summary: Cast vote
 */
router.post('/governance/votes', async (req, res, next) => {
  try {
    const { votingRecordId, memberId, vote } = req.body;
    const result = await governanceService.castVote(votingRecordId, memberId, vote);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/constitution/disputes:
 *   post:
 *     summary: Initiate dispute
 */
router.post('/disputes', async (req, res, next) => {
  try {
    const dispute = await disputeResolutionService.initiateDispute(req.body);
    res.status(201).json(dispute);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/constitution/disputes/:disputeId/escalate:
 *   post:
 *     summary: Escalate dispute
 */
router.post('/disputes/:disputeId/escalate', async (req, res, next) => {
  try {
    const { stage, reason } = req.body;
    let result;
    if (stage === 'caa_mediation') {
      result = await disputeResolutionService.escalateToCAAMediation(req.params.disputeId, reason);
    } else if (stage === 'binding_arbitration') {
      result = await disputeResolutionService.escalateToBindingArbitration(req.params.disputeId, reason);
    } else {
      return res.status(400).json({ error: 'Invalid escalation stage' });
    }
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

