-- 车手表
CREATE TABLE IF NOT EXISTS driver(
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    country VARCHAR(100),
    birthday DATE
);

-- 车队表
CREATE TABLE IF NOT EXISTS team(
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    country VARCHAR(100),
    engine_supplier VARCHAR(100),
    setup_time YEAR
);

-- 合同表
CREATE TABLE IF NOT EXISTS contract(
    id INT AUTO_INCREMENT PRIMARY KEY,
    driver_id INT,
    team_id INT,
    season YEAR,
    car_num INT,
    is_reserve BOOLEAN,
    FOREIGN KEY(driver_id) REFERENCES driver(id),
    FOREIGN KEY(team_id) REFERENCES team(id)
);

-- 赛道表
CREATE TABLE IF NOT EXISTS circuit(
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    location VARCHAR(100),
    country VARCHAR(100),
    drs_zones INT
);

-- 赛事表
CREATE TABLE IF NOT EXISTS prix(
    id INT AUTO_INCREMENT PRIMARY KEY,
    season YEAR,
    round INT,
    name VARCHAR(100),
    circuit_id INT,
    have_sprint BOOLEAN,
    FOREIGN KEY(circuit_id) REFERENCES circuit(id)
);

-- 排位赛结果表
CREATE TABLE IF NOT EXISTS qualifying_result(
    prix_id INT,
    driver_id INT,
    team_id INT,
    fastest_time VARCHAR(20),
    position INT,
    is_sprint BOOLEAN,
    section INT,
    FOREIGN KEY(prix_id) REFERENCES prix(id),
    FOREIGN KEY(driver_id) REFERENCES driver(id),
    FOREIGN KEY(team_id) REFERENCES team(id)
);

-- 正赛结果表
CREATE TABLE IF NOT EXISTS race_result(
    prix_id INT,
    driver_id INT,
    team_id INT,
    start_position INT,
    end_position INT,
    finish_time VARCHAR(20),
    fastest_time VARCHAR(20),
    score INT,
    is_sprint BOOLEAN,
    FOREIGN KEY(prix_id) REFERENCES prix(id),
    FOREIGN KEY(driver_id) REFERENCES driver(id),
    FOREIGN KEY(team_id) REFERENCES team(id)
);

-- 用户表
CREATE TABLE IF NOT EXISTS user(
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(100) UNIQUE,
    password VARCHAR(100),
    email VARCHAR(100),
    country VARCHAR(100),
    avatar VARCHAR(500)
);

-- 评论表
CREATE TABLE IF NOT EXISTS comment(
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    response_id INT,
    root_id INT,
    content VARCHAR(500),
    FOREIGN KEY(user_id) REFERENCES user(id),
    FOREIGN KEY(response_id) REFERENCES comment(id) ON DELETE CASCADE,
    FOREIGN KEY(root_id) REFERENCES comment(id) ON DELETE CASCADE
);

-- 赛季车手信息视图
CREATE OR REPLACE VIEW season_driver(
    id, name, team, country, birthday, car_num, season
) AS
SELECT D.id,
       D.name,
       T.name,
       D.country,
       D.birthday,
       C.car_num,
       C.season
FROM contract C
JOIN team T ON T.id = C.team_id
JOIN driver D ON C.driver_id = D.id;

-- 赛季车队积分榜视图
CREATE OR REPLACE VIEW season_team_standings AS
SELECT 
    p.season,
    rr.team_id,
    SUM(rr.score) AS total_score,
    RANK() OVER (PARTITION BY p.season ORDER BY SUM(rr.score) DESC) AS ranking
FROM race_result rr
JOIN prix p ON rr.prix_id = p.id
GROUP BY p.season, rr.team_id;

CREATE OR REPLACE VIEW prix_result AS
SELECT
    p.season,
    p.id AS prix_id,
    rr.driver_id,
    rr.team_id,
    p.name AS prix_name,
    p.round,
    sd.name AS driver_name,
    sd.car_num,
    sd.team AS team_name,
    rr.end_position AS pos,
    rr.finish_time,
    rr.score,
    rr.is_sprint
FROM
    race_result rr
JOIN
    prix p ON rr.prix_id = p.id
JOIN
    season_driver sd ON rr.driver_id = sd.id AND sd.season = p.season;