import { MoneyValue } from "../money-value";

describe("MoneyValue", () => {
  test("should correctly instantiate with amount, scale, and currency", () => {
    const money = new MoneyValue({ value: 10000, scale: 2, currency_code: "EUR" });
    expect(money.value).toBe(100);
    expect(money.currency).toBe("EUR");
    expect(money.scale).toBe(2);
  });

  test("should add two MoneyValue instances with the same currency", () => {
    const money1 = new MoneyValue({ value: 10000, scale: 2, currency_code: "EUR" });
    const money2 = new MoneyValue({ value: 5000, scale: 2, currency_code: "EUR" });
    const result = money1.add(money2);
    expect(result.value).toBe(150);
  });

  test("should subtract two MoneyValue instances with the same currency", () => {
    const money1 = new MoneyValue({ value: 20000, scale: 2, currency_code: "EUR" });
    const money2 = new MoneyValue({ value: 5000, scale: 2, currency_code: "EUR" });
    const result = money1.subtract(money2);
    expect(result.value).toBe(150);
  });

  test("should throw an error when adding different currencies", () => {
    const money1 = new MoneyValue({ value: 10000, scale: 2, currency_code: "EUR" });
    const money2 = new MoneyValue({ value: 5000, scale: 2, currency_code: "USD" });
    expect(() => money1.add(money2)).toThrow(
      "You must provide a Dinero instance with the same currency"
    );
  });

  test("should correctly convert scale", () => {
    const money = new MoneyValue({ value: 10000, scale: 2, currency_code: "EUR" });
    const converted = money.convertScale(4);
    expect(converted.value).toBe(100);
    expect(converted.scale).toBe(4);
  });

  test("should format correctly according to locale", () => {
    const money = new MoneyValue({ value: 123456, scale: 2, currency_code: "EUR" });
    expect(money.format("en-US")).toBe("€1,234.56");
  });

  test("should compare MoneyValue instances correctly", () => {
    const money1 = new MoneyValue({ value: 10000, scale: 2, currency_code: "EUR" });
    const money2 = new MoneyValue({ value: 10000, scale: 2, currency_code: "EUR" });
    const money3 = new MoneyValue({ value: 5000, scale: 2, currency_code: "EUR" });

    expect(money1.equalsTo(money2)).toBe(true);
    expect(money1.greaterThan(money3)).toBe(true);
    expect(money3.lessThan(money1)).toBe(true);
  });
});
