// Unified Holographic Economic Model API Routes

import { Router } from 'express';
import { uhemEncodingService } from './uhem-encoding.service';
import { uhemProjectionService } from './uhem-projection.service';
import { uhemCorrectionService } from './uhem-correction.service';
import { uhemAnalyticsService } from './uhem-analytics.service';

const router = Router();

/**
 * @swagger
 * /api/v1/uhem/states:
 *   post:
 *     summary: Encode holographic economic state
 */
router.post('/states', async (req, res, next) => {
  try {
    const state = await uhemEncodingService.encodeState(req.body);
    res.status(201).json(state);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/uhem/states/latest:
 *   get:
 *     summary: Get latest state
 */
router.get('/states/latest', async (req, res, next) => {
  try {
    const state = await uhemEncodingService.getLatestState();
    if (!state) {
      return res.status(404).json({ error: 'No state found' });
    }
    res.json(state);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/uhem/projections:
 *   post:
 *     summary: Create projection
 */
router.post('/projections', async (req, res, next) => {
  try {
    const projection = await uhemProjectionService.createProjection(req.body);
    res.status(201).json(projection);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/uhem/projections/forward:
 *   post:
 *     summary: Perform forward projection
 */
router.post('/projections/forward', async (req, res, next) => {
  try {
    const { stateId, targetReality } = req.body;
    const projection = await uhemProjectionService.forwardProject(stateId, targetReality);
    res.json(projection);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/uhem/corrections:
 *   post:
 *     summary: Create correction
 */
router.post('/corrections', async (req, res, next) => {
  try {
    const correction = await uhemCorrectionService.createCorrection(req.body);
    res.status(201).json(correction);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/uhem/corrections/:correctionId/apply:
 *   post:
 *     summary: Apply correction
 */
router.post('/corrections/:correctionId/apply', async (req, res, next) => {
  try {
    const correction = await uhemCorrectionService.applyCorrection(req.params.correctionId);
    res.json(correction);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/uhem/analytics/trends:
 *   get:
 *     summary: Analyze trends
 */
router.get('/analytics/trends', async (req, res, next) => {
  try {
    const timeRange = req.query.start && req.query.end
      ? { start: new Date(req.query.start as string), end: new Date(req.query.end as string) }
      : undefined;
    const trends = await uhemAnalyticsService.analyzeTrends(timeRange);
    res.json(trends);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/uhem/analytics/accuracy:
 *   get:
 *     summary: Get projection accuracy statistics
 */
router.get('/analytics/accuracy', async (req, res, next) => {
  try {
    const stats = await uhemAnalyticsService.getProjectionAccuracy();
    res.json(stats);
  } catch (error) {
      return next(error);
  }
});

export default router;

