content: package org.hiast.analyticsapi.adapter.in.web;
  
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.MethodArgumentNotValidException;
  import org.springframework.web.bind.annotation.ControllerAdvice;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
  
  import jakarta.validation.ConstraintViolationException;
  import java.time.Instant;
  
  /**
   * Global exception handler for the analytics API.
   * Provides consistent error responses across all endpoints.
   */
  @ControllerAdvice
  public class GlobalExceptionHandler {
  
      @ExceptionHandler(IllegalArgumentException.class)
      public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException ex) {
          ErrorResponse error = new ErrorResponse(
                  "INVALID_ARGUMENT",
                  ex.getMessage(),
                  Instant.now().toString()
          );
          return ResponseEntity.badRequest().body(error);
      }
  
      @ExceptionHandler(MethodArgumentTypeMismatchException.class)
      public ResponseEntity<ErrorResponse> handleTypeMismatchException(MethodArgumentTypeMismatchException ex) {
          String message = String.format("Invalid value '%s' for parameter '%s'", 
                  ex.getValue(), ex.getName());
          ErrorResponse error = new ErrorResponse(
                  "INVALID_PARAMETER",
                  message,
                  Instant.now().toString()
          );
          return ResponseEntity.badRequest().body(error);
      }
  
      @ExceptionHandler(MethodArgumentNotValidException.class)
      public ResponseEntity<ErrorResponse> handleValidationException(MethodArgumentNotValidException ex) {
          ErrorResponse error = new ErrorResponse(
                  "VALIDATION_ERROR",
                  "Request validation failed",
                  Instant.now().toString()
          );
          return ResponseEntity.badRequest().body(error);
      }
  
      @ExceptionHandler(ConstraintViolationException.class)
      public ResponseEntity<ErrorResponse> handleConstraintViolationException(ConstraintViolationException ex) {
          ErrorResponse error = new ErrorResponse(
                  "CONSTRAINT_VIOLATION",
                  ex.getMessage(),
                  Instant.now().toString()
          );
          return ResponseEntity.badRequest().body(error);
      }
  
      @ExceptionHandler(Exception.class)
      public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
          ErrorResponse error = new ErrorResponse(
                  "INTERNAL_ERROR",
                  "An unexpected error occurred",
                  Instant.now().toString()
          );
          return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
      }
  
      public static class ErrorResponse {
          private final String code;
          private final String message;
          private final String timestamp;
  
          public ErrorResponse(String code, String message, String timestamp) {
              this.code = code;
              this.message = message;
              this.timestamp = timestamp;
          }
  
          public String getCode() {
              return code;
          }
  
          public String getMessage() {
              return message;
          }
  
          public String getTimestamp() {
              return timestamp;
          }
      }
  } 