// IRU Payment API Routes
// Routes for payment processing

import { Router } from 'express';
import { paymentProcessorService } from '@/core/iru/payment/payment-processor.service';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { validateRequest, iruValidationSchemas } from '@/integration/api-gateway/middleware/validation.middleware';

const router = Router();

// All payment routes require authentication
router.use(zeroTrustAuthMiddleware);

/**
 * @route POST /api/v1/iru/payment/process
 * @desc Process payment for IRU subscription
 * @access Authenticated
 */
router.post(
  '/process',
  validateRequest({ body: iruValidationSchemas.paymentRequest }),
  async (req, res, next) => {
    try {
      const result = await paymentProcessorService.processPayment(req.body);
      res.json({
        success: result.success,
        data: result,
        timestamp: new Date(),
      });
    } catch (error) {
      return next(error);
    }
  }
);

/**
 * @route POST /api/v1/iru/payment/webhook/stripe
 * @desc Handle Stripe webhook
 * @access Public (verified by signature)
 */
router.post('/webhook/stripe', async (req, res, next) => {
  try {
    const signature = req.headers['stripe-signature'] as string;
    await paymentProcessorService.handleWebhook('stripe', req.body, signature);
    res.json({
      success: true,
      message: 'Webhook processed',
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

/**
 * @route POST /api/v1/iru/payment/webhook/braintree
 * @desc Handle Braintree webhook
 * @access Public (verified by signature)
 */
router.post('/webhook/braintree', async (req, res, next) => {
  try {
    const signature = req.headers['bt-signature'] as string;
    await paymentProcessorService.handleWebhook('braintree', req.body, signature);
    res.json({
      success: true,
      message: 'Webhook processed',
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

export default router;
