// SCI API Routes

import { Router } from 'express';
import { sciZoneManagerService } from './sci-zone-manager.service';
import { sciReplicationService } from './sci-replication.service';
import { sciSevmService } from './sci-sevm.service';
import { sciSecurityService } from './sci-security.service';

const router = Router();

/**
 * @swagger
 * /api/v1/sci/zone/create:
 *   post:
 *     summary: Create Sovereign Compute Zone
 */
router.post('/zone/create', async (req, res, next) => {
  try {
    const result = await sciZoneManagerService.createZone(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sci/replication/start:
 *   post:
 *     summary: Start replication
 */
router.post('/replication/start', async (req, res, next) => {
  try {
    const result = await sciReplicationService.startReplication(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sci/sevm/deploy:
 *   post:
 *     summary: Deploy SEVM contract
 */
router.post('/sevm/deploy', async (req, res, next) => {
  try {
    const result = await sciSevmService.deployContract(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sci/security/attestation:
 *   post:
 *     summary: Create security attestation
 */
router.post('/security/attestation', async (req, res, next) => {
  try {
    const { type, ...params } = req.body;
    let result;

    switch (type) {
      case 'tpm':
        result = await sciSecurityService.createTpmAttestation(params);
        break;
      case 'integrity':
        result = await sciSecurityService.createContinuousIntegrityAttestation(params);
        break;
      default:
        throw new Error(`Unknown attestation type: ${type}`);
    }

    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

