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

describe("MoneyValue Value Object", () => {
  // Prueba la creación de un valor monetario válido.
  it("Should create a valid money value (number)", () => {
    const validMoneyValue = MoneyValue.create({
      amount: 10055,
    });

    expect(validMoneyValue.isSuccess).toBe(true);
    expect(validMoneyValue.object.toUnit()).toBe(100.55);
  });

  // Prueba la creación de un valor monetario nulo.
  it("Should create a valid null money value", () => {
    const nullMoneyValue = MoneyValue.create({
      amount: null,
    });

    expect(nullMoneyValue.isSuccess).toBe(true);
    expect(nullMoneyValue.object.isEmpty()).toBe(true);
  });

  // Prueba la creación de un valor monetario válido a partir de una cadena.
  it("Should create a valid money value from string and format it", () => {
    const validMoneyValueFromString = MoneyValue.create({
      amount: "5075",
      scale: 2,
    });

    expect(validMoneyValueFromString.isSuccess).toBe(true);
    expect(validMoneyValueFromString.object.toString()).toEqual("50.75");
  });

  // Prueba la creación de un valor monetario con una cadena no válida.
  it("Should fail to create money value from invalid string", () => {
    const invalidMoneyValueFromString = MoneyValue.create({
      amount: "invalid",
    });

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

  it("should create MoneyValue from number and currency", () => {
    const result = MoneyValue.create({
      amount: 100,
      scale: 3,
      currencyCode: "EUR",
    });
    expect(result.isSuccess).toBe(true);

    const moneyValue = result.object;

    expect(moneyValue.getAmount()).toBe(100);
    expect(moneyValue.getCurrency().code).toBe("EUR");
    expect(moneyValue.getScale()).toBe(3);
  });

  it("should create MoneyValue from string and currency", () => {
    const result = MoneyValue.create({
      amount: "12345",
      scale: 2,
      currencyCode: "USD",
    });
    expect(result.isSuccess).toBe(true);

    const moneyValue = result.object;

    expect(moneyValue.getAmount()).toBe(12345);
    expect(moneyValue.getCurrency().code).toBe("USD");
    expect(moneyValue.getScale()).toBe(2);
  });

  it("should fail to create MoneyValue with invalid amount", () => {
    const result = MoneyValue.create({
      amount: "invalid",
      scale: 2,
      currencyCode: "USD",
    });
    expect(result.isFailure).toBe(true);
  });

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

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

  // Prueba la verificación de valor cero.
  it("Should check if value is zero", () => {
    const zeroMoneyValue = MoneyValue.create({
      amount: 0,
    }).object;
    const nonZeroMoneyValue = MoneyValue.create({
      amount: 50,
    }).object;

    expect(zeroMoneyValue.isZero()).toBe(true);
    expect(nonZeroMoneyValue.isZero()).toBe(false);
  });
});
