// GPU Edge API Routes

import { Router } from 'express';
import { gpuEdgeNodeService } from './gpu-edge-node.service';
import { gpuEdgeDeploymentService } from './gpu-edge-deployment.service';
import { gpuEdgeRoutingService } from './gpu-edge-routing.service';
import { gpuEdgeMonitoringService } from './gpu-edge-monitoring.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';

const router = Router();

/**
 * @route POST /api/gpu-edge/node
 * @desc Create GPU edge node
 */
router.post(
  '/node',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await gpuEdgeNodeService.createGpuEdgeNode(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gpu-edge/node/:nodeId
 * @desc Get node by ID
 */
router.get(
  '/node/:nodeId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const node = await gpuEdgeNodeService.getNode(req.params.nodeId);
      if (!node) {
        return res.status(404).json({ error: 'Node not found' });
      }
      res.json(node);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/gpu-edge/deploy
 * @desc Deploy GPU edge nodes to region
 */
router.post(
  '/deploy',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await gpuEdgeDeploymentService.deployToRegion(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gpu-edge/regions
 * @desc Get all active regions
 */
router.get(
  '/regions',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const regions = await gpuEdgeDeploymentService.getAllActiveRegions();
      res.json(regions);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/gpu-edge/route
 * @desc Calculate optimal 6G route
 */
router.post(
  '/route',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await gpuEdgeRoutingService.calculateOptimalRoute(req.body);
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/gpu-edge/monitor/health/:nodeId
 * @desc Perform health check on node
 */
router.post(
  '/monitor/health/:nodeId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const result = await gpuEdgeMonitoringService.performHealthCheck(
        req.params.nodeId
      );
      res.json(result);
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route GET /api/gpu-edge/monitor/metrics/node/:nodeId
 * @desc Get monitoring metrics for node
 */
router.get(
  '/monitor/metrics/node/:nodeId',
  zeroTrustAuthMiddleware,
  async (req, res, next) => {
    try {
      const metrics = await gpuEdgeMonitoringService.getNodeMetrics(
        req.params.nodeId
      );
      res.json(metrics);
    } catch (error) {
      return next(error);
    }
  }
);

export default router;

