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 { ItemRateEntity } from 'src/modules/item-related/item-rate/domain/entities/item-rate.entity';

@Injectable()
export class IndexSeasonPeriodeItemManager extends BaseIndexManager<ItemRateEntity> {
  async prepareData(): Promise<void> {
    this.filterParam.order_by = `${this.tableName}.id`;
    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',
        'item.item_category',
        'item.bundling_items',
        'item.time_group',
      ],

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

  get selects(): string[] {
    return [
      `${this.tableName}.id`,
      `${this.tableName}.price`,

      `item.id`,
      `item.created_at`,
      `item.status`,
      `item.item_type`,
      `item.name`,
      `item.hpp`,
      `item.limit_type`,
      `item.limit_value`,
      `item.base_price`,
      `item.breakdown_bundling`,
      `item.share_profit`,

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

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

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

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

  setQueryFilter(
    queryBuilder: SelectQueryBuilder<ItemRateEntity>,
  ): SelectQueryBuilder<ItemRateEntity> {
    if (this.filterParam.season_period_ids) {
      queryBuilder.andWhere(
        `${this.tableName}.season_period_id In (:...periodIds)`,
        {
          periodIds: this.filterParam.season_period_ids,
        },
      );
    }

    return queryBuilder;
  }
}
