package io.delphiplatform.api.v3.view;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
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.time.LocalDate;
import java.util.concurrent.CompletableFuture;

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

import io.delphiplatform.api.v3.constant.ApplicationConstants;
import io.delphiplatform.api.v3.model.GroupByField;
import io.delphiplatform.api.v3.model.Popularities;
import io.delphiplatform.api.v3.model.SortOrder;
import io.delphiplatform.api.v3.view.interceptor.OnlyDeclaredParams;
import io.delphiplatform.api.v3.view.util.Params;

@Validated

public interface PopularityApi {

    /**
     * GET /popularity : Query for Spotify popularity Resource route for getting a list of &#x60;Popularity&#x60; with
     * filtering based on provided parameters. At least one of the identifying keys is required in the request:
     * &#x60;artist_id&#x60; / &#x60;product_id&#x60; / &#x60;track_id&#x60; or &#x60;isrc&#x60;.
     *
     * @param startDate  The earliest date to include in the query range. Data is start_date inclusive. (required)
     * @param endDate    The latest date to include in the query. Data returned is end_date inclusive. (required)
     * @param artistId   This &#x60;artist_id&#x60; query parameter is required (required)
     * @param groupBy    Specific time-based term to group the data by (optional)
     * @param limit      The maximum number of &#x60;items&#x60; to return in a single request. (optional)
     * @param offset     The number of &#x60;items&#x60; to offset for pagination. (optional)
     * @param limitRange Limit of the range to query in days to return in a single request. Format: `days:28`
     *                   (optional)
     * @param cursor     Cursor to query, containing start date in format `date:2020-01-01` to return in a single
     *                   request. (optional)
     * @param sortBy     Field name by which to sort the data. (optional)
     * @param sortOrder  Direction to sort the data. Default: &#x60;desc&#x60; if &#x60;sort_by&#x60; provided.
     *                   (optional, default to desc)
     * @return &#x60;Popularity&#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 = "/popularity",
        produces = {"application/json"},
        method = RequestMethod.GET)
    default CompletableFuture<ResponseEntity<Popularities>> popularitySearch(
        @NotNull @RequestParam(value = "start_date")
        @DateTimeFormat(iso = ISO.DATE) LocalDate startDate,
        @NotNull @RequestParam(value = "end_date")
        @DateTimeFormat(iso = ISO.DATE) LocalDate endDate,
        @Pattern(regexp = ApplicationConstants.GRAS_ID_PATTERN)
        @NotNull @RequestParam(value = "artist_id") String artistId,
        @RequestParam(value = "group_by", required = false) GroupByField groupBy,
        @Min(0) @Max(ApplicationConstants.MAX_BIGTABLE_PAGE_SIZE)
        @RequestParam(value = "limit", required = false) Integer limit,
        @Min(0) @RequestParam(value = "offset", required = false) Integer offset,
        @Pattern(regexp = ApplicationConstants.PAGE_LIMIT_RANGE_PATTERN)
        @RequestParam(value = "limit_range", required = false) String limitRange,
        @Pattern(regexp = ApplicationConstants.PAGE_CURSOR_PATTERN)
        @RequestParam(value = "cursor", required = false) String cursor,
        @RequestParam(value = "sort_by", required = false) String sortBy,
        @RequestParam(value = "sort_order", required = false, defaultValue = "desc") SortOrder sortOrder
    ) {
        return getSearchData(Params.builder()
            .startDate(startDate)
            .endDate(endDate)
            .artistId(artistId)
            .groupByField(groupBy)
            .limit(limit)
            .offset(offset)
            .limitRange(limitRange)
            .cursor(cursor)
            .sortBy(sortBy)
            .sortOrder(sortOrder)
            .build());
    }

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

}
