// UMAP API Routes

import { Router } from 'express';
import { umbService } from './umb.service';
import { gpeService } from './gpe.service';
import { acxService } from './acx.service';
import { driftCorrectionService } from './drift-correction.service';

const router = Router();

// UMB routes
router.post('/baseline', async (req, res, next) => {
  try {
    const baseline = await umbService.createBaseline(req.body);
    res.json(baseline);
  } catch (error) {
      return next(error);
  }
});

router.get('/baseline', async (req, res, next) => {
  try {
    const baseline = await umbService.getActiveBaseline();
    res.json(baseline);
  } catch (error) {
      return next(error);
  }
});

router.get('/baseline/all', async (req, res, next) => {
  try {
    const baselines = await umbService.getAllBaselines();
    res.json(baselines);
  } catch (error) {
      return next(error);
  }
});

router.get('/baseline/:umbId', async (req, res, next) => {
  try {
    const baseline = await umbService.getBaseline(req.params.umbId);
    res.json(baseline);
  } catch (error) {
      return next(error);
  }
});

// GPE routes
router.post('/parity', async (req, res, next) => {
  try {
    const parity = await gpeService.calculateParity(req.body);
    res.json(parity);
  } catch (error) {
      return next(error);
  }
});

router.get('/parity', async (req, res, next) => {
  try {
    const parities = await gpeService.getAllParities(req.query);
    res.json(parities);
  } catch (error) {
      return next(error);
  }
});

router.get('/parity/:parityId', async (req, res, next) => {
  try {
    const parity = await gpeService.getParity(req.params.parityId);
    res.json(parity);
  } catch (error) {
      return next(error);
  }
});

router.get('/parity/currency/:currencyCode/:assetType', async (req, res, next) => {
  try {
    const parity = await gpeService.getParityForCurrency(
      req.params.currencyCode,
      req.params.assetType
    );
    res.json(parity);
  } catch (error) {
      return next(error);
  }
});

router.post('/parity/:parityId/recalculate', async (req, res, next) => {
  try {
    const parity = await gpeService.recalculateParity(req.params.parityId, req.body);
    res.json(parity);
  } catch (error) {
      return next(error);
  }
});

// ACX routes
router.post('/contracts', async (req, res, next) => {
  try {
    const contract = await acxService.createContract(req.body);
    res.json(contract);
  } catch (error) {
      return next(error);
  }
});

router.get('/contracts', async (req, res, next) => {
  try {
    const contracts = await acxService.getAllContracts(req.query);
    res.json(contracts);
  } catch (error) {
      return next(error);
  }
});

router.get('/contracts/:contractId', async (req, res, next) => {
  try {
    const contract = await acxService.getContract(req.params.contractId);
    res.json(contract);
  } catch (error) {
      return next(error);
  }
});

router.post('/contracts/:contractId/check', async (req, res, next) => {
  try {
    const result = await acxService.checkContract(req.params.contractId, req.body.currentValue);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

// Drift correction routes
router.post('/drift', async (req, res, next) => {
  try {
    const correction = await driftCorrectionService.createCorrection(req.body);
    res.json(correction);
  } catch (error) {
      return next(error);
  }
});

router.post('/drift/detect', async (req, res, next) => {
  try {
    const correction = await driftCorrectionService.detectAndCorrectDrift(
      req.body.currencyCode,
      req.body.assetType,
      req.body.currentValue,
      req.body.targetValue
    );
    res.json(correction);
  } catch (error) {
      return next(error);
  }
});

router.post('/drift/:correctionId/apply', async (req, res, next) => {
  try {
    const correction = await driftCorrectionService.applyCorrection(req.params.correctionId);
    res.json(correction);
  } catch (error) {
      return next(error);
  }
});

router.get('/drift', async (req, res, next) => {
  try {
    const corrections = await driftCorrectionService.getCorrections(req.query);
    res.json(corrections);
  } catch (error) {
      return next(error);
  }
});

router.get('/drift/:correctionId', async (req, res, next) => {
  try {
    const correction = await driftCorrectionService.getCorrection(req.params.correctionId);
    res.json(correction);
  } catch (error) {
      return next(error);
  }
});

export default router;

