import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { GenerateMonthlyInvoiceUseCase } from '../../application/use-cases/generate-monthly-invoice.use-case';
import { PlanSeedService } from '../seed/plan-seed.service';

@Injectable()
export class BillingScheduler {
  private readonly logger = new Logger(BillingScheduler.name);

  constructor(
    private readonly generateMonthlyInvoice: GenerateMonthlyInvoiceUseCase,
    private readonly planSeed: PlanSeedService,
  ) {}

  // Run on the 1st of each month at 00:05 UTC
  @Cron('5 0 1 * *')
  async handleMonthlyBilling() {
    this.logger.log('Running monthly billing job...');
    try {
      await this.generateMonthlyInvoice.execute();
      this.logger.log('Monthly billing job completed');
    } catch (err) {
      this.logger.error(`Monthly billing job failed: ${err.message}`);
    }
  }
}
