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 { UserEntity } from 'src/modules/user-related/user/domain/entities/user.entity';
import { UserRole } from 'src/modules/user-related/user/constants';

@Injectable()
export class IndexTenantManager extends BaseIndexManager<UserEntity> {
  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: [],

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

  get selects(): string[] {
    return [
      `${this.tableName}.id`,
      `${this.tableName}.status`,
      `${this.tableName}.name`,
      `${this.tableName}.email`,
      `${this.tableName}.username`,
      `${this.tableName}.share_margin`,
      `${this.tableName}.created_at`,
      `${this.tableName}.creator_name`,
      `${this.tableName}.updated_at`,
      `${this.tableName}.editor_name`,
    ];
  }

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

  setQueryFilter(
    queryBuilder: SelectQueryBuilder<UserEntity>,
  ): SelectQueryBuilder<UserEntity> {
    // tampilkan hanya role tenant
    // ? karena menggunakan table user (admin, staff, tenant)
    queryBuilder.andWhere(`${this.tableName}.role = :tenant`, {
      tenant: UserRole.TENANT,
    });

    return queryBuilder;
  }
}
