DROP TABLE IF EXISTS notes;
DROP TABLE IF EXISTS note_relationships;
DROP INDEX IF EXISTS note_ids;
DROP TABLE IF EXISTS favorites;

CREATE TABLE notes (
    id TEXT NOT NULL PRIMARY KEY,
    content TEXT NOT NULL,
    kind TEXT NOT NULL,
	creation_date DATETIME NOT NULL,
	updated_date DATETIME NOT NULL,
	lastview_date DATETIME NOT NULL,
	deleted_date DATETIME NULL
);

CREATE INDEX note_ids ON notes (id);

CREATE TABLE favorites (
	id TEXT NOT NULL,
    location INTEGER NOT NULL,
    FOREIGN KEY (id) REFERENCES notes (id) ON DELETE CASCADE
);

CREATE TABLE note_relationships (
    note_id TEXT NOT NULL,
    parent_id TEXT NOT NULL,
    location INTEGER NOT NULL,
    kind TEXT NOT NULL,
	-- If either note disappears, we want all the edges to disappear as well.
    FOREIGN KEY (note_id) REFERENCES notes (id) ON DELETE CASCADE,
    FOREIGN KEY (parent_id) REFERENCES notes (id) ON DELETE CASCADE
);

