package gateway_funding_profile

import (
	"errors"
	"testing"

	"github.com/tech/sendico/payments/storage/model"
	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/model/account_role"
	moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
)

func TestBuildFundingGateFromProfile_NilProfile(t *testing.T) {
	gate, err := BuildFundingGateFromProfile(nil, &moneyv1.Money{
		Amount:   "100",
		Currency: "USD",
	})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if gate != nil {
		t.Fatalf("expected nil gate")
	}
}

func TestBuildFundingGateFromProfile_None(t *testing.T) {
	gate, err := BuildFundingGateFromProfile(&GatewayFundingProfile{
		Mode: model.FundingModeNone,
	}, &moneyv1.Money{
		Amount:   "100",
		Currency: "USD",
	})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if gate == nil {
		t.Fatalf("expected gate")
	}
	if gate.Mode != model.FundingModeNone {
		t.Fatalf("expected mode %q, got %q", model.FundingModeNone, gate.Mode)
	}
}

func TestBuildFundingGateFromProfile_BalanceReserve(t *testing.T) {
	from := account_role.AccountRoleOperating
	to := account_role.AccountRoleHold
	gate, err := BuildFundingGateFromProfile(&GatewayFundingProfile{
		Mode: model.FundingModeBalanceReserve,
		Reserve: &ReservePolicy{
			BlockAccountRef: "ledger:block",
			FromRole:        &from,
			ToRole:          &to,
			ReleaseOnFail:   true,
			ReleaseOnCancel: true,
		},
	}, &moneyv1.Money{
		Amount:   "100",
		Currency: "USD",
	})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if gate == nil || gate.Reserve == nil {
		t.Fatalf("expected reserve gate")
	}
	if gate.RequiredAmount == nil || gate.RequiredAmount.GetAmount() != "100" {
		t.Fatalf("expected required amount 100, got %#v", gate.RequiredAmount)
	}
}

func TestBuildFundingGateFromProfile_BalanceReserveMissingPolicy(t *testing.T) {
	_, err := BuildFundingGateFromProfile(&GatewayFundingProfile{
		Mode: model.FundingModeBalanceReserve,
	}, &moneyv1.Money{
		Amount:   "100",
		Currency: "USD",
	})
	if !errors.Is(err, merrors.ErrInvalidArg) {
		t.Fatalf("expected invalid arg error, got %v", err)
	}
}

func TestBuildFundingGateFromProfile_DepositObservedUsesRequiredAmountFallback(t *testing.T) {
	gate, err := BuildFundingGateFromProfile(&GatewayFundingProfile{
		Mode: model.FundingModeDepositObserved,
		DepositCheck: &model.DepositCheckPolicy{
			WalletRef: "wallet-1",
		},
	}, &moneyv1.Money{
		Amount:   "42",
		Currency: "USDT",
	})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if gate == nil || gate.DepositCheck == nil {
		t.Fatalf("expected deposit gate")
	}
	if gate.DepositCheck.ExpectedAmount == nil {
		t.Fatalf("expected fallback expected amount")
	}
	if got := gate.DepositCheck.ExpectedAmount.GetAmount(); got != "42" {
		t.Fatalf("expected amount 42, got %q", got)
	}
}

func TestBuildFundingGateFromProfile_DepositObservedMissingWallet(t *testing.T) {
	_, err := BuildFundingGateFromProfile(&GatewayFundingProfile{
		Mode: model.FundingModeDepositObserved,
		DepositCheck: &model.DepositCheckPolicy{
			ExpectedAmount: &moneyv1.Money{
				Amount:   "42",
				Currency: "USDT",
			},
		},
	}, nil)
	if !errors.Is(err, merrors.ErrInvalidArg) {
		t.Fatalf("expected invalid arg error, got %v", err)
	}
}

func TestBuildFundingGateFromProfile_UnspecifiedMode(t *testing.T) {
	_, err := BuildFundingGateFromProfile(&GatewayFundingProfile{}, nil)
	if !errors.Is(err, merrors.ErrInvalidArg) {
		t.Fatalf("expected invalid arg error, got %v", err)
	}
}
