package wallet

import (
	"regexp"
	"testing"

	"hfb_sys/backend/internal/model"
)

func TestApplyEntryRoundsMoneyBeforeComparing(t *testing.T) {
	account := model.WalletAccount{
		UserID:        7,
		FrozenBalance: 606.41,
	}
	entry := Entry{
		UserID:      7,
		Direction:   "out",
		Amount:      406.41 + 200.00,
		BalanceType: "frozen",
	}

	balanceAfter, err := applyEntry(&account, entry)
	if err != nil {
		t.Fatalf("applyEntry() error = %v", err)
	}
	if balanceAfter != 0 {
		t.Fatalf("balanceAfter = %v, want 0", balanceAfter)
	}
	if account.FrozenBalance != 0 {
		t.Fatalf("FrozenBalance = %v, want 0", account.FrozenBalance)
	}
}

func TestNewLedgerNoUsesReadableFormat(t *testing.T) {
	ledgerNo, err := newLedgerNo()
	if err != nil {
		t.Fatalf("newLedgerNo() error = %v", err)
	}
	matched := regexp.MustCompile(`^WL\d{21}$`).MatchString(ledgerNo)
	if !matched {
		t.Fatalf("ledgerNo = %q, want WL + 21 digits", ledgerNo)
	}
}
