package crypto

import (
	"errors"
	"testing"

	"github.com/niels/banking-app/backend/internal/domain"
)

func TestValidChainStatusTransition(t *testing.T) {
	t.Parallel()

	tests := []struct {
		from string
		to   string
		want bool
	}{
		{"pending", "confirmed", true},
		{"pending", "failed", true},
		{"confirmed", "reversed", true},
		{"failed", "confirmed", false},
		{"reversed", "confirmed", false},
		{"pending", "reversed", false},
	}

	for _, tt := range tests {
		if got := validChainStatusTransition(tt.from, tt.to); got != tt.want {
			t.Fatalf("validChainStatusTransition(%q, %q) = %v, want %v", tt.from, tt.to, got, tt.want)
		}
	}
}

func TestParseBaseUnits(t *testing.T) {
	t.Parallel()

	got, err := parseBaseUnits("100000000")
	if err != nil {
		t.Fatalf("parseBaseUnits returned error: %v", err)
	}
	if got.String() != "100000000" {
		t.Fatalf("parseBaseUnits = %s, want 100000000", got.String())
	}

	if _, err := parseBaseUnits("0"); !errors.Is(err, domain.ErrValidation) {
		t.Fatalf("expected zero amount validation error, got %v", err)
	}
	if _, err := parseBaseUnits("-1"); !errors.Is(err, domain.ErrValidation) {
		t.Fatalf("expected negative amount validation error, got %v", err)
	}
	if _, err := parseBaseUnits("1.5"); !errors.Is(err, domain.ErrValidation) {
		t.Fatalf("expected decimal amount validation error, got %v", err)
	}
}

func TestNormalizeCustodyProviderConfigBlocksRealMovementWithoutContractedProduction(t *testing.T) {
	t.Parallel()

	err := normalizeCustodyProviderConfig(&CustodyProviderConfig{
		Provider:            "local_custody",
		ProviderType:        "local_simulator",
		Status:              "sandbox",
		APIEnvironment:      "local",
		RealMovementEnabled: true,
	})
	if !errors.Is(err, domain.ErrValidation) {
		t.Fatalf("expected real movement validation error, got %v", err)
	}
}

func TestNormalizeTravelRuleRejectRequiresReason(t *testing.T) {
	t.Parallel()

	err := normalizeTravelRuleParams(&TravelRuleParams{
		CryptoChainTransactionID: "00000000-0000-0000-0000-000000000001",
		Status:                   "rejected",
	})
	if !errors.Is(err, domain.ErrValidation) {
		t.Fatalf("expected rejection reason validation error, got %v", err)
	}
}
