PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS users (
    id			TEXT PRIMARY KEY,
    username	TEXT UNIQUE NOT NULL,
    password	BLOB
);

CREATE TABLE IF NOT EXISTS sessions (
	key			BLOB PRIMARY KEY,
	user_id		TEXT NOT NULL REFERENCES users(id) ON UPDATE CASCADE,
	created_at	INT NOT NULL
);


CREATE TABLE IF NOT EXISTS tasks (
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	name TEXT NOT NULL,
	description TEXT,
	completed INT NOT NULL,
	created_by TEXT NOT NULL REFERENCES users(id) ON UPDATE CASCADE,
	assigned_to TEXT NOT NULL REFERENCES users(id) ON UPDATE CASCADE,
	created_at INT NOT NULL,
	deadline INT
);

CREATE TABLE IF NOT EXISTS categories (
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	name TEXT NOT NULL,
	description TEXT,
	created_by TEXT NOT NULL REFERENCES users(id) ON UPDATE CASCADE,
	created_at INT NOT NULL
);

CREATE TABLE IF NOT EXISTS task_categories (
	task_id INT NOT NULL REFERENCES tasks(id) ON UPDATE CASCADE,
	category_id INT NOT NULL REFERENCES category(id) ON UPDATE CASCADE,
	PRIMARY KEY (task_id, category_id)
);
