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

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

  async findAllActive(): Promise<BankAccountEntity[]> {
    return this.find({
      where: { is_active: true } as FindOptionsWhere<BankAccountEntity>,
      relations: { bank: true },
      order: { account_name: 'ASC' },
    });
  }

  async findByIdWithBank(id: string): Promise<BankAccountEntity | null> {
    return this.findOne({
      where: { id } as FindOptionsWhere<BankAccountEntity>,
      relations: { bank: true },
    });
  }
}
