package notification

import (
	"fmt"
	"net/http"
)

// Error types for notification SDK
var (
	ErrNotificationServiceUnavailable = fmt.Errorf("notification service unavailable")
	ErrInvalidRequest                 = fmt.Errorf("invalid notification request")
	ErrNetworkTimeout                 = fmt.Errorf("network timeout")
	ErrUnauthorized                   = fmt.Errorf("unauthorized request")
	ErrInternalError                  = fmt.Errorf("internal notification service error")
)

// ErrInvalidConfig represents configuration validation errors
type ErrInvalidConfig struct {
	Field   string
	Message string
}

func (e ErrInvalidConfig) Error() string {
	return fmt.Sprintf("invalid config field %s: %s", e.Field, e.Message)
}

// ErrHTTP represents HTTP-related errors with status codes
type ErrHTTP struct {
	StatusCode int
	Message    string
	Body       string
}

func (e ErrHTTP) Error() string {
	return fmt.Sprintf("HTTP %d: %s", e.StatusCode, e.Message)
}

// ErrValidation represents validation errors
type ErrValidation struct {
	Field   string
	Message string
}

func (e ErrValidation) Error() string {
	return fmt.Sprintf("validation error for field %s: %s", e.Field, e.Message)
}

// ErrRetryExhausted represents retry exhaustion errors
type ErrRetryExhausted struct {
	Attempts    int
	LastError   error
	ElapsedTime string
}

func (e ErrRetryExhausted) Error() string {
	return fmt.Sprintf("retry exhausted after %d attempts (%s): %v", e.Attempts, e.ElapsedTime, e.LastError)
}

// MapHTTPError maps HTTP status codes to appropriate errors
func MapHTTPError(statusCode int, body string) error {
	switch statusCode {
	case http.StatusBadRequest:
		return ErrHTTP{StatusCode: statusCode, Message: "bad request", Body: body}
	case http.StatusUnauthorized:
		return ErrHTTP{StatusCode: statusCode, Message: "unauthorized", Body: body}
	case http.StatusForbidden:
		return ErrHTTP{StatusCode: statusCode, Message: "forbidden", Body: body}
	case http.StatusNotFound:
		return ErrHTTP{StatusCode: statusCode, Message: "not found", Body: body}
	case http.StatusUnprocessableEntity:
		return ErrHTTP{StatusCode: statusCode, Message: "validation error", Body: body}
	case http.StatusInternalServerError:
		return ErrHTTP{StatusCode: statusCode, Message: "internal server error", Body: body}
	case http.StatusBadGateway:
		return ErrHTTP{StatusCode: statusCode, Message: "bad gateway", Body: body}
	case http.StatusServiceUnavailable:
		return ErrHTTP{StatusCode: statusCode, Message: "service unavailable", Body: body}
	case http.StatusGatewayTimeout:
		return ErrHTTP{StatusCode: statusCode, Message: "gateway timeout", Body: body}
	default:
		return ErrHTTP{StatusCode: statusCode, Message: "unknown error", Body: body}
	}
}
