package quote_computation_service

import (
	"testing"

	moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
	paymentv1 "github.com/tech/sendico/pkg/proto/common/payment/v1"
	quotationv2 "github.com/tech/sendico/pkg/proto/payments/quotation/v2"
)

func TestEnsureComputedQuote_UsesItemResolvedEconomicsWhenQuoteUnset(t *testing.T) {
	src := &ComputedQuote{
		DebitAmount: &moneyv1.Money{Amount: "10", Currency: "USD"},
	}
	item := &QuoteComputationPlanItem{
		ResolvedSettlementMode: paymentv1.SettlementMode_SETTLEMENT_FIX_RECEIVED,
		ResolvedFeeTreatment:   quotationv2.FeeTreatment_FEE_TREATMENT_DEDUCT_FROM_DESTINATION,
	}

	got := ensureComputedQuote(src, item)
	if got == nil {
		t.Fatalf("expected quote")
	}
	if got.ResolvedSettlementMode != paymentv1.SettlementMode_SETTLEMENT_FIX_RECEIVED {
		t.Fatalf("unexpected resolved settlement mode: %s", got.ResolvedSettlementMode.String())
	}
	if got.ResolvedFeeTreatment != quotationv2.FeeTreatment_FEE_TREATMENT_DEDUCT_FROM_DESTINATION {
		t.Fatalf("unexpected resolved fee treatment: %s", got.ResolvedFeeTreatment.String())
	}
}

func TestEnsureComputedQuote_PreservesQuoteResolvedEconomics(t *testing.T) {
	src := &ComputedQuote{
		DebitAmount:            &moneyv1.Money{Amount: "10", Currency: "USD"},
		ResolvedSettlementMode: paymentv1.SettlementMode_SETTLEMENT_FIX_SOURCE,
		ResolvedFeeTreatment:   quotationv2.FeeTreatment_FEE_TREATMENT_ADD_TO_SOURCE,
	}
	item := &QuoteComputationPlanItem{
		ResolvedSettlementMode: paymentv1.SettlementMode_SETTLEMENT_FIX_RECEIVED,
		ResolvedFeeTreatment:   quotationv2.FeeTreatment_FEE_TREATMENT_DEDUCT_FROM_DESTINATION,
	}

	got := ensureComputedQuote(src, item)
	if got == nil {
		t.Fatalf("expected quote")
	}
	if got.ResolvedSettlementMode != paymentv1.SettlementMode_SETTLEMENT_FIX_SOURCE {
		t.Fatalf("unexpected resolved settlement mode: %s", got.ResolvedSettlementMode.String())
	}
	if got.ResolvedFeeTreatment != quotationv2.FeeTreatment_FEE_TREATMENT_ADD_TO_SOURCE {
		t.Fatalf("unexpected resolved fee treatment: %s", got.ResolvedFeeTreatment.String())
	}
}

func TestEnsureComputedQuote_DefaultsResolvedEconomicsWhenUnset(t *testing.T) {
	src := &ComputedQuote{
		DebitAmount: &moneyv1.Money{Amount: "10", Currency: "USD"},
	}
	item := &QuoteComputationPlanItem{}

	got := ensureComputedQuote(src, item)
	if got == nil {
		t.Fatalf("expected quote")
	}
	if got.ResolvedSettlementMode != paymentv1.SettlementMode_SETTLEMENT_FIX_SOURCE {
		t.Fatalf("unexpected default settlement mode: %s", got.ResolvedSettlementMode.String())
	}
	if got.ResolvedFeeTreatment != quotationv2.FeeTreatment_FEE_TREATMENT_ADD_TO_SOURCE {
		t.Fatalf("unexpected default fee treatment: %s", got.ResolvedFeeTreatment.String())
	}
}
