DROP TABLE IF EXISTS notes;
DROP TABLE IF EXISTS note_relationships;
DROP TABLE IF EXISTS pages;
DROP TABLE IF EXISTS favorites;
DROP TABLE IF EXISTS page_relationships;

CREATE TABLE notes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    uuid TEXT NOT NULL UNIQUE,
    content TEXT NULL,
    notetype TEXT
);

CREATE INDEX notes_uuids ON notes (uuid);

CREATE TABLE pages (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title text NOT NULL UNIQUE,
    slug text NOT NULL UNIQUE,
    note_id INTEGER,
    FOREIGN KEY (note_id) REFERENCES notes (id) ON DELETE NO ACTION ON UPDATE NO ACTION
);

CREATE INDEX pages_slugs ON pages (slug);

CREATE TABLE favorites (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    position INTEGER NOT NULL
);

CREATE TABLE note_relationships (
    note_id INTEGER NOT NULL,
    parent_id INTEGER NOT NULL,
    position INTEGER NOT NULL,
    nature TEXT NOT NULL,
    FOREIGN KEY (note_id) REFERENCES notes (id) ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY (parent_id) REFERENCES notes (id) ON DELETE NO ACTION ON UPDATE NO ACTION
);

CREATE TABLE page_relationships (
    note_id INTEGER NOT NULL,
	page_id INTEGER NOT NULL,
	FOREIGN KEY (note_id) references notes (id) ON DELETE NO ACTION ON UPDATE NO ACTION,
	FOREIGN KEY (page_id) references pages (id) ON DELETE NO ACTION ON UPDATE NO ACTION
);
