package decimal

import (
	"testing"

	"github.com/shopspring/decimal"
)

func TestParseMoney(t *testing.T) {
	cases := []struct {
		in   string
		want string
	}{
		{"3996000", "3996000"},
		{"12.345", "12.35"}, // rounds to 2dp
		{"", "0"},
		{"0.001", "0"},
	}
	for _, c := range cases {
		got, err := ParseMoney(c.in)
		if err != nil {
			t.Fatalf("ParseMoney(%q): %v", c.in, err)
		}
		if !got.Equal(decimal.RequireFromString(c.want)) {
			t.Errorf("ParseMoney(%q) = %s, want %s", c.in, got, c.want)
		}
	}
}

func TestParseQuantity(t *testing.T) {
	got, err := ParseQuantity("0.001")
	if err != nil {
		t.Fatal(err)
	}
	if !got.Equal(decimal.RequireFromString("0.001")) {
		t.Errorf("ParseQuantity(0.001) = %s, want 0.001", got)
	}
}

func TestMoneyPrecision(t *testing.T) {
	// Money2 always rounds to 2dp for NUMERIC(15,2). PO-tax integer-rounding
	// (Laravel parity) is implemented explicitly in the purchasing module.
	d := decimal.RequireFromString("12.345")
	if got := Money2(d); got.String() != "12.35" {
		t.Errorf("Money2(12.345) = %s, want 12.35", got)
	}
}

func TestNonNegative(t *testing.T) {
	if err := NonNegative(decimal.NewFromInt(-1)); err == nil {
		t.Error("expected error for negative value")
	}
	if err := NonNegative(decimal.Zero); err != nil {
		t.Errorf("zero should pass: %v", err)
	}
}
