package validator

import (
	"errors"
	"strings"

	"apskel-pos-be/internal/constants"
	"apskel-pos-be/internal/contract"
)

type VendorValidator interface {
	ValidateCreateVendorRequest(req *contract.CreateVendorRequest) (error, string)
	ValidateUpdateVendorRequest(req *contract.UpdateVendorRequest) (error, string)
	ValidateListVendorsRequest(req *contract.ListVendorsRequest) (error, string)
}

type VendorValidatorImpl struct{}

func NewVendorValidator() *VendorValidatorImpl {
	return &VendorValidatorImpl{}
}

func (v *VendorValidatorImpl) ValidateCreateVendorRequest(req *contract.CreateVendorRequest) (error, string) {
	if req == nil {
		return errors.New("request body is required"), constants.MissingFieldErrorCode
	}

	if strings.TrimSpace(req.Name) == "" {
		return errors.New("name is required"), constants.MissingFieldErrorCode
	}

	if len(req.Name) < 1 || len(req.Name) > 255 {
		return errors.New("name must be between 1 and 255 characters"), constants.MalformedFieldErrorCode
	}

	if req.Email != nil && strings.TrimSpace(*req.Email) != "" {
		if !isValidEmail(*req.Email) {
			return errors.New("email format is invalid"), constants.MalformedFieldErrorCode
		}
	}

	if req.PhoneNumber != nil && strings.TrimSpace(*req.PhoneNumber) != "" {
		if !isValidPhone(*req.PhoneNumber) {
			return errors.New("phone_number format is invalid"), constants.MalformedFieldErrorCode
		}
	}

	if req.ContactPerson != nil && len(*req.ContactPerson) > 255 {
		return errors.New("contact_person must be at most 255 characters"), constants.MalformedFieldErrorCode
	}

	if req.TaxNumber != nil && len(*req.TaxNumber) > 50 {
		return errors.New("tax_number must be at most 50 characters"), constants.MalformedFieldErrorCode
	}

	if req.PaymentTerms != nil && len(*req.PaymentTerms) > 100 {
		return errors.New("payment_terms must be at most 100 characters"), constants.MalformedFieldErrorCode
	}

	return nil, ""
}

func (v *VendorValidatorImpl) ValidateUpdateVendorRequest(req *contract.UpdateVendorRequest) (error, string) {
	if req == nil {
		return errors.New("request body is required"), constants.MissingFieldErrorCode
	}

	if req.Name != nil {
		if strings.TrimSpace(*req.Name) == "" {
			return errors.New("name cannot be empty"), constants.MalformedFieldErrorCode
		}
		if len(*req.Name) < 1 || len(*req.Name) > 255 {
			return errors.New("name must be between 1 and 255 characters"), constants.MalformedFieldErrorCode
		}
	}

	if req.Email != nil && strings.TrimSpace(*req.Email) != "" {
		if !isValidEmail(*req.Email) {
			return errors.New("email format is invalid"), constants.MalformedFieldErrorCode
		}
	}

	if req.PhoneNumber != nil && strings.TrimSpace(*req.PhoneNumber) != "" {
		if !isValidPhone(*req.PhoneNumber) {
			return errors.New("phone_number format is invalid"), constants.MalformedFieldErrorCode
		}
	}

	if req.ContactPerson != nil && len(*req.ContactPerson) > 255 {
		return errors.New("contact_person must be at most 255 characters"), constants.MalformedFieldErrorCode
	}

	if req.TaxNumber != nil && len(*req.TaxNumber) > 50 {
		return errors.New("tax_number must be at most 50 characters"), constants.MalformedFieldErrorCode
	}

	if req.PaymentTerms != nil && len(*req.PaymentTerms) > 100 {
		return errors.New("payment_terms must be at most 100 characters"), constants.MalformedFieldErrorCode
	}

	return nil, ""
}

func (v *VendorValidatorImpl) ValidateListVendorsRequest(req *contract.ListVendorsRequest) (error, string) {
	if req == nil {
		return errors.New("request body is required"), constants.MissingFieldErrorCode
	}

	if req.Page < 1 {
		return errors.New("page must be at least 1"), constants.MalformedFieldErrorCode
	}

	if req.Limit < 1 || req.Limit > 100 {
		return errors.New("limit must be between 1 and 100"), constants.MalformedFieldErrorCode
	}

	return nil, ""
}
