import { Quantity } from "./Quantity"; // Asegúrate de importar correctamente la clase Quantity.

describe("Quantity Value Object", () => {
  it("Should create a valid quantity (number)", () => {
    const validQuantity = Quantity.createFromFormattedValue(5);

    expect(validQuantity.isSuccess).toBe(true);
    expect(validQuantity.object.toNumber()).toBe(5);
  });

  it("Should create a valid quantity (number)", () => {
    const validQuantity = Quantity.createFromFormattedValue("5,31");

    expect(validQuantity.isSuccess).toBe(true);
    expect(validQuantity.object.toNumber()).toBe(5.31);
    expect(validQuantity.object.toObject()).toStrictEqual({
      amount: 531,
      scale: 2,
    });
  });

  it("Should create a valid quantity (number)", () => {
    const validQuantity = Quantity.createFromFormattedValue("99,");

    expect(validQuantity.isSuccess).toBe(true);
    expect(validQuantity.object.toNumber()).toBe(99);
    expect(validQuantity.object.toObject()).toStrictEqual({
      amount: 990,
      scale: 1,
    });
  });

  it("Should create a valid quantity (number)", () => {
    const validQuantity = Quantity.createFromFormattedValue("9,1");

    expect(validQuantity.isSuccess).toBe(true);
    expect(validQuantity.object.toNumber()).toBe(9.1);
    expect(validQuantity.object.toObject()).toStrictEqual({
      amount: 91,
      scale: 1,
    });
  });

  // Prueba la creación de una cantidad válida.
  it("Should create a valid quantity (number)", () => {
    const validQuantity = Quantity.create({ amount: 5 });

    expect(validQuantity.isSuccess).toBe(true);
    expect(validQuantity.object.toNumber()).toBe(0.05);
  });

  it("Should create a valid quantity (string)", () => {
    const validQuantity = Quantity.create({ amount: "99" });

    expect(validQuantity.isSuccess).toBe(true);
    expect(validQuantity.object.toNumber()).toBe(0.99);
  });

  it("Should create a valid quantity (null)", () => {
    const validQuantity = Quantity.create({ amount: null });

    expect(validQuantity.isSuccess).toBe(true);
    expect(validQuantity.object.isNull).toBeTruthy();
  });

  // Prueba la creación de una cantidad nula.
  it("Should create a default quantity", () => {
    const nullQuantity = Quantity.create();

    expect(nullQuantity.isSuccess).toBe(true);
    expect(nullQuantity.object.amount).toBe(0);
    expect(nullQuantity.object.scale).toBe(2);
  });

  // Prueba la creación de una cantidad válida a partir de una cadena.
  it("Should create a valid quantity from string", () => {
    const validQuantityFromString = Quantity.create({ amount: "318" });

    expect(validQuantityFromString.isSuccess).toBe(true);
    expect(validQuantityFromString.object.toNumber()).toBe(3.18);
  });

  // Prueba la creación de una cantidad válida a partir de una cadena con decimales.
  it("Should create a valid quantity from string", () => {
    const validQuantityFromString = Quantity.create({ amount: "123456", scale: 2 });

    expect(validQuantityFromString.isSuccess).toBe(true);
    expect(validQuantityFromString.object.toNumber()).toBe(1234.56);
  });

  // Prueba la creación de una cantidad con una cadena no válida.
  it("Should fail to create quantity from invalid string", () => {
    const invalidQuantityFromString = Quantity.create({ amount: "invalid" });

    expect(invalidQuantityFromString.isFailure).toBe(true);
  });

  // Prueba la conversión a número.
  it("Should convert to number", () => {
    const quantity = Quantity.create({ amount: 700 }).object;
    const result = quantity.toNumber();

    expect(result).toBe(7);
  });

  // Prueba la conversión a cadena.
  it("Should convert to string", () => {
    const quantity = Quantity.create({ amount: 1500 }).object;
    const result = quantity.toString();

    expect(result).toBe("15.00");
  });

  // Prueba la operación de incremento.
  it("Should increment", () => {
    const quantity = Quantity.create({ amount: 5, scale: 2 }).object;

    const incrementedQuantity = quantity.increment().object;

    expect(incrementedQuantity.toNumber()).toBe(0.06);
  });

  it("Should increment quantity", () => {
    const firstQ = Quantity.create({ amount: -5 }).object;
    const secountQ = Quantity.create({ amount: 105 }).object;
    const incrementedQuantity = firstQ.increment(secountQ).object;

    expect(incrementedQuantity.toNumber()).toBe(1.0);
  });

  // Prueba la operación de decremento.
  it("Should decrement", () => {
    const quantity = Quantity.create({ amount: 0, scale: 0 }).object;
    const decrementedQuantity = quantity.decrement().object;

    expect(decrementedQuantity.toNumber()).toBe(-1);
  });

  // Prueba la operación de decremento.
  it("Should decrement quantity", () => {
    const quantity = Quantity.create({ amount: 100, scale: 1 }).object;
    const decrementedQuantity = quantity.decrement(
      Quantity.create({ amount: 1100, scale: 1 }).object
    ).object;

    expect(decrementedQuantity.toNumber()).toBe(-100);
  });
});
