-- SQLite schema for Kabuki CMS / pics
--
-- Usage:
--   sqlite3 data/pics.sqlite < schema.sqlite.sql
--
-- Config (add to config.php):
--   define('DB_DRIVER', 'sqlite');
--   define('DB_FILE', __DIR__ . '/data/pics.sqlite');

CREATE TABLE IF NOT EXISTS users (
	id_user INTEGER PRIMARY KEY,
	first_name TEXT NOT NULL,
	surname TEXT NOT NULL,
	slug TEXT NOT NULL UNIQUE,
	emailaddress TEXT NOT NULL UNIQUE,
	password_hash TEXT NOT NULL,
	creation_time INTEGER NOT NULL,
	last_action_time INTEGER,
	ip_address TEXT,
	is_admin INTEGER NOT NULL DEFAULT 0,
	reset_key TEXT,
	reset_blocked_until INTEGER
);

CREATE TABLE IF NOT EXISTS assets (
	id_asset INTEGER PRIMARY KEY,
	id_user_uploaded INTEGER NOT NULL,
	subdir TEXT NOT NULL,
	filename TEXT NOT NULL,
	title TEXT,
	slug TEXT UNIQUE,
	mimetype TEXT,
	image_width INTEGER,
	image_height INTEGER,
	date_captured TEXT,
	priority INTEGER DEFAULT 0
);

CREATE TABLE IF NOT EXISTS assets_meta (
	id_asset INTEGER NOT NULL,
	variable TEXT NOT NULL,
	value TEXT,
	PRIMARY KEY (id_asset, variable)
);

CREATE TABLE IF NOT EXISTS assets_thumbs (
	id_asset INTEGER NOT NULL,
	width INTEGER NOT NULL,
	height INTEGER NOT NULL,
	mode TEXT,
	filename TEXT,
	PRIMARY KEY (id_asset, width, height, mode)
);

CREATE TABLE IF NOT EXISTS tags (
	id_tag INTEGER PRIMARY KEY,
	id_parent INTEGER,
	id_asset_thumb INTEGER,
	id_user_owner INTEGER,
	tag TEXT NOT NULL,
	slug TEXT NOT NULL UNIQUE,
	description TEXT,
	kind TEXT NOT NULL DEFAULT 'Tag',
	count INTEGER DEFAULT 0
);

CREATE TABLE IF NOT EXISTS assets_tags (
	id_asset INTEGER NOT NULL,
	id_tag INTEGER NOT NULL,
	PRIMARY KEY (id_asset, id_tag)
);

CREATE TABLE IF NOT EXISTS posts_assets (
	id_post INTEGER NOT NULL,
	id_asset INTEGER NOT NULL,
	PRIMARY KEY (id_post, id_asset)
);

CREATE TABLE IF NOT EXISTS posts_tags (
	id_post INTEGER NOT NULL,
	id_tag INTEGER NOT NULL,
	PRIMARY KEY (id_post, id_tag)
);

CREATE TABLE IF NOT EXISTS settings (
	id_user INTEGER NOT NULL,
	variable TEXT NOT NULL,
	value TEXT,
	time_set TEXT DEFAULT CURRENT_TIMESTAMP,
	PRIMARY KEY (id_user, variable)
);

CREATE TABLE IF NOT EXISTS log_errors (
	id_entry INTEGER PRIMARY KEY,
	id_user INTEGER,
	message TEXT,
	debug_info TEXT,
	file TEXT,
	line INTEGER,
	request_uri TEXT,
	time TEXT DEFAULT CURRENT_TIMESTAMP,
	ip_address TEXT
);
