package f1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import f1.db.DriverDao;
import f1.db.TeamDao;
import f1.entity.Driver;
import f1.entity.DriverHistoryItem;
import f1.entity.DriverStatistic;
import f1.entity.QualifyingResultItem;
import f1.entity.RaceResultItem;
import f1.entity.SeasonDriver;
import f1.entity.SeasonScheduleItem;
import f1.entity.Team;
import f1.entity.TeamHistoryItem;
import f1.entity.TeamStatistic;
import f1.db.RaceDao;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api")
public class F1Controller {

    @Autowired
    private DriverDao driverDao;
    @Autowired
    private TeamDao teamDao;
    @Autowired
    private RaceDao raceDao;

    @GetMapping("/drivers")
    public List<Driver> getAllDrivers() {
        return driverDao.getAllDrivers();
    }

    @GetMapping("/teams")
    public List<Team> getAllTeams() {
        return teamDao.getAllTeams();
    }

    @GetMapping("/season-drivers")
    public List<SeasonDriver> getDriversBySeason(@RequestParam int season) {
        return driverDao.getDriversBySeason(season);
    }

    @GetMapping("/standings/teams")
    public List<Map<String, Object>> getSeasonTeamStandings(@RequestParam int season) {
        return teamDao.getSeasonTop5TeamStandings(season);
    }

    @GetMapping("/standings/drivers")
    public List<Map<String, Object>> getSeasonDriverStandings(@RequestParam int season) {
        return driverDao.getSeasonTop5DriverStandings(season);
    }

    @GetMapping("/prix")
    public List<SeasonScheduleItem> getSeasonSchedule(@RequestParam int season) {
        return raceDao.getSeasonSchedule(season);
    }

    @GetMapping("/teams/{id}/drivers")
    public List<String> getDriverNames(@PathVariable int id, @RequestParam int season) {
        return driverDao.getDriverNamesByTeamAndSeason(id, season);
    }

    @GetMapping("/drivers/{id}/statistics")
    public Map<String, DriverStatistic> getDriverStatistic(@PathVariable int id, @RequestParam int season) {
        Map<String, DriverStatistic> result = new HashMap<>();
        result.put("formal", driverDao.getDriverStatistic(id, season, false));
        result.put("sprint", driverDao.getDriverStatistic(id, season, true));
        return result;
    }

    @GetMapping("/teams/{id}/statistics")
    public TeamStatistic getTeamStatistic(@PathVariable int id, @RequestParam int season) {
        DriverStatistic formal = teamDao.getTeamStatistic(id, season, false);
        DriverStatistic sprint = teamDao.getTeamStatistic(id, season, true);
        List<String> drivers = driverDao.getDriverNamesByTeamAndSeason(id, season);

        Map<String, Integer> standing = teamDao.getTeamStanding(id, season);
        int rank = standing.getOrDefault("ranking", 0);
        int totalScore = standing.getOrDefault("score", 0);

        return new TeamStatistic(formal, sprint, drivers, rank, totalScore);
    }

    @GetMapping("/prix/{prixId}/race")
    public List<RaceResultItem> getRaceResult(@PathVariable int prixId,
            @RequestParam(defaultValue = "false") boolean isSprint) {
        return raceDao.getRaceResults(prixId, isSprint);
    }

    @GetMapping("/prix/{prixId}/qualifying")
    public List<QualifyingResultItem> getQualifyingResult(@PathVariable int prixId,
            @RequestParam(defaultValue = "false") boolean isSprint) {
        return raceDao.getQualifyingResults(prixId, isSprint);
    }

    @GetMapping("/drivers/{id}/results")
    public List<DriverHistoryItem> getDriverHistory(@PathVariable int id, @RequestParam int season) {
        return driverDao.getDriverHistory(id, season);
    }

    @GetMapping("/teams/{id}/results")
    public List<TeamHistoryItem> getTeamHistory(@PathVariable int id, @RequestParam int season) {
        return teamDao.getTeamHistory(id, season);
    }
}