import {
  HttpStatus,
  Injectable,
  UnprocessableEntityException,
} from '@nestjs/common';
import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base-custom.manager';
import { SeasonPeriodEntity } from '../../entities/season-period.entity';
import { EventTopics } from 'src/core/strings/constants/interface.constants';
import { SeasonPeriodModel } from '../../../data/models/season-period.model';
import { SeasonPeriodPriceUpdatedEvent } from '../../entities/event/season-period-price-updated.event';
import { OPERATION } from 'src/core/strings/constants/base.constants';

@Injectable()
export class UpdateSeasonPeriodPriceManager extends BaseCustomManager<SeasonPeriodEntity> {
  async validateProcess(): Promise<void> {
    const existSeason = await this.dataService.getOneByOptions({
      where: {
        season_type_id: this.data.season_period.id,
      },
    });
    if (!existSeason) {
      throw new UnprocessableEntityException({
        statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        message: `Gagagl! Tidak terdapat data denga tipe season ${this.data.season_period.name}`,
        error: 'Unprocessable Entity',
      });
    }
    return;
  }

  async beforeProcess(): Promise<void> {
    return;
  }

  async process(): Promise<void> {
    this.eventBus.publish(
      new SeasonPeriodPriceUpdatedEvent({
        id: '',
        old: null,
        data: this.data,
        user: this.user,
        description: '',
        module: this.tableName,
        op: OPERATION.CREATE,
      }),
    );
    return;
  }

  async afterProcess(): Promise<void> {
    return;
  }

  get entityTarget(): any {
    return SeasonPeriodModel;
  }

  getResult() {
    return;
  }

  get eventTopics(): EventTopics[] {
    return [];
  }
}
