package app_error

import (
	"fmt"
)

// AppError is the standard error type used throughout the application.
type AppError struct {
	Code    ErrorCode `json:"code"`
	Message string    `json:"message"`
	Status  int       `json:"status"` // HTTP status code
	Err     error     `json:"-"`      // Original error, if any
}

// Error satisfies the error interface.
func (e *AppError) Error() string {
	if e.Err != nil {
		return fmt.Sprintf("Code: %s, Message: %s, Original Error: %s", e.Code, e.Message, e.Err.Error())
	}
	return fmt.Sprintf("Code: %s, Message: %s", e.Code, e.Message)
}

// Wrap wraps an existing error into an AppError.
func Wrap(err error, code ErrorCode, message string, status int) *AppError {
	return &AppError{
		Code:    code,
		Message: message,
		Status:  status,
		Err:     err,
	}
}

// New creates a new AppError.
func New(code ErrorCode, message string, status int) *AppError {
	return &AppError{
		Code:    code,
		Message: message,
		Status:  status,
	}
}