content: package com.example.auction.controller;
  
  import com.example.auction.dto.AuctionCreateDTO;
  import com.example.auction.dto.BidCreateDTO;
  import com.example.auction.dto.ErrorResponse;
  import com.example.auction.exception.AuctionException;
  import com.example.auction.exception.AuctionNotFoundException;
  import com.example.auction.model.Auction;
  import com.example.auction.model.Bid;
  import com.example.auction.service.AuctionService;
  import jakarta.validation.Valid;
  import jakarta.validation.constraints.Max;
  import jakarta.validation.constraints.Min;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.validation.FieldError;
  import org.springframework.web.bind.MethodArgumentNotValidException;
  import org.springframework.web.bind.annotation.*;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  @RestController
  @RequestMapping("/api")
  @CrossOrigin(origins = "*")
  public class AuctionController {
      private static final Logger logger = LoggerFactory.getLogger(AuctionController.class);
      
      @Autowired
      private AuctionService auctionService;
      
      @GetMapping("/health")
      public ResponseEntity<Map<String, String>> healthCheck() {
          Map<String, String> response = new HashMap<>();
          response.put("status", "healthy");
          response.put("backend", "java");
          return ResponseEntity.ok(response);
      }
      
      @GetMapping("/auctions")
      public ResponseEntity<List<Auction>> getAuctions(
              @RequestParam(required = false) String status,
              @RequestParam(required = false) String sort) {
          try {
              List<Auction> auctions = auctionService.getAuctions(status, sort);
              return ResponseEntity.ok(auctions);
          } catch (AuctionException e) {
              logger.error("Error fetching auctions", e);
              throw e;
          }
      }
      
      @PostMapping("/auctions")
      public ResponseEntity<Auction> createAuction(@Valid @RequestBody AuctionCreateDTO dto) {
          try {
              // Java's @Valid annotation ensures type safety at compile time
              Auction auction = auctionService.createAuction(dto);
              return ResponseEntity.status(HttpStatus.CREATED).body(auction);
          } catch (Exception e) {
              logger.error("Error creating auction", e);
              throw new AuctionException("Failed to create auction: " + e.getMessage());
          }
      }
      
      @GetMapping("/auctions/{auctionId}")
      public ResponseEntity<Auction> getAuction(@PathVariable String auctionId) {
          Auction auction = auctionService.getAuction(auctionId);
          return ResponseEntity.ok(auction);
      }
      
      @PostMapping("/auctions/{auctionId}/bid")
      public ResponseEntity<Bid> placeBid(
              @PathVariable String auctionId,
              @Valid @RequestBody BidCreateDTO dto) {
          try {
              // Strong typing prevents type coercion issues
              Bid bid = auctionService.placeBid(auctionId, dto);
              return ResponseEntity.status(HttpStatus.CREATED).body(bid);
          } catch (AuctionException e) {
              logger.warn("Invalid bid attempt: {}", e.getMessage());
              throw e;
          }
      }
      
      @GetMapping("/auctions/{auctionId}/bids")
      public ResponseEntity<List<Bid>> getBids(
              @PathVariable String auctionId,
              @RequestParam(defaultValue = "50") @Min(1) @Max(100) int limit) {
          List<Bid> bids = auctionService.getBids(auctionId, limit);
          return ResponseEntity.ok(bids);
      }
      
      @PostMapping("/auctions/{auctionId}/close")
      public ResponseEntity<Auction> closeAuction(@PathVariable String auctionId) {
          Auction auction = auctionService.closeAuction(auctionId);
          return ResponseEntity.ok(auction);
      }
      
      @DeleteMapping("/reset")
      public ResponseEntity<Map<String, String>> resetDatabase() {
          try {
              auctionService.resetDatabase();
              Map<String, String> response = new HashMap<>();
              response.put("message", "Database reset successfully");
              return ResponseEntity.ok(response);
          } catch (Exception e) {
              logger.error("Error resetting database", e);
              throw new AuctionException("Failed to reset database");
          }
      }
      
      // Exception handlers with strong typing
      @ExceptionHandler(AuctionNotFoundException.class)
      public ResponseEntity<ErrorResponse> handleAuctionNotFound(AuctionNotFoundException e) {
          ErrorResponse error = new ErrorResponse("NOT_FOUND", e.getMessage());
          return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
      }
      
      @ExceptionHandler(AuctionException.class)
      public ResponseEntity<ErrorResponse> handleAuctionException(AuctionException e) {
          ErrorResponse error = new ErrorResponse("AUCTION_ERROR", e.getMessage());
          return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
      }
      
      @ExceptionHandler(MethodArgumentNotValidException.class)
      public ResponseEntity<ErrorResponse> handleValidationException(MethodArgumentNotValidException e) {
          Map<String, Object> details = new HashMap<>();
          e.getBindingResult().getAllErrors().forEach((error) -> {
              String fieldName = ((FieldError) error).getField();
              String errorMessage = error.getDefaultMessage();
              details.put(fieldName, errorMessage);
          });
          
          ErrorResponse error = new ErrorResponse("VALIDATION_ERROR", "Invalid request data", details);
          return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
      }
      
      @ExceptionHandler(Exception.class)
      public ResponseEntity<ErrorResponse> handleGenericException(Exception e) {
          logger.error("Unexpected error", e);
          ErrorResponse error = new ErrorResponse("INTERNAL_ERROR", "An unexpected error occurred");
          return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error);
      }
  }