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 "drivers" VALUES ("101", "Max", "Verstappen");
INSERT INTO "drivers" VALUES ("102", "Lewis", "Hamilton");
INSERT INTO "drivers" VALUES ("103", "Charles", "Leclerc");
INSERT INTO "races" VALUES ("201", "2023", "1", "Austrian Grand Prix", "2023-07-09");
INSERT INTO "races" VALUES ("202", "2023", "2", "British Grand Prix", "2023-07-16");
INSERT INTO "lapTimes" VALUES ("201", "101", "1", "1:05.123", "65123");
INSERT INTO "lapTimes" VALUES ("201", "101", "2", "1:04.789", "64789");
INSERT INTO "lapTimes" VALUES ("201", "102", "1", "1:06.000", "66000");
INSERT INTO "lapTimes" VALUES ("201", "103", "1", "1:06.500", "66500");
INSERT INTO "pitStops" VALUES ("201", "101", "1", "15", "0:20.500", "20500");
INSERT INTO "pitStops" VALUES ("201", "102", "1", "16", "0:22.000", "22000");
INSERT INTO "pitStops" VALUES ("201", "103", "1", "14", "0:21.000", "21000");
INSERT INTO "results" VALUES ("301", "201", "101", "1", "5400000", "2", "1:04.789");
INSERT INTO "results" VALUES ("302", "201", "102", "2", "5410000", "5", "1:05.500");
INSERT INTO "results" VALUES ("303", "201", "103", "3", "5420000", "7", "1:06.000");
INSERT INTO "driverStandings" VALUES ("401", "150.0");
INSERT INTO "driverStandings" VALUES ("402", "120.0");
INSERT INTO "driverStandings" VALUES ("403", "100.0");
INSERT INTO "qualifying" VALUES ("501", "1");
INSERT INTO "qualifying" VALUES ("502", "2");
INSERT INTO "qualifying" VALUES ("503", "3");
INSERT INTO "status" VALUES ("601", "Finished");
INSERT INTO "status" VALUES ("602", "Retired");