// MSGF API Routes

import { Router } from 'express';
import { msgfCouncilService } from './msgf-council.service';
import { msgfTierService } from './msgf-tier.service';
import { msgfPolicyService } from './msgf-policy.service';
import { msgfEnforcementService } from './msgf-enforcement.service';
import { aesuService } from './aesu.service';

const router = Router();

// Council routes
router.post('/councils', async (req, res, next) => {
  try {
    const council = await msgfCouncilService.createCouncil(req.body);
    res.json(council);
  } catch (error) {
      return next(error);
  }
});

router.get('/councils', async (req, res, next) => {
  try {
    const councils = await msgfCouncilService.getAllCouncils(req.query.councilType as string);
    res.json(councils);
  } catch (error) {
      return next(error);
  }
});

router.get('/councils/:councilId', async (req, res, next) => {
  try {
    const council = await msgfCouncilService.getCouncil(req.params.councilId);
    res.json(council);
  } catch (error) {
      return next(error);
  }
});

router.post('/councils/:councilId/members', async (req, res, next) => {
  try {
    const member = await msgfCouncilService.addMember({
      councilId: req.params.councilId,
      ...req.body,
    });
    res.json(member);
  } catch (error) {
      return next(error);
  }
});

router.post('/councils/:councilId/decisions', async (req, res, next) => {
  try {
    const decision = await msgfCouncilService.createDecision(
      req.params.councilId,
      req.body.decisionType,
      req.body.decisionContent
    );
    res.json(decision);
  } catch (error) {
      return next(error);
  }
});

router.post('/decisions/:decisionId/approve', async (req, res, next) => {
  try {
    const decision = await msgfCouncilService.approveDecision(req.params.decisionId);
    res.json(decision);
  } catch (error) {
      return next(error);
  }
});

router.post('/decisions/:decisionId/execute', async (req, res, next) => {
  try {
    const decision = await msgfCouncilService.executeDecision(req.params.decisionId);
    res.json(decision);
  } catch (error) {
      return next(error);
  }
});

// Tier routes
router.post('/tiers', async (req, res, next) => {
  try {
    const tier = await msgfTierService.createTier(req.body);
    res.json(tier);
  } catch (error) {
      return next(error);
  }
});

router.get('/tiers', async (req, res, next) => {
  try {
    const tiers = await msgfTierService.getAllTiers();
    res.json(tiers);
  } catch (error) {
      return next(error);
  }
});

router.get('/tiers/:tierId', async (req, res, next) => {
  try {
    const tier = await msgfTierService.getTier(req.params.tierId);
    res.json(tier);
  } catch (error) {
      return next(error);
  }
});

router.post('/tiers/delegations', async (req, res, next) => {
  try {
    const delegation = await msgfTierService.createDelegation(req.body);
    res.json(delegation);
  } catch (error) {
      return next(error);
  }
});

// Policy routes
router.post('/policies', async (req, res, next) => {
  try {
    const policy = await msgfPolicyService.createPolicy(req.body);
    res.json(policy);
  } catch (error) {
      return next(error);
  }
});

router.get('/policies', async (req, res, next) => {
  try {
    const policies = await msgfPolicyService.getAllPolicies(req.query);
    res.json(policies);
  } catch (error) {
      return next(error);
  }
});

router.get('/policies/:policyId', async (req, res, next) => {
  try {
    const policy = await msgfPolicyService.getPolicy(req.params.policyId);
    res.json(policy);
  } catch (error) {
      return next(error);
  }
});

router.post('/policies/:policyId/activate', async (req, res, next) => {
  try {
    const policy = await msgfPolicyService.activatePolicy(
      req.params.policyId,
      req.body.effectiveDate ? new Date(req.body.effectiveDate) : undefined
    );
    res.json(policy);
  } catch (error) {
      return next(error);
  }
});

// Enforcement routes
router.post('/enforcements', async (req, res, next) => {
  try {
    const enforcement = await msgfEnforcementService.createEnforcement(req.body);
    res.json(enforcement);
  } catch (error) {
      return next(error);
  }
});

router.post('/enforcements/:enforcementId/execute', async (req, res, next) => {
  try {
    const enforcement = await msgfEnforcementService.executeEnforcement(req.params.enforcementId);
    res.json(enforcement);
  } catch (error) {
      return next(error);
  }
});

router.post('/privileges/suspend', async (req, res, next) => {
  try {
    const privilege = await msgfEnforcementService.suspendPrivilege(req.body);
    res.json(privilege);
  } catch (error) {
      return next(error);
  }
});

router.post('/privileges/:privilegeId/restore', async (req, res, next) => {
  try {
    const privilege = await msgfEnforcementService.restorePrivilege(req.params.privilegeId);
    res.json(privilege);
  } catch (error) {
      return next(error);
  }
});

router.get('/privileges/:sovereignBankId', async (req, res, next) => {
  try {
    const privileges = await msgfEnforcementService.getPrivileges(req.params.sovereignBankId);
    res.json(privileges);
  } catch (error) {
      return next(error);
  }
});

// AESU routes
router.post('/aesu/analyze', async (req, res, next) => {
  try {
    const recommendations = await aesuService.performAnalysis(req.body);
    res.json(recommendations);
  } catch (error) {
      return next(error);
  }
});

router.post('/aesu/recommendations', async (req, res, next) => {
  try {
    const result = await aesuService.createPolicyRecommendation(
      req.body.councilId,
      req.body.analysisRequest
    );
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

router.post('/aesu/decisions', async (req, res, next) => {
  try {
    const decision = await aesuService.executeAutonomousDecision(
      req.body.decisionType,
      req.body.decisionData
    );
    res.json(decision);
  } catch (error) {
      return next(error);
  }
});

export default router;

