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.concurrent.CompletableFuture;

import io.delphiplatform.api.util.PaginatedList;
import io.delphiplatform.api.v3.model.ArtistSimple;
import io.delphiplatform.api.v3.model.Artists;
import io.delphiplatform.api.v3.search.service.ArtistSearchService;
import io.delphiplatform.api.v3.view.util.Params;

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

    private final ArtistSearchService artistSearchService;

    @Autowired
    public SearchApiController(ArtistSearchService artistSearchService) {
        this.artistSearchService = artistSearchService;
    }

    @Override
    public CompletableFuture<ResponseEntity<Object>> getArtistSearchData(Params params) {
        return CompletableFuture.supplyAsync(() -> {
            PaginatedList<ArtistSimple> artists = artistSearchService
                .searchAndConvertAllByFullNameWithRdb(params.getQuery(), params.getLimit(), params.getPage());
            return ResponseEntity.ok(new Artists().items(artists).count(artists.size()).totalCount(artists.totalCount()));
        });
    }
}
