// D-SEZ (Digital Sovereign Economic Zone) API Routes
// Supplement B: MetaverseDubai Integration Framework

import { Router } from 'express';
import { dsezService } from './d-sez.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';

const router = Router();

/**
 * @route POST /api/metaverse/d-sez
 * @desc Create Digital Sovereign Economic Zone
 */
router.post(
  '/d-sez',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await dsezService.createDsez(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/metaverse/d-sez/:dsezId
 * @desc Get D-SEZ by ID
 */
router.get(
  '/d-sez/:dsezId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const dsez = await dsezService.getDsez(req.params.dsezId);
      if (!dsez) {
        return res.status(404).json({ error: 'D-SEZ not found' });
      }
      res.json(dsez);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/metaverse/d-sez
 * @desc Get all active D-SEZs
 */
router.get(
  '/d-sez',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const dsezs = await dsezService.getAllDsezs();
      res.json(dsezs);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/metaverse/d-sez/node/:nodeId
 * @desc Get D-SEZ by metaverse node ID
 */
router.get(
  '/d-sez/node/:nodeId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const dsez = await dsezService.getDsezByNode(req.params.nodeId);
      if (!dsez) {
        return res.status(404).json({ error: 'D-SEZ not found for node' });
      }
      res.json(dsez);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route PATCH /api/metaverse/d-sez/:dsezId/config
 * @desc Update D-SEZ configuration
 */
router.patch(
  '/d-sez/:dsezId/config',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await dsezService.updateDsezConfig(
        req.params.dsezId,
        req.body
      );
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/d-sez/:dsezId/suspend
 * @desc Suspend D-SEZ
 */
router.post(
  '/d-sez/:dsezId/suspend',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      await dsezService.suspendDsez(req.params.dsezId);
      res.json({ message: 'D-SEZ suspended' });
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/metaverse/d-sez/:dsezId/activate
 * @desc Activate D-SEZ
 */
router.post(
  '/d-sez/:dsezId/activate',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      await dsezService.activateDsez(req.params.dsezId);
      res.json({ message: 'D-SEZ activated' });
    } catch (error) {
      return next(error);
    }
  }
);

export default router;

