import { Injectable } from '@nestjs/common';
import { BaseUpdateManager } from 'src/core/modules/domain/usecase/managers/base-update.manager';
import { ItemCategoryEntity } from '../../entities/item-category.entity';
import { ItemCategoryModel } from '../../../data/models/item-category.model';
import { ItemCategoryUpdatedEvent } from '../../entities/event/item-category-updated.event';
import {
  EventTopics,
  columnUniques,
  validateRelations,
} from 'src/core/strings/constants/interface.constants';

@Injectable()
export class UpdateItemCategoryManager extends BaseUpdateManager<ItemCategoryEntity> {
  async validateProcess(): Promise<void> {
    Object.assign(this.data, {
      item_type: this.data.item_type.toLowerCase(),
    });
    return;
  }

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

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

  get validateRelations(): validateRelations[] {
    if (this.data.item_type != this.oldData.item_type) {
      return [
        {
          relation: 'items',
          message:
            'Gagal! tidak dapat mengubah tipe item karena sudah berelasi dengan item',
        },
      ];
    } else {
      return [];
    }
  }

  get uniqueColumns(): columnUniques[] {
    return [{ column: 'name' }];
  }

  get entityTarget(): any {
    return ItemCategoryModel;
  }

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