package quotation_service_v2

import (
	"strings"
	"time"

	qshared "github.com/tech/sendico/payments/quotation/internal/shared"
	"github.com/tech/sendico/payments/storage/model"
	payecon "github.com/tech/sendico/pkg/payments/economics"
	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 minExpiry(values []time.Time) (time.Time, bool) {
	var min time.Time
	for _, value := range values {
		if value.IsZero() {
			continue
		}
		if min.IsZero() || value.Before(min) {
			min = value
		}
	}
	if min.IsZero() {
		return time.Time{}, false
	}
	return min, true
}

func firstNonEmpty(values ...string) string {
	for _, value := range values {
		trimmed := strings.TrimSpace(value)
		if trimmed != "" {
			return trimmed
		}
	}
	return ""
}

func cloneProtoMoney(src *moneyv1.Money) *moneyv1.Money {
	return qshared.CloneProtoMoneyTrimUpperCurrency(src)
}

func resolvedSettlementModeFromModel(mode model.SettlementMode) paymentv1.SettlementMode {
	switch mode {
	case model.SettlementModeFixReceived:
		return paymentv1.SettlementMode_SETTLEMENT_FIX_RECEIVED
	case model.SettlementModeFixSource:
		return paymentv1.SettlementMode_SETTLEMENT_FIX_SOURCE
	default:
		return paymentv1.SettlementMode_SETTLEMENT_FIX_SOURCE
	}
}

func defaultResolvedFeeTreatment() quotationv2.FeeTreatment {
	return payecon.DefaultFeeTreatment()
}
