package com.tasteby.controller;

import com.tasteby.domain.Restaurant;
import com.tasteby.service.SearchService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

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

    private final SearchService searchService;

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

    @GetMapping
    public List<Restaurant> search(
            @RequestParam String q,
            @RequestParam(defaultValue = "keyword") String mode,
            @RequestParam(defaultValue = "20") int limit) {
        // #293 — q 빈값 가드: '%%' LIKE로 응답 폭발 차단
        if (q == null || q.isBlank()) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "검색어가 필요합니다");
        }
        if (limit > 100) limit = 100;
        if (limit < 1) limit = 1;
        return searchService.search(q.trim(), mode, limit);
    }
}
