// ASSS API Routes

import { Router } from 'express';
import { asssSimulationService } from './asss-simulation.service';
import { asssModelService } from './asss-model.service';
import { asssScenarioService } from './asss-scenario.service';

const router = Router();

/**
 * @swagger
 * /api/v1/asss/simulation:
 *   post:
 *     summary: Create and start simulation
 */
router.post('/simulation', async (req, res, next) => {
  try {
    const result = await asssSimulationService.createSimulation(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/asss/simulation/:simulationId:
 *   get:
 *     summary: Get simulation
 */
router.get('/simulation/:simulationId', async (req, res, next) => {
  try {
    const simulation = await asssSimulationService.getSimulation(req.params.simulationId);
    if (!simulation) {
      return res.status(404).json({ error: 'Simulation not found' });
    }
    res.json(simulation);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/asss/simulation/:simulationId/complete:
 *   post:
 *     summary: Complete simulation
 */
router.post('/simulation/:simulationId/complete', async (req, res, next) => {
  try {
    await asssSimulationService.completeSimulation(req.params.simulationId);
    res.json({ status: 'completed' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/asss/simulations:
 *   get:
 *     summary: List simulations
 */
router.get('/simulations', async (req, res, next) => {
  try {
    const { simulationType } = req.query;
    const simulations = await asssSimulationService.listSimulations(
      simulationType as string | undefined
    );
    res.json(simulations);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/asss/model/:simulationId:
 *   post:
 *     summary: Execute model
 */
router.post('/model/:simulationId', async (req, res, next) => {
  try {
    const output = await asssModelService.executeModel(req.params.simulationId, req.body);
    res.json(output);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/asss/outcomes/:simulationId:
 *   get:
 *     summary: Get outcomes for simulation
 */
router.get('/outcomes/:simulationId', async (req, res, next) => {
  try {
    const outcomes = await asssModelService.getOutcomes(req.params.simulationId);
    res.json(outcomes);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/asss/scenario:
 *   post:
 *     summary: Create scenario
 */
router.post('/scenario', async (req, res, next) => {
  try {
    const result = await asssScenarioService.createScenario(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/asss/scenario/:scenarioId:
 *   get:
 *     summary: Get scenario
 */
router.get('/scenario/:scenarioId', async (req, res, next) => {
  try {
    const scenario = await asssScenarioService.getScenario(req.params.scenarioId);
    if (!scenario) {
      return res.status(404).json({ error: 'Scenario not found' });
    }
    res.json(scenario);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/asss/scenarios:
 *   get:
 *     summary: List scenarios
 */
router.get('/scenarios', async (req, res, next) => {
  try {
    const { scenarioType, stressLevel } = req.query;
    const scenarios = await asssScenarioService.listScenarios(
      scenarioType as string | undefined,
      stressLevel as string | undefined
    );
    res.json(scenarios);
  } catch (error) {
      return next(error);
  }
});

export default router;

