import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { ThirdPartyCatalogService } from './third-party-catalog.service';

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

  constructor(private readonly thirdPartyCatalogService: ThirdPartyCatalogService) {}

  @Cron(process.env.CATALOG_SYNC_CRON || '0 */2 * * *')
  async handleCatalogSync() {
    if (process.env.CATALOG_SYNC_CRON_ENABLED !== 'true') {
      return;
    }

    try {
      await this.thirdPartyCatalogService.syncThirdPartyCatalog({
        triggerType: 'scheduled',
      });
    } catch (error) {
      this.logger.error(
        error instanceof Error ? error.message : String(error),
        error instanceof Error ? error.stack : undefined,
      );
    }
  }
}
