import { Controller } from '@nestjs/common';
import { EventBus } from '@nestjs/cqrs';
import { Cron } from '@nestjs/schedule';
import { CronMidnightEvent } from '../entities/cron-midnight.event';

@Controller()
export class MidnightCronManager {
  constructor(public eventBus: EventBus) {}

  @Cron(`${process.env.CRON_MIDNIGHT}`)
  async scheduler(): Promise<void> {
    const local_time = new Date().toLocaleDateString('en-US', {
      timeZone: 'Asia/Jakarta',
    });
    const now = new Date(local_time).getTime();

    console.log('Cron Event executed every 00:00 minutes.', now);

    this.eventBus.publishAll([new CronMidnightEvent()]);
  }
}
