/**
 * Test script for the auto-renewal cron job.
 *
 * Usage:
 *   node scripts/testRenewalCron.js
 *
 * This will immediately run the full cron logic:
 *   - Renew if last invoice is 'paid' (or no invoice exists) → generate new invoice + advance next_billing_at
 *   - Skip if last invoice is NOT paid → just advance next_billing_at, no new invoice
 *   - Skip if gift-abo without registered user (user_id NULL) → don't advance
 *
 * Test setup (run in MySQL/phpMyAdmin):
 *
 *   -- 1. Make a subscription due for billing:
 *   UPDATE coffee_abonements SET next_billing_at = NOW() - INTERVAL 1 DAY WHERE id = <ID>;
 *
 *   -- 2. To test "paid" flow: mark last invoice as paid:
 *   UPDATE invoices SET status = 'paid' WHERE source_type = 'subscription' AND source_id = <ABO_ID> ORDER BY id DESC LIMIT 1;
 *
 *   -- 3. To test "unpaid" flow: leave invoice status as 'issued'
 *
 *   -- 4. To test gift skip: set user_id to NULL:
 *   UPDATE coffee_abonements SET user_id = NULL WHERE id = <ID>;
 */
require('dotenv').config();
const RenewalCronService = require('../services/abonemments/RenewalCronService');

(async () => {
  console.log('=== Renewal Cron Test ===');
  console.log('Time:', new Date().toISOString());
  console.log('Day of month:', new Date().getDate(), '(reactivation before 11th?', new Date().getDate() <= 10, ')');
  console.log('');

  const cron = new RenewalCronService();

  try {
    await cron.processDueRenewals();
    console.log('');
    console.log('=== Test complete ===');
  } catch (err) {
    console.error('Test failed:', err);
  }

  setTimeout(() => process.exit(0), 3000);
})();
