import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ClsService } from 'nestjs-cls';
import { TenantAwareRepository } from '../../../common/repositories/tenant-aware.repository';
import { CurrencyEntity } from '../../../entities/currency.entity';

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

  async findAllActive(): Promise<CurrencyEntity[]> {
    return this.find({ where: { is_active: true } as any, order: { code: 'ASC' } } as any);
  }

  async findByCode(code: string): Promise<CurrencyEntity | null> {
    const tenantId = this.getTenantId();
    return this.repo.findOne({
      where: { code, tenant_id: tenantId, is_deleted: false } as any,
    });
  }
}
