// DBIS Conscious-Ledger Integration Model API Routes

import { Router } from 'express';
import { climIntegrationService } from './clim-integration.service';
import { climContractService } from './clim-contract.service';
import { climAnalyticsService } from './clim-analytics.service';

const router = Router();

/**
 * @swagger
 * /api/v1/clim/state:
 *   post:
 *     summary: Hash consciousness state
 */
router.post('/state', async (req, res, next) => {
  try {
    const state = await climIntegrationService.hashConsciousnessState(req.body);
    res.status(201).json(state);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/clim/state/:stateId:
 *   get:
 *     summary: Get consciousness state by ID
 */
router.get('/state/:stateId', async (req, res, next) => {
  try {
    const state = await climIntegrationService.getConsciousnessState(req.params.stateId);
    if (!state) {
      return res.status(404).json({ error: 'State not found' });
    }
    res.json(state);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/clim/state/agent/:agentId:
 *   get:
 *     summary: Get consciousness states for agent
 */
router.get('/state/agent/:agentId', async (req, res, next) => {
  try {
    const states = await climIntegrationService.getConsciousnessStatesForAgent(req.params.agentId);
    res.json(states);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/clim/contract:
 *   post:
 *     summary: Create cognitive smart contract
 */
router.post('/contract', async (req, res, next) => {
  try {
    const contract = await climContractService.createCognitiveContract(req.body);
    res.status(201).json(contract);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/clim/contract/:contractId:
 *   get:
 *     summary: Get contract by ID
 */
router.get('/contract/:contractId', async (req, res, next) => {
  try {
    const contract = await climContractService.getContract(req.params.contractId);
    if (!contract) {
      return res.status(404).json({ error: 'Contract not found' });
    }
    res.json(contract);
  } catch (error) {
      return next(error);
  }
});

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

/**
 * @swagger
 * /api/v1/clim/contract/pending:
 *   get:
 *     summary: Get pending execution contracts
 */
router.get('/contract/pending', async (req, res, next) => {
  try {
    const contracts = await climContractService.getPendingExecutionContracts();
    res.json(contracts);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/clim/analytics/:agentId:
 *   get:
 *     summary: Get behavioral analytics for agent
 */
router.get('/analytics/:agentId', async (req, res, next) => {
  try {
    const analytics = await climAnalyticsService.getBehavioralAnalytics(req.params.agentId);
    res.json(analytics);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/clim/analytics/aggregate:
 *   get:
 *     summary: Get aggregate analytics across all agents
 */
router.get('/analytics/aggregate', async (req, res, next) => {
  try {
    const analytics = await climAnalyticsService.getAggregateAnalytics();
    res.json(analytics);
  } catch (error) {
      return next(error);
  }
});

export default router;

