import { Injectable } from '@nestjs/common';
import { BaseDataService } from 'src/core/modules/data/service/base-data.service';
import { FaqEntity } from '../../domain/entities/faq.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { FaqModel } from '../models/faq.model';
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
import { Repository } from 'typeorm';

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

  async getSortColumn(): Promise<number> {
    const query = this.repo.createQueryBuilder('data');

    const sortColumn = await query
      .select('data.sort_order')
      .orderBy('data.sort_order', 'DESC')
      .getOne();

    const lastColumn = sortColumn?.sort_order ?? 0;
    return lastColumn + 1;
  }
}
