/**
 * @swagger
 * tags:
 *   name: SolaceNet Payment Gateway
 *   description: Payment gateway operations
 */

import { Router } from 'express';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { paymentGatewayService, PaymentIntentRequest } from './payment-gateway.service';

const router = Router();

router.post('/intents', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const request = req.body as PaymentIntentRequest;
    const intent = await paymentGatewayService.createPaymentIntent(request);
    res.status(201).json(intent);
  } catch (error) {
    next(error);
  }
});

router.post('/intents/:id/capture', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const { id } = req.params;
    const tenantId = req.headers['x-tenant-id'] as string;
    await paymentGatewayService.capturePayment(id, tenantId);
    res.json({ success: true });
  } catch (error) {
    next(error);
  }
});

router.post('/refunds', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const { paymentId, amount } = req.body;
    const tenantId = req.headers['x-tenant-id'] as string;
    await paymentGatewayService.refundPayment(paymentId, amount, tenantId);
    res.json({ success: true });
  } catch (error) {
    next(error);
  }
});

export default router;
