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

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

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

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

  get relations(): RelationParam {
    return {
      // relation only join (for query purpose)
      joinRelations: [],

      // relation join and select (relasi yang ingin ditampilkan),
      selectRelations: [
        'item_category',
        'bundling_items',
        'tenant',
        'time_group',
      ],

      // relation yang hanya ingin dihitung (akan return number)
      countRelations: [],
    };
  }

  get selects(): string[] {
    return [
      `${this.tableName}.id`,
      `${this.tableName}.created_at`,
      `${this.tableName}.status`,
      `${this.tableName}.item_type`,
      `${this.tableName}.name`,
      `${this.tableName}.hpp`,
      `${this.tableName}.limit_type`,
      `${this.tableName}.limit_value`,
      `${this.tableName}.base_price`,
      `${this.tableName}.share_profit`,
      `${this.tableName}.breakdown_bundling`,
      `${this.tableName}.play_estimation`,
      `${this.tableName}.show_to_booking`,
      `${this.tableName}.booking_description`,
      `${this.tableName}.usage_type`,

      `item_category.id`,
      `item_category.name`,

      'bundling_items.id',
      'bundling_items.name',

      'tenant.id',
      'tenant.name',

      'time_group.id',
      'time_group.name',
    ];
  }

  get specificFilter(): Param[] {
    return [
      {
        cols: `${this.tableName}.name`,
        data: this.filterParam.names,
      },
      {
        cols: `${this.tableName}.item_type::text`,
        data: this.filterParam.item_types,
      },
      {
        cols: `${this.tableName}.limit_type::text`,
        data: this.filterParam.limit_types,
      },
      {
        cols: `item_category.name`,
        data: this.filterParam.item_categories,
      },
    ];
  }

  setQueryFilter(
    queryBuilder: SelectQueryBuilder<ItemEntity>,
  ): SelectQueryBuilder<ItemEntity> {
    if (this.filterParam.q) {
      queryBuilder.andWhere(
        `${this.tableName}.name ILIKE '%${this.filterParam.q}%'`,
      );
    }

    if (this.filterParam.tenant_ids?.length) {
      queryBuilder.andWhere(`${this.tableName}.tenant_id In (:...tenantIds)`, {
        tenantIds: this.filterParam.tenant_ids,
      });
    } else if (!this.filterParam.all_item) {
      queryBuilder.andWhere(`${this.tableName}.tenant_id Is Null`);
    }

    if (this.filterParam.time_group_ids?.length) {
      queryBuilder.andWhere(
        `(${this.tableName}.time_group_id In (:...timeGroupIds) OR ${this.tableName}.time_group_id Is Null)`,
        {
          timeGroupIds: this.filterParam.time_group_ids,
        },
      );
    }

    if (this.filterParam.show_to_booking) {
      queryBuilder.andWhere(`${this.tableName}.show_to_booking = true`);
    }

    if (this.filterParam.without_time_group != null) {
      const withoutTimeGroup = this.filterParam.without_time_group
        ? 'Is Null'
        : 'Is Not Null';

      queryBuilder.andWhere(
        `${this.tableName}.time_group_id ${withoutTimeGroup}`,
      );
    }

    return queryBuilder;
  }
}
