package de.effigenix.infrastructure.shared.web.exception;

import io.swagger.v3.oas.annotations.media.Schema;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * Standard error response format for all API errors.
 *
 * Provides consistent error structure across all endpoints.
 * Used by GlobalExceptionHandler for all error responses.
 */
@Schema(description = "Standard error response format")
public record ErrorResponse(
        @Schema(description = "Error code", example = "USER_NOT_FOUND")
        String code,

        @Schema(description = "Error message", example = "User with ID 'user-123' not found")
        String message,

        @Schema(description = "HTTP status code", example = "404")
        int status,

        @Schema(description = "Timestamp when error occurred")
        OffsetDateTime timestamp,

        @Schema(description = "Request path where error occurred", example = "/api/users/user-123")
        String path,

        @Schema(description = "Validation errors (for 400 Bad Request)")
        List<ValidationError> validationErrors
) {
    /**
     * Creates an ErrorResponse from an ApplicationError.
     */
    public static ErrorResponse from(
            String code,
            String message,
            int status,
            String path
    ) {
        return new ErrorResponse(
                code,
                message,
                status,
                OffsetDateTime.now(ZoneOffset.UTC),
                path,
                null
        );
    }

    /**
     * Creates an ErrorResponse with validation errors.
     */
    public static ErrorResponse withValidationErrors(
            String message,
            int status,
            String path,
            List<ValidationError> validationErrors
    ) {
        return new ErrorResponse(
                "VALIDATION_ERROR",
                message,
                status,
                OffsetDateTime.now(ZoneOffset.UTC),
                path,
                validationErrors
        );
    }

    /**
     * Represents a single validation error.
     */
    @Schema(description = "Validation error for a specific field")
    public record ValidationError(
            @Schema(description = "Field name", example = "username")
            String field,

            @Schema(description = "Error message", example = "Username is required")
            String message
    ) {
    }
}
