// DBIS Admin Console API Routes

import { Router } from 'express';
import { dbisAdminService } from './dbis-admin.service';
import { requireAdminPermission } from '@/integration/api-gateway/middleware/admin-permission.middleware';
import { AdminPermission } from '@/core/admin/shared/permissions.constants';

const router = Router();

// Global Overview Dashboard
router.get(
  '/dashboard/overview',
  requireAdminPermission(AdminPermission.VIEW_GLOBAL_OVERVIEW),
  async (req, res, next) => {
    try {
      const overview = await dbisAdminService.globalOverview.getGlobalOverview();
      return res.json(overview);
    } catch (error) {
      return next(error);
    }
  }
);

// Participants & Jurisdictions
router.get(
  '/participants',
  requireAdminPermission(AdminPermission.VIEW_PARTICIPANTS),
  async (req, res, next) => {
    try {
      const participants = await dbisAdminService.participants.getParticipantDirectory();
      return res.json(participants);
    } catch (error) {
      return next(error);
    }
  }
);

router.get(
  '/participants/:scbId',
  requireAdminPermission(AdminPermission.VIEW_PARTICIPANTS),
  async (req, res, next) => {
    try {
      const participant = await dbisAdminService.participants.getParticipantDetails(
        req.params.scbId
      );
      if (!participant) {
        return res.status(404).json({ error: 'Participant not found' });
      }
      return res.json(participant);
    } catch (error) {
      return next(error);
    }
  }
);

router.get(
  '/participants/:scbId/jurisdiction',
  requireAdminPermission(AdminPermission.SCB_JURISDICTION_SETTINGS),
  async (req, res, next) => {
    try {
      const settings = await dbisAdminService.participants.getJurisdictionSettings(
        req.params.scbId
      );
      if (!settings) {
        return res.status(404).json({ error: 'Jurisdiction settings not found' });
      }
      return res.json(settings);
    } catch (error) {
      return next(error);
    }
  }
);

router.get(
  '/corridors',
  requireAdminPermission(AdminPermission.VIEW_PARTICIPANTS),
  async (req, res, next) => {
    try {
      const corridors = await dbisAdminService.participants.getCorridors();
      return res.json(corridors);
    } catch (error) {
      return next(error);
    }
  }
);

// GRU Command Center
router.get(
  '/gru/command',
  requireAdminPermission(AdminPermission.VIEW_GRU_COMMAND),
  async (req, res, next) => {
    try {
      const dashboard = await dbisAdminService.gruCommand.getGRUCommandDashboard();
      return res.json(dashboard);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/gru/issuance/proposal',
  requireAdminPermission(AdminPermission.GRU_ISSUANCE_PROPOSAL),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.gruControls.createIssuanceProposal(
        employeeId,
        req.body
      );
      return res.status(201).json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/gru/lock',
  requireAdminPermission(AdminPermission.GRU_LOCK_UNLOCK),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.gruControls.lockUnlockGRUClass(employeeId, req.body);
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/gru/circuit-breakers',
  requireAdminPermission(AdminPermission.GRU_CIRCUIT_BREAKERS),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.gruControls.setCircuitBreakers(employeeId, req.body);
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/gru/bonds/window',
  requireAdminPermission(AdminPermission.GRU_BOND_ISSUANCE_WINDOW),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.gruControls.manageBondIssuanceWindow(
        employeeId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/gru/bonds/buyback',
  requireAdminPermission(AdminPermission.GRU_BOND_BUYBACK),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const { bondId, amount } = req.body;
      const result = await dbisAdminService.gruControls.triggerEmergencyBuyback(
        employeeId,
        bondId,
        amount
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

// GAS & QPS
router.get(
  '/gas-qps',
  requireAdminPermission(AdminPermission.VIEW_GAS_QPS),
  async (req, res, next) => {
    try {
      const dashboard = await dbisAdminService.gasQps.getGASQPSDashboard();
      return res.json(dashboard);
    } catch (error) {
      return next(error);
    }
  }
);

// CBDC & FX
router.get(
  '/cbdc-fx',
  requireAdminPermission(AdminPermission.VIEW_CBDC_FX),
  async (req, res, next) => {
    try {
      const dashboard = await dbisAdminService.cbdcFx.getCBDCFXDashboard();
      return res.json(dashboard);
    } catch (error) {
      return next(error);
    }
  }
);

// Metaverse & Edge
router.get(
  '/metaverse-edge',
  requireAdminPermission(AdminPermission.VIEW_METAVERSE_EDGE),
  async (req, res, next) => {
    try {
      const dashboard = await dbisAdminService.metaverseEdge.getMetaverseEdgeDashboard();
      return res.json(dashboard);
    } catch (error) {
      return next(error);
    }
  }
);

// Risk & Compliance
router.get(
  '/risk-compliance',
  requireAdminPermission(AdminPermission.VIEW_RISK_COMPLIANCE),
  async (req, res, next) => {
    try {
      const dashboard = await dbisAdminService.riskCompliance.getRiskComplianceDashboard();
      return res.json(dashboard);
    } catch (error) {
      return next(error);
    }
  }
);

// Corridor Controls
router.post(
  '/corridors/caps',
  requireAdminPermission(AdminPermission.CORRIDOR_ADJUST_CAPS),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.corridorControls.adjustCorridorCaps(
        employeeId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/corridors/throttle',
  requireAdminPermission(AdminPermission.CORRIDOR_THROTTLE),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.corridorControls.throttleCorridor(
        employeeId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/corridors/enable-disable',
  requireAdminPermission(AdminPermission.CORRIDOR_ENABLE_DISABLE),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.corridorControls.enableDisableCorridor(
        employeeId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

// Network Controls
router.post(
  '/network/quiesce',
  requireAdminPermission(AdminPermission.NETWORK_QUIESCE_SUBSYSTEM),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.networkControls.quiesceSubsystem(employeeId, req.body);
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/network/kill-switch',
  requireAdminPermission(AdminPermission.NETWORK_KILL_SWITCH),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.networkControls.activateKillSwitch(
        employeeId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/network/escalate',
  requireAdminPermission(AdminPermission.NETWORK_ESCALATE_INCIDENT),
  async (req, res, next) => {
    try {
      const employeeId = req.headers['x-employee-id'] as string || req.sovereignBankId || '';
      const result = await dbisAdminService.networkControls.escalateIncident(
        employeeId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

export default router;

