-- admins
CREATE TABLE IF NOT EXISTS admin (
     id INT AUTO_INCREMENT PRIMARY KEY,
     username VARCHAR(255) NOT NULL,
     password VARCHAR(255) NOT NULL,
     permissions INT NOT NULL
);

-- user
CREATE TABLE IF NOT EXISTS user (
    id INT AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(255) NOT NULL,
    lastname VARCHAR(255) NOT NULL,
    birthday DATE NOT NULL,
    telephone VARCHAR(255),
    username VARCHAR(255) NOT NULL,
    uuid VARCHAR(36)
);

-- update team draft on username update
CREATE TRIGGER IF NOT EXISTS user_username_update AFTER UPDATE ON user
    FOR EACH ROW
    BEGIN
        IF OLD.username <> NEW.username THEN
            UPDATE team_draft SET member_one_name = NEW.username WHERE member_one_name = OLD.username;
            UPDATE team_draft SET member_two_name = NEW.username WHERE member_two_name = OLD.username;
        END IF;
    END;
DELIMITER ;

-- blocked user
CREATE TABLE IF NOT EXISTS blocked_user (
    id INT AUTO_INCREMENT PRIMARY KEY,
    uuid VARCHAR(255) UNIQUE NOT NULL,
    comment TINYTEXT
);

-- team
CREATE TABLE IF NOT EXISTS team (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) UNIQUE NOT NULL,
    color CHAR(7) NOT NULL,
    last_joined TIMESTAMP
);

-- team member
CREATE TABLE IF NOT EXISTS team_member (
   team_id INT NOT NULL,
   user_id INT NOT NULL,
   FOREIGN KEY (team_id) REFERENCES team(id) ON DELETE CASCADE,
   FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE
);

-- team draft
CREATE TABLE IF NOT EXISTS team_draft (
    member_one_name VARCHAR(255) NOT NULL,
    member_two_name VARCHAR(255) NOT NULL,
    team_id INT NOT NULL,
    FOREIGN KEY (team_id) REFERENCES team(id) ON DELETE CASCADE
);

-- death
CREATE TABLE IF NOT EXISTS death (
    message VARCHAR(1024) NOT NULL,
    dead_user_id INT NOT NULL,
    killer_user_id INT,
    FOREIGN KEY (dead_user_id) REFERENCES user(id) ON DELETE CASCADE,
    FOREIGN KEY (killer_user_id) REFERENCES user(id) ON DELETE CASCADE
);

-- report
CREATE TABLE IF NOT EXISTS report (
    id INT AUTO_INCREMENT PRIMARY KEY,
    reason VARCHAR(255) NOT NULL,
    body TEXT,
    url_hash VARCHAR(255) NOT NULL UNIQUE,
    created_at TIMESTAMP,
    reporter_team_id INT,
    reported_team_id INT,
    FOREIGN KEY (reporter_team_id) REFERENCES team(id) ON DELETE CASCADE,
    FOREIGN KEY (reported_team_id) REFERENCES team(id) ON DELETE CASCADE
);

-- report status
CREATE TABLE IF NOT EXISTS report_status (
    status ENUM('open', 'closed'),
    notice TEXT,
    statement TEXT,
    report_id INT NOT NULL UNIQUE,
    reviewer_id INT,
    FOREIGN KEY (report_id) REFERENCES report(id) ON DELETE CASCADE,
    FOREIGN KEY (reviewer_id) REFERENCES admin(id) ON DELETE CASCADE
);

-- strike reason
CREATE TABLE IF NOT EXISTS strike_reason (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    weight TINYINT NOT NULL
);

-- strike
CREATE TABLE IF NOT EXISTS strike (
    at TIMESTAMP NOT NULL,
    report_id INT NOT NULL UNIQUE,
    strike_reason_id INT NOT NULL,
    FOREIGN KEY (report_id) REFERENCES report(id) ON DELETE CASCADE,
    FOREIGN KEY (strike_reason_id) REFERENCES strike_reason(id) ON DELETE CASCADE
);

-- feedback
CREATE TABLE IF NOT EXISTS feedback (
    id INT AUTO_INCREMENT PRIMARY KEY,
    event VARCHAR(255) NOT NULL,
    title VARCHAR(255),
    content TEXT,
    url_hash VARCHAR(255) NOT NULL UNIQUE,
    last_changed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    user_id INT,
    FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE
);

-- settings
CREATE TABLE IF NOT EXISTS settings (
    name VARCHAR(255) NOT NULL,
    value TEXT NOT NULL
);
