package payments

import (
	"errors"
	"fmt"
)

// Valid payment types
var validPaymentTypes = map[string]bool{
	"deposit": true,
	"full":    true,
	"tip":     true,
	"balance": true,
	"partial": true,
}

// ValidateAmount checks that amount is greater than 0 and has valid precision (max 2 decimal places when in pence)
func ValidateAmount(amount int64) error {
	if amount <= 0 {
		return errors.New("amount must be greater than 0")
	}
	// Amount is in pence (integer), so no precision issues possible at this level
	// The frontend must ensure the input has max 2 decimal places before converting to pence
	return nil
}

// ValidatePartialAmount checks that the partial amount doesn't exceed the remaining balance
func ValidatePartialAmount(amountCents int64, remainingCents int64) error {
	if amountCents > remainingCents {
		return fmt.Errorf("partial amount (£%.2f) exceeds remaining balance (£%.2f)",
			float64(amountCents)/100, float64(remainingCents)/100)
	}
	if amountCents <= 0 {
		return errors.New("amount must be greater than 0")
	}
	return nil
}

// ValidatePaymentType checks that payment type is valid
func ValidatePaymentType(pt string) error {
	if !validPaymentTypes[pt] {
		return errors.New("invalid payment type")
	}
	return nil
}

// ValidateRefundReason checks that refund reason is not empty
func ValidateRefundReason(reason string) error {
	if reason == "" {
		return errors.New("refund reason is required")
	}
	return nil
}

// ValidateCardInfo checks that at least one of cardID or newCardToken is provided
func ValidateCardInfo(cardID, newCardToken *string) error {
	if cardID == nil && (newCardToken == nil || *newCardToken == "") {
		return errors.New("either card_id or new_card_token is required")
	}
	return nil
}
