PRAGMA foreign_keys = ON;

PRAGMA writable_schema = 1;
DELETE FROM sqlite_master WHERE type IN ("table", "index", "trigger");
PRAGMA writable_schema = 0;
VACUUM;

CREATE TABLE "driverStandings" (
"driverStandingsId" INT,
"points" FLOAT,
PRIMARY KEY ("driverStandingsId")
);

CREATE TABLE "lapTimes" (
"raceId" INT,
"driverId" INT,
"lap" INT,
"time" TEXT,
"milliseconds" INT,
PRIMARY KEY ("raceId", "driverId", "lap"),
FOREIGN KEY ("raceId") REFERENCES "races"("raceId"),
FOREIGN KEY ("driverId") REFERENCES "drivers"("driverId")
);

CREATE TABLE "pitStops" (
"raceId" INT,
"driverId" INT,
"stop" INT,
"lap" INT,
"duration" TEXT,
"milliseconds" INT,
PRIMARY KEY ("raceId", "driverId", "stop"),
FOREIGN KEY ("raceId") REFERENCES "races"("raceId"),
FOREIGN KEY ("driverId") REFERENCES "drivers"("driverId")
);

CREATE TABLE "circuits" (
"circuitId" INT UNIQUE,
"name" TEXT,
"location" TEXT,
"country" TEXT,
PRIMARY KEY ("circuitId")
);

CREATE TABLE "drivers" (
"driverId" INT UNIQUE,
"forename" TEXT,
"surname" TEXT,
PRIMARY KEY ("driverId")
);

CREATE TABLE "qualifying" (
"qualifyId" INT,
"position" INT,
PRIMARY KEY ("qualifyId")
);

CREATE TABLE "races" (
"raceId" INT UNIQUE,
"year" INT,
"circuitId" INT,
"name" TEXT,
"date" DATE,
PRIMARY KEY ("raceId"),
FOREIGN KEY ("circuitId") REFERENCES "circuits"("circuitId")
);

CREATE TABLE "results" (
"resultId" INT,
"raceId" INT,
"driverId" INT,
"position" INT,
"milliseconds" INT,
"fastestLap" INT,
"fastestLapTime" TEXT,
PRIMARY KEY ("resultId"),
FOREIGN KEY ("raceId") REFERENCES "races"("raceId"),
FOREIGN KEY ("driverId") REFERENCES "drivers"("driverId")
);

CREATE TABLE "status" (
"statusId" INT,
"status" TEXT,
PRIMARY KEY ("statusId")
);



INSERT INTO "circuits" VALUES ("1", "Red Bull Ring", "Spielberg", "Austria");
INSERT INTO "circuits" VALUES ("2", "Silverstone Circuit", "Silverstone", "United Kingdom");
INSERT INTO "circuits" VALUES ("3", "Monza Circuit", "Monza", "Italy");
INSERT INTO "races" VALUES ("101", "2023", "1", "Austrian Grand Prix", "2023-07-09");
INSERT INTO "races" VALUES ("102", "2023", "2", "British Grand Prix", "2023-07-16");
INSERT INTO "races" VALUES ("103", "2023", "3", "Italian Grand Prix", "2023-09-03");
INSERT INTO "drivers" VALUES ("201", "Max", "Verstappen");
INSERT INTO "drivers" VALUES ("202", "Lewis", "Hamilton");
INSERT INTO "drivers" VALUES ("203", "Charles", "Leclerc");
INSERT INTO "drivers" VALUES ("204", "Lando", "Norris");
INSERT INTO "lapTimes" VALUES ("101", "201", "1", "1:05.432", "65432");
INSERT INTO "lapTimes" VALUES ("101", "201", "2", "1:04.987", "64987");
INSERT INTO "lapTimes" VALUES ("101", "201", "3", "1:05.123", "65123");
INSERT INTO "lapTimes" VALUES ("101", "202", "1", "1:06.000", "66000");
INSERT INTO "lapTimes" VALUES ("101", "202", "2", "1:05.500", "65500");
INSERT INTO "lapTimes" VALUES ("101", "203", "1", "1:05.800", "65800");
INSERT INTO "lapTimes" VALUES ("101", "203", "2", "1:05.600", "65600");
INSERT INTO "lapTimes" VALUES ("101", "204", "1", "1:06.200", "66200");
INSERT INTO "pitStops" VALUES ("101", "201", "1", "10", "22.345", "22345");
INSERT INTO "pitStops" VALUES ("101", "201", "2", "25", "21.987", "21987");
INSERT INTO "pitStops" VALUES ("101", "202", "1", "12", "23.500", "23500");
INSERT INTO "pitStops" VALUES ("101", "203", "1", "15", "22.000", "22000");
INSERT INTO "pitStops" VALUES ("101", "204", "1", "14", "24.100", "24100");
INSERT INTO "results" VALUES ("301", "101", "201", "1", "5400000", "2", "1:04.987");
INSERT INTO "results" VALUES ("302", "101", "202", "2", "5420000", "5", "1:05.500");
INSERT INTO "results" VALUES ("303", "101", "203", "3", "5450000", "7", "1:05.600");
INSERT INTO "results" VALUES ("304", "101", "204", "4", "5500000", "10", "1:06.200");
INSERT INTO "driverStandings" VALUES ("201", "150.5");
INSERT INTO "driverStandings" VALUES ("202", "120.0");
INSERT INTO "driverStandings" VALUES ("203", "110.0");
INSERT INTO "driverStandings" VALUES ("204", "85.0");
INSERT INTO "qualifying" VALUES ("401", "1");
INSERT INTO "qualifying" VALUES ("402", "2");
INSERT INTO "qualifying" VALUES ("403", "3");
INSERT INTO "qualifying" VALUES ("404", "4");
INSERT INTO "status" VALUES ("501", "Finished");
INSERT INTO "status" VALUES ("502", "Accident");
INSERT INTO "status" VALUES ("503", "Mechanical");