package validator

import (
	"errors"
	"strings"

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

type OrganizationValidator interface {
	ValidateCreateOrganizationRequest(req *contract.CreateOrganizationRequest) (error, string)
	ValidateUpdateOrganizationRequest(req *contract.UpdateOrganizationRequest) (error, string)
	ValidateListOrganizationsRequest(req *contract.ListOrganizationsRequest) (error, string)
}

type OrganizationValidatorImpl struct{}

func NewOrganizationValidator() *OrganizationValidatorImpl {
	return &OrganizationValidatorImpl{}
}

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

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

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

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

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

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

	if !isValidPlanType(req.PlanType) {
		return errors.New("invalid plan_type"), constants.MalformedFieldErrorCode
	}

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

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

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

	if !isValidEmail(req.AdminEmail) {
		return errors.New("admin_email format is invalid"), constants.MalformedFieldErrorCode
	}

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

	if len(req.AdminPassword) < 6 {
		return errors.New("admin_password must be at least 6 characters"), constants.MalformedFieldErrorCode
	}

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

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

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

	if len(req.OutletCurrency) != 3 {
		return errors.New("outlet_currency must be exactly 3 characters"), constants.MalformedFieldErrorCode
	}

	return nil, ""
}

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

	// At least one field should be provided for update
	if req.Name == nil && req.Email == nil && req.PhoneNumber == nil && req.PlanType == nil {
		return errors.New("at least one field must be provided for update"), 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 {
		if strings.TrimSpace(*req.Email) == "" {
			return errors.New("email cannot be empty"), constants.MalformedFieldErrorCode
		}
		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.PlanType != nil {
		if strings.TrimSpace(*req.PlanType) == "" {
			return errors.New("plan_type cannot be empty"), constants.MalformedFieldErrorCode
		}
		if !isValidPlanType(*req.PlanType) {
			return errors.New("invalid plan_type"), constants.MalformedFieldErrorCode
		}
	}

	return nil, ""
}

func (v *OrganizationValidatorImpl) ValidateListOrganizationsRequest(req *contract.ListOrganizationsRequest) (error, string) {
	if req == nil {
		return errors.New("request 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
	}

	if req.PlanType != "" && !isValidPlanType(req.PlanType) {
		return errors.New("invalid plan_type"), constants.MalformedFieldErrorCode
	}

	return nil, ""
}

// Helper functions moved to validator_helpers.go
