import { UniqueID } from "@/core/common/domain";
import { Collection, Result } from "@repo/rdx-utils";
import { CustomerInvoice } from "../aggregates";
import { ICustomerInvoiceRepository } from "../repositories";
import { ICustomerInvoiceService } from "./customer-invoice-service.interface";

export class CustomerInvoiceService implements ICustomerInvoiceService {
  constructor(private readonly invoiceRepository: ICustomerInvoiceRepository) {}

  async findCustomerInvoices(
    transaction?: any
  ): Promise<Result<Collection<CustomerInvoice>, Error>> {
    const invoicesOrError = await this.invoiceRepository.findAll(transaction);
    if (invoicesOrError.isFailure) {
      return Result.fail(invoicesOrError.error);
    }

    return Result.ok(invoicesOrError.data);
  }

  async findCustomerInvoiceById(
    invoiceId: UniqueID,
    transaction?: any
  ): Promise<Result<CustomerInvoice>> {
    return await this.invoiceRepository.findById(invoiceId, transaction);
  }
}
