package com.infoclinika.pdx.controller;

import com.infoclinika.pdx.projection.AggregateProjectStatistics;
import com.infoclinika.pdx.service.annotation.PrimarySiteService;
import com.infoclinika.pdx.service.annotation.ProjectService;
import com.infoclinika.pdx.specification.FilterQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Handles requests to get project data.
 */
@RestController
@RequestMapping("/api/project")
public class ProjectController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final ProjectService projectService;
    private final PrimarySiteService primarySiteService;

    /**
     * Constructor to provide required services.
     */
    @Autowired
    public ProjectController(ProjectService projectService, PrimarySiteService primarySiteService) {
        this.projectService = projectService;
        this.primarySiteService = primarySiteService;
    }

    /**
     * Returns statistics by main categories.
     */
    @GetMapping("/statistics")
    public List<AggregateProjectStatistics> getStatisticsByCategories(
            @RequestParam(name = "queries", required = false) List<FilterQuery> queries) {
        logger.debug("Request to /api/project/statistics?queries={}", queries);

        final List<FilterQuery> filterQueries = primarySiteService.getQueriesWithIgnoredCategories(queries);
        return projectService.getStatistics(filterQueries);
    }
}
