CREATE TABLE IF NOT EXISTS "user" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"name" text NOT NULL UNIQUE,
	"pw" text, 
	"deleted" boolean NOT NULL DEFAULT FALSE,
	"last_access" DATETIME,
        "user_token" TEXT NOT NULL DEFAULT (lower(hex(randomblob(16))))
);

CREATE TABLE IF NOT EXISTS "role" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"name" text NOT NULL UNIQUE,
	"cluster" text
);

CREATE TABLE IF NOT EXISTS "user_role" (
	"user_id" INTEGER NOT NULL REFERENCES user(id),
	"role_id" INTEGER NOT NULL REFERENCES role(id),
	CONSTRAINT unq UNIQUE (user_id, role_id) 
);

CREATE TABLE IF NOT EXISTS "trip_type" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"name" text NOT NULL UNIQUE,
	"desc" text NOT NULL,
	"question" text NOT NULL,
	"icon" text NOT NULL
);

CREATE TABLE IF NOT EXISTS "trip_details" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"planned_starting_time" text NOT NULL,
	"max_people" INTEGER NOT NULL,
	"day" TEXT NOT NULL,
	"allow_guests" boolean NOT NULL default false,
	"notes" TEXT,
	"always_show" boolean NOT NULL default false,
	"is_locked" boolean NOT NULL default false,
	"trip_type_id" INTEGER REFERENCES trip_type(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS "planned_event" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"name" text NOT NULL,
	"planned_amount_cox" INTEGER unsigned NOT NULL,
	"trip_details_id" INTEGER NOT NULL REFERENCES TRIP_details(id) ON DELETE CASCADE,
	"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS "trip" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"cox_id" INTEGER NOT NULL REFERENCES user(id),
	"trip_details_id" INTEGER REFERENCES trip_details(id) ON DELETE CASCADE,
	"planned_event_id" INTEGER REFERENCES planned_event(id) ON DELETE CASCADE,
	"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP,
	CONSTRAINT unq UNIQUE (cox_id, planned_event_id) -- allow cox to participate only once for each planned event
);

CREATE TABLE IF NOT EXISTS "user_trip" (
	"user_id" INTEGER REFERENCES user(id),
	"user_note" text, -- only shown if user_id = none
	"trip_details_id" INTEGER NOT NULL REFERENCES trip_details(id),
	"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS "log" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"msg" text NOT NULL,
	"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS "rower" (
	"logbook_id" INTEGER NOT NULL REFERENCES logbook(id) ON DELETE CASCADE,
	"rower_id" INTEGER NOT NULL REFERENCES user(id),
	CONSTRAINT unq UNIQUE (logbook_id, rower_id)
);

CREATE TABLE IF NOT EXISTS "notification" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"user_id" INTEGER NOT NULL REFERENCES user(id),
	"message" TEXT NOT NULL,
	"read_at" DATETIME,
	"created_at" DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
	"category" TEXT NOT NULL,
	"action_after_reading" TEXT,
	"link" TEXT
);

CREATE TABLE IF NOT EXISTS "waterlevel" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"day" DATE NOT NULL,
	"time" TEXT NOT NULL,
	"max" INTEGER NOT NULL,
	"min" INTEGER NOT NULL,
	"mittel" INTEGER NOT NULL,
	"tumax" INTEGER NOT NULL,
	"tumin" INTEGER NOT NULL,
	"tumittel" INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS "weather" (
	"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
	"day" DATE NOT NULL,
	"max_temp" FLOAT NOT NULL,
	"wind_gust" FLOAT NOT NULL,
	"rain_mm" FLOAT NOT NULL
);
