import { BaseCoreModel } from 'src/core/modules/data/model/base-core.model';
import { QueueOrder } from '../../domain/entities/order.entity';
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { QueueTicket } from '../../domain/entities/ticket.entity';
import { QueueItem } from '../../domain/entities/queue-item.entity';

import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
import { Queue } from '../../domain/entities/queue.entity';
import { BaseModel } from 'src/core/modules/data/model/base.model';

@Entity(TABLE_NAME.QUEUE_ORDER)
export class QueueOrderModel
  extends BaseCoreModel<QueueOrderModel>
  implements QueueOrder
{
  @Column('varchar')
  code: string;

  @Column('varchar', { nullable: true })
  customer: string;

  @Column('varchar', { nullable: true })
  phone: string;

  @Column('varchar', { nullable: false })
  transaction_id: string;

  @OneToMany(() => QueueTicketModel, (model) => model.order, {
    cascade: true,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  tickets: QueueTicket[];

  @Column({ type: 'bigint' })
  date: number;
}

@Entity(TABLE_NAME.QUEUE_TICKET)
export class QueueTicketModel
  extends BaseCoreModel<QueueTicketModel>
  implements QueueTicket
{
  @Column('varchar')
  code: string;

  @Column('varchar', { nullable: true })
  customer: string;

  @Column('varchar', { nullable: true })
  phone: string;

  @ManyToOne(() => QueueOrderModel, (model) => model.tickets, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'order_id' })
  order: QueueOrder;

  @Column('varchar')
  order_id: string;

  @Column({ type: 'bigint' })
  date: number;

  @OneToMany(() => QueueItemModel, (model) => model.ticket, {
    cascade: true,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  items: QueueItemModel[];
}

@Entity(TABLE_NAME.QUEUE_ITEM)
export class QueueItemModel
  extends BaseCoreModel<QueueItemModel>
  implements QueueItem
{
  @ManyToOne(() => QueueTicketModel, (model) => model.items, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'ticket_id' })
  ticket: QueueTicket;

  @Column('varchar')
  ticket_id: string;

  @OneToMany(() => QueueModel, (model) => model.item, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  queue: QueueModel[];

  @Column('varchar')
  item_id: string;

  @ManyToOne(() => ItemModel)
  @JoinColumn({ name: 'item_id' })
  item: ItemModel;

  @Column('int')
  qty: number;
}

@Entity(TABLE_NAME.QUEUE)
export class QueueModel extends BaseModel<Queue> implements Queue {
  @Column('varchar')
  code: string;

  @Column('varchar')
  status: string;

  @Column({ type: 'bigint' })
  time: number;

  @Column({ type: 'bigint', nullable: true })
  last_notification: number;

  @Column({ type: 'bigint' })
  call_time: number;

  @Column({ type: 'boolean' })
  vip: boolean;

  @ManyToOne(() => QueueItemModel, (model) => model.queue, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'item_id' })
  item: QueueItemModel;

  @Column('varchar')
  item_id: string;

  @Column('varchar', { nullable: true })
  item_queue_id: string;

  @Column('int')
  qty: number;

  average = 0;
  peak_level = 100;
}
