PRAGMA foreign_keys = 1;

CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    emailaddress VARCHAR UNIQUE NOT NULL,
    password VARCHAR NOT NULL,
    isadmin BOOLEAN DEFAULT FALSE);

CREATE TABLE zones (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    zone VARCHAR NOT NULL,
    owner INTEGER NOT NULL,
    UNIQUE(zone),
    FOREIGN KEY(owner) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE );

CREATE TABLE logs (
    id INTEGER PRIMARY KEY,
    user TEXT NOT NULL,
    log TEXT NOT NULL,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);

CREATE TABLE groups (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name VARCHAR UNIQUE NOT NULL,
    desc VARCHAR);

CREATE TABLE groupmembers (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    "group" INTEGER NOT NULL,
    user INTEGER NOT NULL,
    UNIQUE("group",user),
    FOREIGN KEY("group") REFERENCES groups(id) ON DELETE CASCADE,
    FOREIGN KEY(user) REFERENCES users(id) ON DELETE CASCADE);

CREATE TABLE permissions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    zone INTEGER NOT NULL,
    user INTEGER,
    "group" INTEGER,
    permissions INTEGER,
    UNIQUE(zone,user,"group"),
    FOREIGN KEY(zone) REFERENCES zones(id) ON DELETE CASCADE,
    FOREIGN KEY(user) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY("group") REFERENCES groups(id) ON DELETE CASCADE);

CREATE TABLE metadata (
    name VARCHAR PRIMARY KEY,
    value VARCHAR NOT NULL);

INSERT INTO metadata (name, value) VALUES ("version","2");
