import { QueueModel } from 'src/modules/queue/data/models/queue.model';
import { CustomerQueueManager } from './customer-queue.manager';
import { QueueCondition } from '../formula/queue-condition.formula';

export class CustomerQueueDetailManager extends CustomerQueueManager {
  private queues: QueueModel[] = [];
  currentQueues(queues: QueueModel[]) {
    this.queues = queues;
  }
  get data() {
    const queueCondition = new QueueCondition(this.queues);
    return this.tickets.map((ticket) => {
      const queueItems = this.mergeQueueItems(ticket);
      return {
        id: ticket.id,
        code: ticket.code,
        customer: ticket.customer,
        phone: ticket.phone,
        date: ticket.date,
        summary: this.summaryTicket(ticket),
        items: queueItems.map((item) => {
          return {
            id: item.id,
            item_queue_id: item.queue_item_id,
            title: item.title,
            image_url: item.image_url,
            summary: {
              total_tickets: item.qty,
              total_used: this.totalUsedItems(item.queues),
              total_queue: this.totalQueueItems(item.queues),
            },
            queue: item.queues
              .sort((a, b) => b.time - a.time)
              .map((q) => {
                return {
                  code: q.code,
                  qty: q.qty,
                  time: this.toTime(q.time),
                  status: q.status,
                };
              }),
            queue_condition: queueCondition.condition(item.queue_item_id),
          };
        }),
      };
    });
  }
}
