// Reality-Spanning Smart Contract Kernel (RSSCK) API Routes

import { Router } from 'express';
import { rssckService } from './rssck.service';

const router = Router();

/**
 * @swagger
 * /api/v1/rssck/contracts:
 *   post:
 *     summary: Create reality-spanning contract
 */
router.post('/contracts', async (req, res, next) => {
  try {
    const result = await rssckService.createRealitySpanningContract(req.body);
    return res.status(201).json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/rssck/contracts/:contractId:
 *   get:
 *     summary: Get reality-spanning contract
 */
router.get('/contracts/:contractId', async (req, res, next) => {
  try {
    const contract = await rssckService.getRealitySpanningContract(req.params.contractId);
    if (!contract) {
      return res.status(404).json({ error: 'Contract not found' });
    }
    return res.json(contract);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/rssck/contracts/:contractId/execute:
 *   post:
 *     summary: Execute contract
 */
router.post('/contracts/:contractId/execute', async (req, res, next) => {
  try {
    const result = await rssckService.executeContract({
      contractId: req.params.contractId,
      ...req.body,
    });
    return res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/rssck/contracts/:contractId/resolve:
 *   post:
 *     summary: Resolve contract conflict
 */
router.post('/contracts/:contractId/resolve', async (req, res, next) => {
  try {
    const result = await rssckService.resolveContractConflict(
      req.params.contractId,
      req.body.resolutionType,
      req.body.resolutionResult
    );
    return res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

