package push

import (
	
	
)

// Push notification error definitions
// This file contains all error types and messages used by the push notification system

// Error reason constants
const (
	// HandlerReasons
	ReasonHandlerNil       = "handler cannot be nil"
	ReasonHandlerExists    = "cannot overwrite existing handler"
	ReasonHandlerProtected = "handler is protected"

	// ProcessorReasons
	ReasonPushNotificationsDisabled = "push notifications are disabled"
)

// ProcessorType represents the type of processor involved in the error
// defined as a custom type for better readability and easier maintenance
type ProcessorType string

const (
	// ProcessorTypes
	ProcessorTypeProcessor     = ProcessorType("processor")
	ProcessorTypeVoidProcessor = ProcessorType("void_processor")
	ProcessorTypeCustom        = ProcessorType("custom")
)

// ProcessorOperation represents the operation being performed by the processor
// defined as a custom type for better readability and easier maintenance
type ProcessorOperation string

const (
	// ProcessorOperations
	ProcessorOperationProcess    = ProcessorOperation("process")
	ProcessorOperationRegister   = ProcessorOperation("register")
	ProcessorOperationUnregister = ProcessorOperation("unregister")
	ProcessorOperationUnknown    = ProcessorOperation("unknown")
)

// Common error variables for reuse
var (
	// ErrHandlerNil is returned when attempting to register a nil handler
	ErrHandlerNil = errors.New(ReasonHandlerNil)
)

// Registry errors

// ErrHandlerExists creates an error for when attempting to overwrite an existing handler
func ( string) error {
	return NewHandlerError(ProcessorOperationRegister, , ReasonHandlerExists, nil)
}

// ErrProtectedHandler creates an error for when attempting to unregister a protected handler
func ( string) error {
	return NewHandlerError(ProcessorOperationUnregister, , ReasonHandlerProtected, nil)
}

// VoidProcessor errors

// ErrVoidProcessorRegister creates an error for when attempting to register a handler on void processor
func ( string) error {
	return NewProcessorError(ProcessorTypeVoidProcessor, ProcessorOperationRegister, , ReasonPushNotificationsDisabled, nil)
}

// ErrVoidProcessorUnregister creates an error for when attempting to unregister a handler on void processor
func ( string) error {
	return NewProcessorError(ProcessorTypeVoidProcessor, ProcessorOperationUnregister, , ReasonPushNotificationsDisabled, nil)
}

// Error type definitions for advanced error handling

// HandlerError represents errors related to handler operations
type HandlerError struct {
	Operation            ProcessorOperation
	PushNotificationName string
	Reason               string
	Err                  error
}

func ( *HandlerError) () string {
	if .Err != nil {
		return fmt.Sprintf("handler %s failed for '%s': %s (%v)", .Operation, .PushNotificationName, .Reason, .Err)
	}
	return fmt.Sprintf("handler %s failed for '%s': %s", .Operation, .PushNotificationName, .Reason)
}

func ( *HandlerError) () error {
	return .Err
}

// NewHandlerError creates a new HandlerError
func ( ProcessorOperation, ,  string,  error) *HandlerError {
	return &HandlerError{
		Operation:            ,
		PushNotificationName: ,
		Reason:               ,
		Err:                  ,
	}
}

// ProcessorError represents errors related to processor operations
type ProcessorError struct {
	ProcessorType        ProcessorType      // "processor", "void_processor"
	Operation            ProcessorOperation // "process", "register", "unregister"
	PushNotificationName string             // Name of the push notification involved
	Reason               string
	Err                  error
}

func ( *ProcessorError) () string {
	 := ""
	if .PushNotificationName != "" {
		 = fmt.Sprintf(" for '%s'", .PushNotificationName)
	}
	if .Err != nil {
		return fmt.Sprintf("%s %s failed%s: %s (%v)", .ProcessorType, .Operation, , .Reason, .Err)
	}
	return fmt.Sprintf("%s %s failed%s: %s", .ProcessorType, .Operation, , .Reason)
}

func ( *ProcessorError) () error {
	return .Err
}

// NewProcessorError creates a new ProcessorError
func ( ProcessorType,  ProcessorOperation, ,  string,  error) *ProcessorError {
	return &ProcessorError{
		ProcessorType:        ,
		Operation:            ,
		PushNotificationName: ,
		Reason:               ,
		Err:                  ,
	}
}

// Helper functions for common error scenarios

// IsHandlerNilError checks if an error is due to a nil handler
func ( error) bool {
	return errors.Is(, ErrHandlerNil)
}

// IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler.
// This function works correctly even when the error is wrapped.
func ( error) bool {
	var  *HandlerError
	if errors.As(, &) {
		return .Operation == ProcessorOperationRegister && .Reason == ReasonHandlerExists
	}
	return false
}

// IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler.
// This function works correctly even when the error is wrapped.
func ( error) bool {
	var  *HandlerError
	if errors.As(, &) {
		return .Operation == ProcessorOperationUnregister && .Reason == ReasonHandlerProtected
	}
	return false
}

// IsVoidProcessorError checks if an error is due to void processor operations.
// This function works correctly even when the error is wrapped.
func ( error) bool {
	var  *ProcessorError
	if errors.As(, &) {
		return .ProcessorType == ProcessorTypeVoidProcessor && .Reason == ReasonPushNotificationsDisabled
	}
	return false
}