import { MoneyValue, Percentage, Quantity } from "core/common/domain";
import { InvoiceItemDescription } from "../../value-objects";
import { InvoiceItem } from "./invoice-item";

describe("InvoiceItem", () => {
  it("debería calcular correctamente el subtotal (unitPrice * quantity)", () => {
    const props = {
      description: InvoiceItemDescription.create("Producto A"),
      quantity: Quantity.create({ amount: 200, scale: 2 }),
      unitPrice: MoneyValue.create(50),
      discount: Percentage.create(0),
    };

    const result = InvoiceItem.create(props);

    expect(result.isOk()).toBe(true);
    const invoiceItem = result.unwrap();
    expect(invoiceItem.subtotalPrice.value).toBe(100); // 50 * 2
  });

  it("debería calcular correctamente el total con descuento", () => {
    const props = {
      description: new InvoiceItemDescription("Producto B"),
      quantity: new Quantity(3),
      unitPrice: new MoneyValue(30),
      discount: new Percentage(10), // 10%
    };

    const result = InvoiceItem.create(props);

    expect(result.isOk()).toBe(true);
    const invoiceItem = result.unwrap();
    expect(invoiceItem.totalPrice.value).toBe(81); // (30 * 3) - 10% de (30 * 3)
  });

  it("debería devolver los valores correctos de las propiedades", () => {
    const props = {
      description: new InvoiceItemDescription("Producto C"),
      quantity: new Quantity(1),
      unitPrice: new MoneyValue(100),
      discount: new Percentage(5),
    };

    const result = InvoiceItem.create(props);

    expect(result.isOk()).toBe(true);
    const invoiceItem = result.unwrap();
    expect(invoiceItem.description.value).toBe("Producto C");
    expect(invoiceItem.quantity.value).toBe(1);
    expect(invoiceItem.unitPrice.value).toBe(100);
    expect(invoiceItem.discount.value).toBe(5);
  });

  it("debería manejar correctamente un descuento del 0%", () => {
    const props = {
      description: new InvoiceItemDescription("Producto D"),
      quantity: new Quantity(4),
      unitPrice: new MoneyValue(25),
      discount: new Percentage(0),
    };

    const result = InvoiceItem.create(props);

    expect(result.isOk()).toBe(true);
    const invoiceItem = result.unwrap();
    expect(invoiceItem.totalPrice.value).toBe(100); // 25 * 4
  });

  it("debería manejar correctamente un descuento del 100%", () => {
    const props = {
      description: new InvoiceItemDescription("Producto E"),
      quantity: new Quantity(2),
      unitPrice: new MoneyValue(50),
      discount: new Percentage(100),
    };

    const result = InvoiceItem.create(props);

    expect(result.isOk()).toBe(true);
    const invoiceItem = result.unwrap();
    expect(invoiceItem.totalPrice.value).toBe(0); // (50 * 2) - 100% de (50 * 2)
  });
});
