CREATE TABLE film (
	rowid      INTEGER PRIMARY KEY NOT NULL,
	title      TEXT NOT NULL,
	ogtitle    TEXT,
	year       INT NOT NULL,
	poster     INTEGER,
	lastfetch  INTEGER,
	CONSTRAINT fk_film_poster FOREIGN KEY (poster) REFERENCES poster(rowid) ON DELETE SET NULL
) STRICT;

CREATE TABLE poster (
	rowid      INTEGER PRIMARY KEY NOT NULL,
	film       INTEGER NOT NULL,
	path       TEXT NOT NULL,
	CONSTRAINT fk_poster_film FOREIGN KEY (film) REFERENCES film(rowid) ON DELETE CASCADE
) STRICT;

CREATE TABLE person (
	rowid      INTEGER PRIMARY KEY NOT NULL,
	name       TEXT NOT NULL,
	birth      INT,
	death      INT,
	picture    TEXT,
	lastfetch  INTEGER
) STRICT;

CREATE TABLE crew (
	film       INTEGER NOT NULL,
	person     INTEGER NOT NULL,
	role       TEXT NOT NULL,
	PRIMARY KEY (film, person, role),
	CONSTRAINT fk_crew_film FOREIGN KEY (film) REFERENCES film(rowid) ON DELETE CASCADE,
	CONSTRAINT fk_crew_person FOREIGN KEY (person) REFERENCES person(rowid) ON DELETE CASCADE
) STRICT;

CREATE TABLE user (
	rowid      INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
	name       TEXT UNIQUE NOT NULL,
	pass       TEXT NOT NULL,
	salt       TEXT NOT NULL,
	perm       INT NOT NULL DEFAULT 0
) STRICT;

CREATE TABLE rating (
	user       INTEGER NOT NULL,
	film       INTEGER NOT NULL,
	value      INTEGER NOT NULL,
	time       TEXT NOT NULL,
	PRIMARY KEY (user, film),
	CONSTRAINT fk_rating_user FOREIGN KEY (user) REFERENCES user(rowid) ON DELETE CASCADE,
	CONSTRAINT fk_rating_film FOREIGN KEY (film) REFERENCES film(rowid) ON DELETE CASCADE
) STRICT;

INSERT INTO user
	(name, perm, pass, salt)
VALUES
	('admin', 1, 'c487293be46cee0c75db63f11cf20c756efdc45c7fb024b517bdfb837ab68c33', 'uwubebou'); /* uwu */
