CREATE TABLE IF NOT EXISTS TASKS (
	id INTEGER PRIMARY KEY,
	text varchar(64) NOT NULL,
	enabled BOOL DEFAULT(true)
);

CREATE TABLE IF NOT EXISTS AVAILABILITIES (
	id INTEGER PRIMARY KEY,
	text varchar(32) NOT NULL,
	color varchar(7) NOT NULL,
	enabled BOOL DEFAULT(true)
);

CREATE TABLE IF NOT EXISTS USERS (
	name varchar(64) PRIMARY KEY,
	password binary(60) NOT NULL,
	admin BOOL NOT NULL DEFAULT(false),
	tokenID varchar(64) NOT NULL,
	CHECK (length(password) = 60),
	CHECK (length(tokenID) = 36)
);

CREATE TABLE IF NOT EXISTS EVENTS (
	id INTEGER PRIMARY KEY,
	date DATETIME NOT NULL,
	description TEXT DEFAULT "" 
);

CREATE TABLE IF NOT EXISTS USER_AVAILABILITIES (
	userName varchar(64) NOT NULL,
	eventID INTEGER NOT NULL,
	availabilityID INTEGER NOT NULL,
	PRIMARY KEY (userName, eventID),
	FOREIGN KEY (userName) REFERENCES USERS(name) ON DELETE CASCADE ON UPDATE CASCADE,
	FOREIGN KEY (eventID) REFERENCES EVENTS(id) ON DELETE CASCADE ON UPDATE CASCADE,
	FOREIGN KEY (availabilityID) REFERENCES AVAILABILITIES(id) ON UPDATE CASCADE
);

CREATE TABLE IF NOT EXISTS USER_ASSIGNMENTS (
	eventID INTEGER NOT NULL,
	taskID INTEGER NOT NULL,
	userName varchar(64),
	PRIMARY KEY (eventID, taskID),
	FOREIGN KEY (eventID) REFERENCES EVENTS(id) ON DELETE CASCADE,
	FOREIGN KEY (userName) REFERENCES USERS(name) ON DELETE CASCADE ON UPDATE CASCADE,
	FOREIGN KEY (taskID) REFERENCES TASKS(id) ON UPDATE CASCADE
);
