DROP TABLE IF EXISTS draws;
DROP TABLE IF EXISTS people;
DROP TABLE IF EXISTS project;
DROP TABLE IF EXISTS user;

CREATE TABLE user (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username TEXT UNIQUE NOT NULL,
    password TEXT NOT NULL
);

CREATE TABLE project (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    description TEXT,
    image_url TEXT,
    total_days INTEGER NOT NULL DEFAULT 24
);

CREATE TABLE people (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    project_id INTEGER NOT NULL,
    name TEXT NOT NULL,
    draws INTEGER NOT NULL DEFAULT 0,
    max_draws INTEGER NOT NULL DEFAULT 0,
    FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE
);

CREATE TABLE draws (
    day INTEGER NOT NULL,
    person_id INTEGER NOT NULL,
    project_id INTEGER NOT NULL,
    draw_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (day, project_id),
    FOREIGN KEY (person_id) REFERENCES people(id),
    FOREIGN KEY (project_id) REFERENCES project(id) ON DELETE CASCADE
);

