// FACE API Routes

import { Router } from 'express';
import { faceEconomyService } from './face-economy.service';
import { faceBehavioralService } from './face-behavioral.service';
import { faceSupplyService } from './face-supply.service';
import { faceStabilizationService } from './face-stabilization.service';
import { faceIncentiveService } from './face-incentive.service';

const router = Router();

// Economy routes
router.post('/economies', async (req, res, next) => {
  try {
    const economy = await faceEconomyService.createEconomy(req.body);
    res.json(economy);
  } catch (error) {
      return next(error);
  }
});

router.get('/economies', async (req, res, next) => {
  try {
    const economies = await faceEconomyService.getEconomiesForBank(req.query.sovereignBankId as string);
    res.json(economies);
  } catch (error) {
      return next(error);
  }
});

router.get('/economies/:economyId', async (req, res, next) => {
  try {
    const economy = await faceEconomyService.getEconomy(req.params.economyId);
    res.json(economy);
  } catch (error) {
      return next(error);
  }
});

// Behavioral engine routes
router.post('/behavioral', async (req, res, next) => {
  try {
    const engine = await faceBehavioralService.createBehavioralEngine(req.body);
    res.json(engine);
  } catch (error) {
      return next(error);
  }
});

router.get('/behavioral/:economyId', async (req, res, next) => {
  try {
    const engine = await faceBehavioralService.getBehavioralEngine(req.params.economyId);
    res.json(engine);
  } catch (error) {
      return next(error);
  }
});

router.post('/behavioral/:economyId/analyze', async (req, res, next) => {
  try {
    const analysis = await faceBehavioralService.analyzeBehavior(req.params.economyId, req.body);
    res.json(analysis);
  } catch (error) {
      return next(error);
  }
});

// Supply contract routes
router.post('/supply', async (req, res, next) => {
  try {
    const contract = await faceSupplyService.createSupplyContract(req.body);
    res.json(contract);
  } catch (error) {
      return next(error);
  }
});

router.get('/supply/:economyId', async (req, res, next) => {
  try {
    const contracts = await faceSupplyService.getContractsForEconomy(req.params.economyId);
    res.json(contracts);
  } catch (error) {
      return next(error);
  }
});

router.post('/supply/:contractId/check', async (req, res, next) => {
  try {
    const result = await faceSupplyService.checkSupplyContract(req.params.contractId, req.body.currentVelocity);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

// Stabilization contract routes
router.post('/stabilization', async (req, res, next) => {
  try {
    const contract = await faceStabilizationService.createStabilizationContract(req.body);
    res.json(contract);
  } catch (error) {
      return next(error);
  }
});

router.get('/stabilization/:economyId', async (req, res, next) => {
  try {
    const contracts = await faceStabilizationService.getContractsForEconomy(req.params.economyId);
    res.json(contracts);
  } catch (error) {
      return next(error);
  }
});

router.post('/stabilization/:contractId/check', async (req, res, next) => {
  try {
    const result = await faceStabilizationService.checkStabilizationContract(req.params.contractId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

// Incentive routes
router.post('/incentives', async (req, res, next) => {
  try {
    const incentive = await faceIncentiveService.createIncentive(req.body);
    res.json(incentive);
  } catch (error) {
      return next(error);
  }
});

router.get('/incentives/:economyId', async (req, res, next) => {
  try {
    const incentives = await faceIncentiveService.getIncentivesForEconomy(req.params.economyId);
    res.json(incentives);
  } catch (error) {
      return next(error);
  }
});

router.post('/incentives/:incentiveId/apply', async (req, res, next) => {
  try {
    const result = await faceIncentiveService.checkAndApplyIncentive(req.params.incentiveId, req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

