import { Injectable } from '@nestjs/common';
import { BaseUpdateManager } from 'src/core/modules/domain/usecase/managers/base-update.manager';
import { TransactionEntity } from '../../entities/transaction.entity';
import { TransactionModel } from '../../../data/models/transaction.model';
import { TransactionUpdatedEvent } from '../../entities/event/transaction-updated.event';
import {
  EventTopics,
  columnUniques,
  validateRelations,
} from 'src/core/strings/constants/interface.constants';
import { TransactionType } from '../../../constants';
import { mappingRevertTransaction } from './helpers/mapping-transaction.helper';

@Injectable()
export class UpdateTransactionManager extends BaseUpdateManager<TransactionEntity> {
  async validateProcess(): Promise<void> {
    return;
  }

  async beforeProcess(): Promise<void> {
    mappingRevertTransaction(this.data, TransactionType.ADMIN);
    const changeDate = this.data.booking_date != this.oldData.booking_date;

    if (changeDate) {
      Object.assign(this.data, {
        booking_date_before: this.oldData.booking_date,
      });
    }
    return;
  }

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

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

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

  get entityTarget(): any {
    return TransactionModel;
  }

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