import {
  QueueItemModel,
  QueueTicketModel,
} from '../../../data/models/queue.model';

export class CustomerQueueManager {
  constructor(protected readonly tickets: QueueTicketModel[]) {}
  get data(): any {
    return this.tickets;
  }

  toTime(timestamp: number): string {
    // js function to convert timestamp (1729739455000) to time with format HH:mm
    const date = new Date(timestamp / 1000);
    const hours = date.getHours() + 7;
    const minutes = date.getMinutes();
    return `${hours < 10 ? '0' : ''}${hours}:${
      minutes < 10 ? '0' : ''
    }${minutes}`;
  }

  totalActivities(ticket: QueueTicketModel): number {
    return ticket.items.length;
  }

  totalTickets(ticket: QueueTicketModel): number {
    return ticket.items.reduce((acc, item) => acc + item.qty, 0);
  }

  totalUsedItems(item: QueueItemModel): number {
    return item.queue
      .filter((q) => ['done', 'called'].includes(q.status))
      .reduce((acc, item) => acc + item.qty, 0);
  }

  totalUsedTickets(ticket: QueueTicketModel): number {
    const tickets = ticket.items.map((item) => {
      return this.totalUsedItems(item);
    });

    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    return tickets.reduce(reducer, 0);
  }

  totalQueueItems(item: QueueItemModel): number {
    return item.queue
      .filter((q) => ['waiting'].includes(q.status))
      .reduce((acc, item) => acc + item.qty, 0);
  }
  totalQueueTickets(ticket: QueueTicketModel): number {
    const tickets = ticket.items.map((item) => {
      return this.totalQueueItems(item);
    });

    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    return tickets.reduce(reducer, 0);
  }
}
