import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, FindOptionsWhere, FindOptionsOrder } from 'typeorm';
import { ClsService } from 'nestjs-cls';
import { TenantAwareRepository } from '../../../common/repositories/tenant-aware.repository';
import { TaxTypeEntity } from '../../../entities/tax-type.entity';

@Injectable()
export class TaxTypeRepository extends TenantAwareRepository<TaxTypeEntity> {
  constructor(@InjectRepository(TaxTypeEntity) repo: Repository<TaxTypeEntity>, cls: ClsService) {
    super(repo, cls);
  }

  async findAllSorted(): Promise<TaxTypeEntity[]> {
    return this.find({ order: { name: 'ASC' } as FindOptionsOrder<TaxTypeEntity> });
  }

  async findAllActive(): Promise<TaxTypeEntity[]> {
    return this.find({
      where: { is_active: true } as FindOptionsWhere<TaxTypeEntity>,
      order: { name: 'ASC' } as FindOptionsOrder<TaxTypeEntity>,
    });
  }

  async findByName(name: string): Promise<TaxTypeEntity | null> {
    return this.findOne({
      where: { name },
    });
  }
}
