package com.zioinfo.kintex.work.search;

import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 통합검색 API (/api/work/search). 인증 사용자. */
@RestController
@RequestMapping("/api/work/search")
public class SearchController {

    private final SearchService service;
    private final EventAccessGuard guard;

    public SearchController(SearchService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    @GetMapping
    public ApiResponse<List<SearchResultItem>> search(@AuthenticationPrincipal KintexPrincipal principal,
                                                      @RequestParam("q") String q,
                                                      @RequestParam(defaultValue = "30") int limit) {
        guard.require(principal);
        return ApiResponse.ok(service.search(principal, q, limit));
    }
}
