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 { SupplierPayoutEntity } from '../../../entities/supplier-payout.entity';

/**
 * §12e Pay Supplier — payout header repo. Mutations flow through
 * PaymentSettlementService so per-settlement totals + booking + master
 * rollups stay aligned with the parent payout. This repo only carries
 * reads + the initial save.
 */
@Injectable()
export class SupplierPayoutRepository extends TenantAwareRepository<SupplierPayoutEntity> {
  constructor(
    @InjectRepository(SupplierPayoutEntity)
    repo: Repository<SupplierPayoutEntity>,
    cls: ClsService,
  ) {
    super(repo, cls);
  }

  async findByVoucherNo(voucherNo: string): Promise<SupplierPayoutEntity | null> {
    return this.findOne({
      where: { voucher_no: voucherNo } as any,
    });
  }
}
