/**
 * One-shot runner for FeatureEnrichmentTask.
 *
 * Usage:
 *   npx ts-node -r tsconfig-paths/register src/scripts/run-feature-enrichment.ts
 *
 * Backfills football_ai_features rows for all FT football matches in the last
 * 60 days that lack one. Safe to re-run — idempotent via ON CONFLICT DO NOTHING.
 */

import { NestFactory } from "@nestjs/core";
import { Logger } from "@nestjs/common";
import { Module } from "@nestjs/common";
import { DatabaseModule } from "../database/database.module";
import { FeatureEnrichmentTask } from "../tasks/feature-enrichment.task";
import { TaskLockService } from "../tasks/task-lock.service";

@Module({
  imports: [DatabaseModule],
  providers: [FeatureEnrichmentTask, TaskLockService],
})
class FeatureEnrichmentRunnerModule {}

async function main() {
  const logger = new Logger("FeatureEnrichmentRunner");
  const app = await NestFactory.createApplicationContext(
    FeatureEnrichmentRunnerModule,
    { logger: ["log", "warn", "error"] },
  );

  const task = app.get(FeatureEnrichmentTask);
  logger.log("Starting one-shot feature enrichment backfill...");
  const started = Date.now();
  const result = await task.runEnrichment();
  const elapsed = Date.now() - started;
  logger.log(
    `Done in ${elapsed}ms. inserted=${result.inserted} repaired=${result.repaired} movement=${result.movementUpdated}`,
  );

  await app.close();
}

main().catch((err) => {
  // eslint-disable-next-line no-console
  console.error(err);
  process.exit(1);
});
