// IRU Portal API Routes
// Routes for Phoenix Portal

import { Router } from 'express';
import { portalService } from '@/core/iru/portal.service';
import { monitoringService } from '@/core/iru/monitoring.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';

const router = Router();

// All portal routes require authentication
router.use(zeroTrustAuthMiddleware);

/**
 * @route GET /api/v1/iru/portal/dashboard
 * @desc Get participant dashboard
 * @access Authenticated
 */
router.get('/dashboard', async (req, res, next) => {
  try {
    // TODO: Get participantId from auth token
    const participantId = (req as any).user?.participantId || (req as any).user?.id;
    
    if (!participantId) {
      return res.status(401).json({
        success: false,
        error: {
          code: 'UNAUTHORIZED',
          message: 'Participant ID not found in token',
        },
        timestamp: new Date(),
      });
    }

    const dashboard = await portalService.getDashboard(participantId);
    res.json({
      success: true,
      data: dashboard,
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

/**
 * @route GET /api/v1/iru/portal/iru-management
 * @desc Get IRU management data
 * @access Authenticated
 */
router.get('/iru-management', async (req, res, next) => {
  try {
    const participantId = (req as any).user?.participantId || (req as any).user?.id;
    
    if (!participantId) {
      return res.status(401).json({
        success: false,
        error: {
          code: 'UNAUTHORIZED',
          message: 'Participant ID not found in token',
        },
        timestamp: new Date(),
      });
    }

    const management = await portalService.getIRUManagement(participantId);
    res.json({
      success: true,
      data: management,
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

/**
 * @route GET /api/v1/iru/portal/deployment/:subscriptionId
 * @desc Get deployment status
 * @access Authenticated
 */
router.get('/deployment/:subscriptionId', async (req, res, next) => {
  try {
    const { subscriptionId } = req.params;
    const status = await portalService.getDeploymentStatus(subscriptionId);
    res.json({
      success: true,
      data: status,
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

/**
 * @route GET /api/v1/iru/portal/monitoring/:subscriptionId/health
 * @desc Get service health
 * @access Authenticated
 */
router.get('/monitoring/:subscriptionId/health', async (req, res, next) => {
  try {
    const { subscriptionId } = req.params;
    const health = await monitoringService.getServiceHealth(subscriptionId);
    res.json({
      success: true,
      data: health,
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

/**
 * @route GET /api/v1/iru/portal/monitoring/:subscriptionId/metrics
 * @desc Get performance metrics
 * @access Authenticated
 */
router.get('/monitoring/:subscriptionId/metrics', async (req, res, next) => {
  try {
    const { subscriptionId } = req.params;
    const timeRange = req.query.timeRange as string;
    const metrics = await monitoringService.getPerformanceMetrics(subscriptionId, timeRange);
    res.json({
      success: true,
      data: metrics,
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

export default router;
