package utils

import (
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate"
)

func ValidateGetPaymentLinkBody(request *models.GetPaymentLinkBody) errors.Error {
	if request == nil {
		return errors.NewWithMessage("request body is empty", errors.ErrInvalidArgs)
	}

	switch request.Type {
	case models.PaymentTypeInstallments, models.PaymentTypeSBP, models.PaymentTypeTinkoff, models.PaymentTypeYoomoney:
		return nil
	case models.PaymentTypeQiwi, models.PaymentTypeSberPay, models.PaymentTypeMobile, models.PaymentTypeCash:
		return validateMobilePayment(request)
	case models.PaymentTypeAlfabank:
		return validateLoginPayment(request)
	case models.PaymentTypeBankCard:
		return validateBankcardPayment(request)
	default:
		return errors.NewWithMessage("unknown payment type", errors.ErrInvalidArgs)
	}
}

func validateMobilePayment(request *models.GetPaymentLinkBody) errors.Error {
	if validate.IsStringEmpty(request.PhoneNumber) {
		return errors.NewWithMessage("phone number is empty", errors.ErrInvalidArgs)
	}

	return nil
}

func validateLoginPayment(request *models.GetPaymentLinkBody) errors.Error {
	if validate.IsStringEmpty(request.Login) {
		return errors.NewWithMessage("login is empty", errors.ErrInvalidArgs)
	}

	return nil
}

func validateBankcardPayment(request *models.GetPaymentLinkBody) errors.Error {
	if request.BankCard == nil {
		return errors.NewWithMessage("bank card is empty", errors.ErrInvalidArgs)
	}

	if validate.IsStringEmpty(request.BankCard.Number) {
		return errors.NewWithMessage("bankcard number is empty", errors.ErrInvalidArgs)
	}

	if validate.IsStringEmpty(request.BankCard.ExpiryYear) {
		return errors.NewWithMessage("bankcard expiry year is empty", errors.ErrInvalidArgs)
	}

	if validate.IsStringEmpty(request.BankCard.ExpiryMonth) {
		return errors.NewWithMessage("bankcard expiry month is empty", errors.ErrInvalidArgs)
	}

	return nil
}
