package validator

import (
	"apskel-pos-be/internal/constants"
	"apskel-pos-be/internal/contract"

	"github.com/go-playground/validator/v10"
)

type PaymentMethodValidator interface {
	ValidateCreatePaymentMethodRequest(req *contract.CreatePaymentMethodRequest) (error, string)
	ValidateUpdatePaymentMethodRequest(req *contract.UpdatePaymentMethodRequest) (error, string)
	ValidateListPaymentMethodsRequest(req *contract.ListPaymentMethodsRequest) (error, string)
}

type PaymentMethodValidatorImpl struct {
	validate *validator.Validate
}

func NewPaymentMethodValidator() PaymentMethodValidator {
	return &PaymentMethodValidatorImpl{
		validate: validator.New(),
	}
}

func (v *PaymentMethodValidatorImpl) ValidateCreatePaymentMethodRequest(req *contract.CreatePaymentMethodRequest) (error, string) {
	if err := v.validate.Struct(req); err != nil {
		return err, constants.ValidationErrorCode
	}

	if req.Name == "" {
		return constants.ErrPaymentMethodNameRequired, constants.MissingFieldErrorCode
	}

	if req.Type == "" {
		return constants.ErrPaymentMethodTypeRequired, constants.MissingFieldErrorCode
	}

	// Validate payment method type
	if !v.isValidPaymentMethodType(req.Type) {
		return constants.ErrInvalidPaymentMethodType, constants.InvalidFieldErrorCode
	}

	return nil, ""
}

func (v *PaymentMethodValidatorImpl) ValidateUpdatePaymentMethodRequest(req *contract.UpdatePaymentMethodRequest) (error, string) {
	if err := v.validate.Struct(req); err != nil {
		return err, constants.ValidationErrorCode
	}

	// Additional business logic validation
	if req.Name != nil && *req.Name == "" {
		return constants.ErrPaymentMethodNameRequired, constants.MissingFieldErrorCode
	}

	if req.Type != nil && *req.Type == "" {
		return constants.ErrPaymentMethodTypeRequired, constants.MissingFieldErrorCode
	}

	// Validate payment method type if provided
	if req.Type != nil && !v.isValidPaymentMethodType(*req.Type) {
		return constants.ErrInvalidPaymentMethodType, constants.InvalidFieldErrorCode
	}

	return nil, ""
}

func (v *PaymentMethodValidatorImpl) ValidateListPaymentMethodsRequest(req *contract.ListPaymentMethodsRequest) (error, string) {
	if err := v.validate.Struct(req); err != nil {
		return err, constants.ValidationErrorCode
	}

	// Additional business logic validation
	if req.Page < 1 {
		return constants.ErrInvalidPageNumber, constants.InvalidFieldErrorCode
	}

	if req.Limit < 1 || req.Limit > 100 {
		return constants.ErrInvalidLimit, constants.InvalidFieldErrorCode
	}

	// Validate payment method type if provided
	if req.Type != nil && !v.isValidPaymentMethodType(*req.Type) {
		return constants.ErrInvalidPaymentMethodType, constants.InvalidFieldErrorCode
	}

	return nil, ""
}

func (v *PaymentMethodValidatorImpl) isValidPaymentMethodType(paymentMethodType string) bool {
	validTypes := []string{
		string(constants.PaymentMethodTypeCash),
		string(constants.PaymentMethodTypeCard),
		string(constants.PaymentMethodTypeDigitalWallet),
		string(constants.PaymentMethodTypeQR),
		string(constants.PaymentMethodTypeEDC),
	}

	for _, validType := range validTypes {
		if paymentMethodType == validType {
			return true
		}
	}

	return false
}
