import { Injectable } from '@nestjs/common';
import { BaseDataService } from 'src/core/modules/data/service/base-data.service';
import { UserPrivilegeConfigurationEntity } from '../../domain/entities/user-privilege-configuration.entity';
import { UserPrivilegeConfigurationModel } from '../models/user-privilege-configuration.model';
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { FilterUserPrivilegeEntity } from '../../domain/entities/filter-user-privilege.entity';
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';

@Injectable()
export class UserPrivilegeConfigurationService extends BaseDataService<UserPrivilegeConfigurationEntity> {
  constructor(
    @InjectRepository(UserPrivilegeConfigurationModel, CONNECTION_NAME.DEFAULT)
    private repo: Repository<UserPrivilegeConfigurationModel>,
  ) {
    super(repo);
  }

  async getIndex(
    queryBuilder: SelectQueryBuilder<UserPrivilegeConfigurationEntity>,
    params: FilterUserPrivilegeEntity,
  ): Promise<PaginationResponse<UserPrivilegeConfigurationEntity>> {
    const [data, total] = await queryBuilder
      .take(+params.limit)
      .skip(+params.limit * +params.page - +params.limit)
      .getManyAndCount();

    return {
      data,
      total,
    };
  }
}
