import { joinRelationHelper } from 'src/core/helpers/query/join-relations.helper';
import { BaseReadManager } from '../base-read.manager';

export abstract class BaseDetailManager<Entity> extends BaseReadManager {
  protected dataId: string;
  protected result: Entity;
  abstract get setFindProperties(): any;

  setData(dataId: string): void {
    this.dataId = dataId;
  }

  async process(): Promise<void> {
    const { joinRelations, selectRelations, countRelations } = this.relations;
    if (joinRelations?.length)
      joinRelationHelper(joinRelations, this.tableName, this.queryBuilder);
    if (selectRelations?.length)
      joinRelationHelper(
        selectRelations,
        this.tableName,
        this.queryBuilder,
        'select',
      );
    if (countRelations?.length)
      joinRelationHelper(
        countRelations,
        this.tableName,
        this.queryBuilder,
        'count',
      );

    if (this.selects?.length) this.queryBuilder.select(this.selects);
    this.queryBuilder.where(this.setFindProperties);
    this.result = await this.queryBuilder.getOne();
  }

  getResult(): Entity {
    return this.result;
  }
}
