const { processPayoutRetries } = require("./handler");
const { logger } = require("../shared");

/**
 * Lambda handler for payout retry processing.
 *
 * Invoked by EventBridge Scheduler on a daily schedule (7 AM EST).
 * No event payload required - processes all failed payouts.
 *
 * @param {Object} event - EventBridge Scheduler event (unused)
 * @returns {Promise<Object>} Result of the retry processing
 */
exports.handler = async (event) => {
  logger.info("Lambda invoked", {
    event,
    invocationTime: new Date().toISOString(),
  });

  try {
    const result = await processPayoutRetries();

    logger.info("Payout retry processing completed", {
      totalProcessed: result.totalProcessed,
      successful: result.successfulCount,
      failed: result.failedCount,
    });

    return {
      statusCode: 200,
      body: JSON.stringify(result),
    };
  } catch (error) {
    logger.error("Payout retry processing failed", {
      error: error.message,
      stack: error.stack,
    });

    return {
      statusCode: 500,
      body: JSON.stringify({
        success: false,
        error: error.message,
      }),
    };
  }
};
