import { Injectable } from '@nestjs/common';
import { UserPrivilegeConfigurationEntity } from '../../../entities/user-privilege-configuration.entity';
import {
  EventTopics,
  validateRelations,
} from 'src/core/strings/constants/interface.constants';
import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base-custom.manager';
import { UserPrivilegeConfigurationModel } from 'src/modules/user-related/user-privilege/data/models/user-privilege-configuration.model';
import { UserPrivilegeConfigUpdatedEvent } from '../../../entities/event/user-privilege-configuration-updated.event';

@Injectable()
export class UpdateUserPrivilegeConfigurationManager extends BaseCustomManager<UserPrivilegeConfigurationEntity> {
  async validateProcess(): Promise<void> {
    return;
  }

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

  async process(): Promise<void> {
    this.result = await this.dataService.update(
      this.queryRunner,
      this.entityTarget,
      { id: this.data.id },
      this.data,
    );

    this.publishEvents();
    return;
  }

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

  get validateRelations(): validateRelations[] {
    return [];
  }

  getResult() {
    return this.result;
  }

  get entityTarget(): any {
    return UserPrivilegeConfigurationModel;
  }

  get eventTopics(): EventTopics[] {
    /**
     * Remove Event update privilege
     * This event cause the privilege update twice
     * When I check this event function, the function to add privilege detail (view, create, update, etc)
     * But when case update, privilege doesn't need to add the privilege detail again
     * So, this event is not require in privilege update
     */
    return [
      {
        topic: UserPrivilegeConfigUpdatedEvent,
        data: this.data,
      },
    ];
  }
}
