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.List;
import java.util.Set;

import javax.validation.constraints.Pattern;

import io.delphiplatform.api.v3.constant.ApplicationConstants;
import io.delphiplatform.api.v3.model.SortOrder;
import io.delphiplatform.api.v3.model.amazon.AmazonStationsStreamsResults;
import io.delphiplatform.api.v3.validation.ExactMatchesForSingleValue;
import io.delphiplatform.api.v3.view.interceptor.OnlyDeclaredParams;
import io.delphiplatform.api.v3.view.util.Params;
import lombok.NonNull;

import static io.delphiplatform.api.v3.bigtable.processing.AmazonStationsSortingService.SORT_STATION_ID;
import static io.delphiplatform.api.v3.bigtable.processing.AmazonStationsSortingService.SORT_STATION_NAME;
import static io.delphiplatform.api.v3.bigtable.processing.AmazonStationsSortingService.SORT_WORLDWIDE_STREAMING_1_DAY_ISRC;
import static io.delphiplatform.api.v3.bigtable.processing.AmazonStationsSortingService.SORT_WORLDWIDE_STREAMING_7_DAYS_ISRC;
import static io.delphiplatform.api.v3.bigtable.processing.AmazonStationsSortingService.SORT_WORLDWIDE_STREAMING_7_DAYS_STATION;
import static io.delphiplatform.api.v3.constant.ApplicationConstants.COUNTRY_CODE_PATTERN;

@Validated
public interface StationsApi {

    ResponseEntity<AmazonStationsStreamsResults> getAmazonStationsStreams(@NonNull Params params);

    @OnlyDeclaredParams
    @RequestMapping(value = "/amazon/stations",
        produces = {"application/json"},
        method = RequestMethod.GET)
    default ResponseEntity<AmazonStationsStreamsResults> getAmazonStations(
        @RequestParam(value = "isrc")
            List<@Pattern(regexp = ApplicationConstants.ISRC_PATTERN) String> isrc,
        @RequestParam(value = "country_code", required = false)
            Set<@Pattern(regexp = COUNTRY_CODE_PATTERN) String> countryCode,
        @ExactMatchesForSingleValue(message = ApplicationConstants.ERROR_MESSAGE_SORT_BY_PARAMETER, anyOf = {
            SORT_STATION_ID, SORT_STATION_NAME, SORT_WORLDWIDE_STREAMING_1_DAY_ISRC,
            SORT_WORLDWIDE_STREAMING_7_DAYS_ISRC, SORT_WORLDWIDE_STREAMING_7_DAYS_STATION
        })
        @RequestParam(value = "sort_by", required = false) String sortBy,
        @RequestParam(value = "sort_order", required = false, defaultValue = "desc") SortOrder sortOrder
    ) {
        return getAmazonStationsStreams(Params.builder()
            .isrc(isrc)
            .countryCode(countryCode)
            .sortBy(sortBy)
            .sortOrder(sortOrder)
            .build());
    }
}
