/**
 * Run Previous-Day Completed Match Sync
 * Usage: npm run feeder:previous-day
 */

import { NestFactory } from "@nestjs/core";
import { FeederService } from "../modules/feeder/feeder.service";
import { Logger } from "@nestjs/common";

async function bootstrap() {
  process.env.FEEDER_MODE = "historical";

  const logger = new Logger("FeederPreviousDayScript");

  logger.log("🚀 Starting previous-day completed match sync...");

  // Load AppModule after FEEDER_MODE is set so cron imports can be disabled.
  // eslint-disable-next-line @typescript-eslint/no-require-imports
  const { AppModule } = require("../app.module");
  const app = await NestFactory.createApplicationContext(AppModule, {
    logger: ["log", "error", "warn"],
  });

  try {
    const feederService = app.get(FeederService);
    await feederService.runPreviousDayCompletedMatchesScan();
    logger.log("✅ Previous-day completed match sync completed successfully!");
  } catch (error: any) {
    logger.error(`❌ Feeder failed: ${error.message}`);
    logger.error(error.stack);
    process.exit(1);
  } finally {
    await app.close();
  }

  process.exit(0);
}

void bootstrap();
