package io.delphiplatform.api.v3.view;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.concurrent.CompletableFuture;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import io.delphiplatform.api.v3.constant.ApplicationConstants;
import io.delphiplatform.api.v3.model.IndexParam;
import io.delphiplatform.api.v3.view.interceptor.OnlyDeclaredParams;
import io.delphiplatform.api.v3.view.util.Params;

@Validated
public interface SearchLinkfireLinksApi {

    /**
     * GET /search/linkfire/links: Search Elasticsearch linkfire-link index via query string
     *
     * @param query Elasticsearch query string (required)
     * @param limit The maximum number of &#x60;items&#x60; to return in a single request. (optional)
     * @param page  The page number (optional)
     * @return &#x60;LinkfireLinks&#x60; response object (status code 200) or 400 Invalid Input Response (status code
     * 400) or 401 Unauthorized (AuthError) Response (status code 401) or 500 Internal Error Response (status code 500)
     */
    @OnlyDeclaredParams
    @RequestMapping(value = "/search/linkfire/links",
        produces = {"application/json"},
        method = RequestMethod.GET)
    default CompletableFuture<ResponseEntity<Object>> search(
        @RequestParam(value = "query", required = true) String query,
        @Min(0) @Max(ApplicationConstants.MAX_PAGE_SIZE)
        @RequestParam(value = "limit", required = false) Integer limit,
        @Min(0) @RequestParam(value = "page", required = false, defaultValue = "0") Integer page
    ) {
        limit = limit == null ? ApplicationConstants.DEFAULT_SEARCH_LIMIT : limit;
        Params params = Params.builder()
            .index(IndexParam.LINKFIRE_LINK)
            .query(query)
            .limit(limit)
            .page(page)
            .build();
        return getSearchData(params);
    }

    CompletableFuture<ResponseEntity<Object>> getSearchData(Params params);

}
