import { Tax } from "@erp/core/api";
import { DomainEntity, UniqueID } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import { InvoiceAmount } from "../../value-objects/invoice-amount";

export interface InvoiceTaxProps {
  tax: Tax;
  taxesAmount: InvoiceAmount;
}

export class InvoiceTax extends DomainEntity<InvoiceTaxProps> {
  static create(props: InvoiceTaxProps, id?: UniqueID): Result<InvoiceTax, Error> {
    const invoiceTax = new InvoiceTax(props, id);

    // Reglas de negocio / validaciones
    // ...
    // ...

    return Result.ok(invoiceTax);
  }

  public get tax(): Tax {
    return this.props.tax;
  }

  getProps(): InvoiceTaxProps {
    return this.props;
  }

  toPrimitive() {
    return this.getProps();
  }

  public getTaxAmount(taxableAmount: InvoiceAmount): InvoiceAmount {
    return taxableAmount.percentage(this.tax.percentage);
  }
}
