package com.rtlabs.search.api;

import jakarta.validation.constraints.Pattern;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/search")
public class SearchController {

  private final SearchService searchService;

  public SearchController(SearchService searchService) {
    this.searchService = searchService;
  }

  @GetMapping("/users")
  public ResponseEntity<List<UserSummary>> searchUsers(
      @RequestParam("q")
      @Pattern(regexp = "^([\\w\\s]+\\.?)+$", message = "query invalid")
      String query) {
    return ResponseEntity.ok(searchService.findUsers(query));
  }
}
