// SNFN API Routes

import { Router } from 'express';
import { snfnNodeService } from './snfn-node.service';
import { snfnLoanService } from './snfn-loan.service';
import { snfnSettlementService } from './snfn-settlement.service';

const router = Router();

/**
 * @swagger
 * /api/v1/snfn/node:
 *   post:
 *     summary: Create supra fund node
 */
router.post('/node', async (req, res, next) => {
  try {
    const result = await snfnNodeService.createNode(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/snfn/node/:nodeId:
 *   get:
 *     summary: Get supra fund node
 */
router.get('/node/:nodeId', async (req, res, next) => {
  try {
    const node = await snfnNodeService.getNode(req.params.nodeId);
    if (!node) {
      return res.status(404).json({ error: 'Node not found' });
    }
    res.json(node);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/snfn/node/dfn:
 *   post:
 *     summary: Create development fund node
 */
router.post('/node/dfn', async (req, res, next) => {
  try {
    await snfnNodeService.createDFN(req.body);
    res.json({ status: 'created' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/snfn/node/csn:
 *   post:
 *     summary: Create crisis stabilization node
 */
router.post('/node/csn', async (req, res, next) => {
  try {
    await snfnNodeService.createCSN(req.body);
    res.json({ status: 'created' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/snfn/loan/eligibility/:borrowerBankId:
 *   get:
 *     summary: Calculate loan eligibility
 */
router.get('/loan/eligibility/:borrowerBankId', async (req, res, next) => {
  try {
    const eligibility = await snfnLoanService.calculateLoanEligibility(req.params.borrowerBankId);
    res.json({ loanEligibility: eligibility.toString() });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/snfn/loan:
 *   post:
 *     summary: Create loan request
 */
router.post('/loan', async (req, res, next) => {
  try {
    const result = await snfnLoanService.createLoan(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/snfn/loan/:loanId:
 *   get:
 *     summary: Get loan
 */
router.get('/loan/:loanId', async (req, res, next) => {
  try {
    const loan = await snfnLoanService.getLoan(req.params.loanId);
    if (!loan) {
      return res.status(404).json({ error: 'Loan not found' });
    }
    res.json(loan);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/snfn/settlement:
 *   post:
 *     summary: Disburse funds
 */
router.post('/settlement', async (req, res, next) => {
  try {
    const result = await snfnSettlementService.disburseFunds(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/snfn/settlement/:settlementId/finalize:
 *   post:
 *     summary: Finalize settlement
 */
router.post('/settlement/:settlementId/finalize', async (req, res, next) => {
  try {
    await snfnSettlementService.finalizeSettlement(req.params.settlementId);
    res.json({ status: 'finalized' });
  } catch (error) {
      return next(error);
  }
});

export default router;

