import { Injectable } from '@nestjs/common';
import {
  EventTopics,
  columnUniques,
  validateRelations,
} from 'src/core/strings/constants/interface.constants';
import { BaseCreateManager } from 'src/core/modules/domain/usecase/managers/base-create.manager';

import { QueueOrderModel } from '../../data/models/queue.model';
import { SplitQueueDto } from '../../infrastructure/controllers/dto/split-queue.dto';
import { QueueService } from '../../data/services/queue.service';

@Injectable()
export class SplitQueueManager extends BaseCreateManager<QueueOrderModel> {
  private dto: SplitQueueDto;
  constructor(private readonly queueService: QueueService) {
    super();
  }

  setRequestData(dto: SplitQueueDto): void {
    this.dto = dto;
  }

  prepareData(): Promise<void> {
    const { tickets, ...order } = this.data;
    const ticket = tickets[0];
    const items = tickets
      .map((ticket) => {
        return ticket.items;
      })
      .flat();
    this.data = {
      code: order.code,
      customer: order.customer,
      phone: order.phone,
      date: order.date,
      updated_at: new Date().getTime(),
      tickets: [
        {
          code: `${order.code}-1`,
          customer: this.dto.name,
          phone: this.dto.phone,
          date: ticket.date,
          order_id: order.id,
          items: items.map((item) => {
            const itemQty = this.dto.items.find(
              (i) => i.queue_item_id === item.id,
            );
            return {
              item_id: item.item_id,
              qty: itemQty.qty,
            };
          }),
        },
      ],
    };
    super.prepareData();
    return;
  }

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

  async afterProcess(): Promise<void> {
    this.dto.items.forEach((item) => {
      this.queueService.updateItemQty(item.queue_item_id, item.qty);
    });
    return;
  }

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

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

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

  get entityTarget(): any {
    return QueueOrderModel;
  }
}
