// SCB Admin Console API Routes

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

const router = Router();

// SCB Overview Dashboard
router.get(
  '/dashboard/overview',
  requireAdminPermission(AdminPermission.VIEW_SCB_OVERVIEW),
  async (req, res, next) => {
    try {
      const scbId = (req as any).sovereignBankId;
      if (!scbId) {
        return res.status(400).json({ error: 'Sovereign Bank ID required' });
      }
      const overview = await scbAdminService.overview.getSCBOverview(scbId);
      return res.json(overview);
    } catch (error) {
      return next(error);
    }
  }
);

// FI Management
router.get(
  '/fi',
  requireAdminPermission(AdminPermission.VIEW_FI_MANAGEMENT),
  async (req, res, next) => {
    try {
      const scbId = (req as any).sovereignBankId;
      if (!scbId) {
        return res.status(400).json({ error: 'Sovereign Bank ID required' });
      }
      const dashboard = await scbAdminService.fiManagement.getFIManagementDashboard(scbId);
      return res.json(dashboard);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/fi/approve-suspend',
  requireAdminPermission(AdminPermission.FI_APPROVE_SUSPEND),
  async (req, res, next) => {
    try {
      const scbId = (req as any).sovereignBankId;
      if (!scbId) {
        return res.status(400).json({ error: 'Sovereign Bank ID required' });
      }
      const employeeId = req.headers['x-employee-id'] as string || scbId;
      const result = await scbAdminService.fiControls.approveSuspendFI(
        employeeId,
        scbId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/fi/limits',
  requireAdminPermission(AdminPermission.FI_SET_LIMITS),
  async (req, res, next) => {
    try {
      const scbId = (req as any).sovereignBankId;
      if (!scbId) {
        return res.status(400).json({ error: 'Sovereign Bank ID required' });
      }
      const employeeId = req.headers['x-employee-id'] as string || scbId;
      const result = await scbAdminService.fiControls.setFILimits(employeeId, scbId, req.body);
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/fi/api-profile',
  requireAdminPermission(AdminPermission.FI_API_PROFILES),
  async (req, res, next) => {
    try {
      const scbId = (req as any).sovereignBankId;
      if (!scbId) {
        return res.status(400).json({ error: 'Sovereign Bank ID required' });
      }
      const employeeId = req.headers['x-employee-id'] as string || scbId;
      const result = await scbAdminService.fiControls.assignAPIProfile(
        employeeId,
        scbId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

// Corridor & FX Policy
router.get(
  '/corridors',
  requireAdminPermission(AdminPermission.VIEW_CORRIDOR_POLICY),
  async (req, res, next) => {
    try {
      const scbId = (req as any).sovereignBankId;
      if (!scbId) {
        return res.status(400).json({ error: 'Sovereign Bank ID required' });
      }
      const dashboard = await scbAdminService.corridorPolicy.getCorridorPolicyDashboard(scbId);
      return res.json(dashboard);
    } catch (error) {
      return next(error);
    }
  }
);

// CBDC Controls
router.post(
  '/cbdc/parameters',
  requireAdminPermission(AdminPermission.CBDC_UPDATE_PARAMETERS),
  async (req, res, next) => {
    try {
      const scbId = (req as any).sovereignBankId;
      if (!scbId) {
        return res.status(400).json({ error: 'Sovereign Bank ID required' });
      }
      const employeeId = req.headers['x-employee-id'] as string || scbId;
      const result = await scbAdminService.cbdcControls.updateCBDCParameters(
        employeeId,
        scbId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

router.post(
  '/gru/policy',
  requireAdminPermission(AdminPermission.CBDC_UPDATE_PARAMETERS),
  async (req, res, next) => {
    try {
      const scbId = (req as any).sovereignBankId;
      if (!scbId) {
        return res.status(400).json({ error: 'Sovereign Bank ID required' });
      }
      const employeeId = req.headers['x-employee-id'] as string || scbId;
      const result = await scbAdminService.cbdcControls.updateGRUPolicy(
        employeeId,
        scbId,
        req.body
      );
      return res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

export default router;

