import { Injectable } from '@nestjs/common';
import { BaseIndexManager } from 'src/core/modules/domain/usecase/managers/base-index.manager';
import { SelectQueryBuilder } from 'typeorm';
import {
  Param,
  RelationParam,
} from 'src/core/modules/domain/entities/base-filter.entity';
import { Queue } from '../entities/queue.entity';

@Injectable()
export class IndexQueueManager extends BaseIndexManager<Queue> {
  async prepareData(): Promise<void> {
    return;
  }

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

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

  get relations(): RelationParam {
    return {
      joinRelations: [],
      selectRelations: ['item', 'item.ticket'],
      countRelations: [],
    };
  }

  get selects(): string[] {
    return [
      `${this.tableName}.id`,
      `${this.tableName}.code`,
      `${this.tableName}.status`,
      `${this.tableName}.time`,
      `${this.tableName}.call_time`,
      `${this.tableName}.vip`,
      `${this.tableName}.item`,
      `${this.tableName}.qty`,
      `${this.tableName}.created_at`,

      `item.id`,
      `ticket.customer`,
      `ticket.phone`,
    ];
  }

  get specificFilter(): Param[] {
    return [
      {
        cols: `${this.tableName}.status`,
        data: this.filterParam.status,
      },
    ];
  }

  setQueryFilter(
    queryBuilder: SelectQueryBuilder<Queue>,
  ): SelectQueryBuilder<Queue> {
    if (this.filterParam.vip != null) {
      queryBuilder.andWhere(`${this.tableName}.vip = :vip`, {
        vip: this.filterParam.vip,
      });
    }
    return queryBuilder;
  }
}
