/**
 * Manually run the date-driven booking status sync (same logic as the
 * daily 00:10 UTC cron).
 *
 * Usage:
 *   npm run booking:sync-status
 *   npx ts-node src/cli/sync-booking-status.ts
 */
import { NestFactory } from '@nestjs/core';
import { AppModule } from '../app.module';
import { BookingStatusSyncService } from '../modules/booking/services/booking-status-sync.service';

async function main(): Promise<void> {
  const app = await NestFactory.createApplicationContext(AppModule, {
    logger: ['error', 'log', 'warn'],
  });

  try {
    const service = app.get(BookingStatusSyncService);
    const result = await service.syncAllTenants();
    console.log(`Booking status sync complete (${result.today} UTC)`);
    console.log(`  tenants processed: ${result.tenantsProcessed}`);
    console.log(`  set to upcoming:   ${result.upcomingUpdated}`);
    console.log(`  set to ongoing:    ${result.ongoingUpdated}`);
    console.log(`  set to completed:  ${result.completedUpdated}`);
  } catch (err: unknown) {
    console.error(err instanceof Error ? err.message : err);
    process.exitCode = 1;
  }

  await Promise.race([
    app.close().catch(() => undefined),
    new Promise<void>((resolve) => setTimeout(resolve, 2000)),
  ]);
  process.exit(process.exitCode ?? 0);
}

void main();
