import * as dotenv from 'dotenv';

const envFilePath =
  process.env.NODE_ENV == 'production'
    ? '.env'
    : `.env.${process.env.NODE_ENV || 'development'}`;
dotenv.config({ path: envFilePath });

import { NestFactory } from '@nestjs/core';
import { AppModule } from '../app.module';
import { CampaignService } from '../modules/campaign/campaign.services';

/**
 * One-off seeder: force-offline campaign payments for brands with
 * offline_payment_allowed = 'forced'.
 *
 * - Converts existing unpaid / offline payments to force_offline
 * - Creates missing force_offline payment rows for closed jobs
 *
 * Run: npm run seed:force-offline-payments
 * (build first: npm run build)
 */
async function bootstrap() {
  const app = await NestFactory.createApplicationContext(AppModule, {
    logger: ['error', 'warn', 'log'],
  });

  try {
    const campaignService = app.get(CampaignService);
    console.log("Starting force-offline payment backfill for brands with offline_payment_allowed = 'forced'...");
    const result = await campaignService.forceOfflinePaymentsForForcedBrands();
    console.log('Force-offline payment backfill completed:', result);
  } catch (error) {
    console.error('Force-offline payment seeder failed:', error);
    process.exitCode = 1;
  } finally {
    await app.close();
  }
}

bootstrap();
