package f1.db;

import java.sql.SQLException;
import java.util.ArrayList;

import f1.entity.QualifyingResultItem;
import f1.entity.RaceResultItem;
import f1.entity.SeasonScheduleItem;

public class RaceDao extends BaseDao {

    public RaceDao(Database db) {
        super(db);
    }

    public ArrayList<RaceResultItem> getRaceResults(int prixId, boolean isSprint) {
        ArrayList<RaceResultItem> results = new ArrayList<>();
        String sql = "SELECT pos, car_num, driver_name, team_name, finish_time, score FROM prix_result WHERE prix_id = ? AND is_sprint = ? ORDER BY pos";
        try (var con = getConnection(); var stmt = con.prepareStatement(sql)) {
            stmt.setInt(1, prixId);
            stmt.setBoolean(2, isSprint);
            var rs = stmt.executeQuery();
            while (rs.next()) {
                results.add(new RaceResultItem(
                        rs.getInt("pos"),
                        rs.getInt("car_num"),
                        rs.getString("driver_name"),
                        rs.getString("team_name"),
                        rs.getString("finish_time"),
                        rs.getInt("score")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return results;
    }

    public ArrayList<QualifyingResultItem> getQualifyingResults(int prixId, boolean isSprint) {
        ArrayList<QualifyingResultItem> results = new ArrayList<>();
        String sql = """
                SELECT
                    qr.position,
                    sd.car_num,
                    sd.name,
                    sd.team,
                    MAX(CASE WHEN section = 1 THEN fastest_time END) AS q1_time,
                    MAX(CASE WHEN section = 2 THEN fastest_time END) AS q2_time,
                    MAX(CASE WHEN section = 3 THEN fastest_time END) AS q3_time
                FROM
                    qualifying_result qr
                JOIN
                    prix p ON qr.prix_id = p.id
                JOIN
                    season_driver sd ON qr.driver_id = sd.id AND sd.season = p.season
                WHERE
                    qr.prix_id = ?
                    AND qr.is_sprint = ?
                GROUP BY
                    qr.driver_id, qr.position, sd.car_num, sd.name, sd.team
                ORDER BY
                    qr.position
                """;
        try (var con = getConnection(); var stmt = con.prepareStatement(sql)) {
            stmt.setInt(1, prixId);
            stmt.setBoolean(2, isSprint);
            var rs = stmt.executeQuery();
            while (rs.next()) {
                results.add(new QualifyingResultItem(
                        rs.getInt("position"),
                        rs.getInt("car_num"),
                        rs.getString("name"),
                        rs.getString("team"),
                        rs.getString("q1_time"),
                        rs.getString("q2_time"),
                        rs.getString("q3_time")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return results;
    }

    public ArrayList<SeasonScheduleItem> getSeasonSchedule(int season) {
        ArrayList<SeasonScheduleItem> schedule = new ArrayList<>();
        String sql = """
                SELECT
                    p.id,
                    p.round,
                    p.name,
                    p.have_sprint,
                    p.circuit_id,
                    c.country,
                    c.location
                FROM
                    prix p
                JOIN circuit c ON c.id = p.circuit_id
                WHERE
                    p.season = ?
                ORDER BY
                    p.round
                """;
        try (var con = getConnection(); var stmt = con.prepareStatement(sql)) {
            stmt.setInt(1, season);
            var rs = stmt.executeQuery();
            while (rs.next()) {
                schedule.add(new SeasonScheduleItem(
                        rs.getInt("id"),
                        rs.getInt("round"),
                        rs.getString("name"),
                        rs.getBoolean("have_sprint"),
                        rs.getInt("circuit_id"),
                        rs.getString("country"),
                        rs.getString("location")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return schedule;
    }
}
