import {
  HttpStatus,
  Injectable,
  UnprocessableEntityException,
} from '@nestjs/common';
import { BaseUpdateStatusManager } from 'src/core/modules/domain/usecase/managers/base-update-status.manager';
import { ItemEntity } from '../../entities/item.entity';
import {
  EventTopics,
  validateRelations,
} from 'src/core/strings/constants/interface.constants';
import { ItemModel } from '../../../data/models/item.model';
import { ItemChangeStatusEvent } from '../../entities/event/item-change-status.event';
import { STATUS } from 'src/core/strings/constants/base.constants';

@Injectable()
export class ActiveItemManager extends BaseUpdateStatusManager<ItemEntity> {
  getResult(): string {
    return `Success active data ${this.result.name}`;
  }

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

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

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

  get validateRelations(): validateRelations[] {
    const timeGroupId = this.data.time_group_id ?? this.data.time_group?.id;
    const relation =
      this.data.bundling_items?.length > 0
        ? 'bundling_items'
        : 'bundling_parents';

    const validateRelations: validateRelations[] = [
      {
        relation: 'tenant',
        singleQuery: ['status', '!=', STATUS.ACTIVE],
        message: `Gagal! tenant tidak aktif`,
      },
      {
        relation: 'bundling_items',
        singleQuery: ['status', '!=', STATUS.ACTIVE],
        message: `Gagal! Terdapat item yang belum aktif`,
      },
    ];

    if (timeGroupId != null) {
      validateRelations.push({
        relation: relation,
        singleQuery: ['time_group_id', 'not in', [timeGroupId, null]],
        message: `Gagal Update! Time group item dan bundling item tidak sama`,
      });
    }

    return validateRelations;
  }

  get entityTarget(): any {
    return ItemModel;
  }

  get eventTopics(): EventTopics[] {
    return [
      {
        topic: ItemChangeStatusEvent,
        data: this.data,
      },
    ];
  }
}
