package io.delphiplatform.api.v3.view.tiktok;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.server.ResponseStatusException;

import java.util.concurrent.CompletableFuture;

import javax.transaction.NotSupportedException;

import io.delphiplatform.api.v3.bigtable.BigtableTikTokTrackAnalyticsService;
import io.delphiplatform.api.v3.model.tiktok.TikTokTrackAnalytics;
import io.delphiplatform.api.v3.view.util.Params;

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

    private final BigtableTikTokTrackAnalyticsService trackAnalyticsService;

    public TikTokApiController(
        BigtableTikTokTrackAnalyticsService trackAnalyticsService) {
        this.trackAnalyticsService = trackAnalyticsService;
    }

    @Override
    public CompletableFuture<ResponseEntity<TikTokTrackAnalytics>> getTikTokAnalytics(Params params) {
        try {
            return trackAnalyticsService.getAnalytics(params).thenApply(ResponseEntity::ok);
        } catch (NotSupportedException e) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
        }
    }
}
