package io.delphiplatform.api.v3.view;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import io.delphiplatform.api.v3.model.ads.LinkfireLink;
import io.delphiplatform.api.v3.model.ads.LinkfireLinks;
import io.delphiplatform.api.v3.search.service.LinkfireLinkSearchService;
import io.delphiplatform.api.v3.view.util.Params;

@Controller
@RequestMapping("${spring.data.rest.base-path:/v3}")
public class SearchLinkfireLinksApiController implements SearchLinkfireLinksApi {

    private final LinkfireLinkSearchService linkfireLinkSearchService;

    @Autowired
    public SearchLinkfireLinksApiController(LinkfireLinkSearchService linkfireLinkSearchService) {
        this.linkfireLinkSearchService = linkfireLinkSearchService;
    }

    @Override
    public CompletableFuture<ResponseEntity<Object>> getSearchData(Params params) {
        return CompletableFuture.supplyAsync(() -> {
            List<LinkfireLink> linkfireLinks = linkfireLinkSearchService
                .searchAndConvertAllByLinkUrlWithRdb(params.getQuery(), params.getLimit(), params.getPage());
            return ResponseEntity.ok(new LinkfireLinks().items(linkfireLinks).count(linkfireLinks.size()));
        });
    }
}
