import { CurrencyCode, LanguageCode, MoneyValue, Percentage, Quantity } from "@repo/rdx-ddd";
import { CustomerInvoiceItemDescription } from "../../value-objects";
import { CustomerInvoiceItem } from "./customer-invoice-item";

describe("CustomerInvoiceItem", () => {
  it("debería calcular correctamente el subtotal (unitPrice * quantity)", () => {
    const props = {
      description: CustomerInvoiceItemDescription.create("Producto A"),
      quantity: Quantity.create({ amount: 200, scale: 2 }),
      unitPrice: MoneyValue.create(50),
      discount: Percentage.create(0),
      languageCode: LanguageCode.create("es"),
      currencyCode: CurrencyCode.create("EUR"),
    };

    const result = CustomerInvoiceItem.create(props);

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

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

    const result = CustomerInvoiceItem.create(props);

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

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

    const result = CustomerInvoiceItem.create(props);

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

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

    const result = CustomerInvoiceItem.create(props);

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

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

    const result = CustomerInvoiceItem.create(props);

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