// IRU Deployment API Routes
// Routes for one-click deployment

import { Router } from 'express';
import { z } from 'zod';
import { deploymentOrchestrator } from '@/core/iru/deployment/deployment-orchestrator.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { validateRequest, iruValidationSchemas } from '@/integration/api-gateway/middleware/validation.middleware';

const router = Router();

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

/**
 * @route POST /api/v1/iru/deployment/initiate
 * @desc Initiate IRU deployment
 * @access Authenticated
 */
router.post(
  '/initiate',
  validateRequest({ body: iruValidationSchemas.deploymentRequest }),
  async (req, res, next) => {
    try {
      const { subscriptionId, deploymentConfig } = req.body;
      const status = await deploymentOrchestrator.initiateDeployment({
        subscriptionId,
        deploymentConfig,
      });
      res.status(202).json({
        success: true,
        data: status,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

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

/**
 * @route POST /api/v1/iru/deployment/:deploymentId/cancel
 * @desc Cancel deployment
 * @access Authenticated
 */
router.post('/:deploymentId/cancel', async (req, res, next) => {
  try {
    const { deploymentId } = req.params;
    await deploymentOrchestrator.cancelDeployment(deploymentId);
    res.json({
      success: true,
      message: `Deployment ${deploymentId} cancelled`,
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

export default router;
