-- Users table
CREATE TABLE users (
  id TEXT PRIMARY KEY,
  username TEXT UNIQUE NOT NULL,
  password_hash TEXT NOT NULL,
  role TEXT NOT NULL,
  created_at TEXT NOT NULL,
  updated_at TEXT NOT NULL
);

-- User profiles table
CREATE TABLE user_profiles (
  user_id TEXT PRIMARY KEY,
  avatar_path TEXT,
  bio TEXT,
  preferences_json TEXT NOT NULL DEFAULT '{}',
  created_at TEXT NOT NULL,
  updated_at TEXT NOT NULL,
  FOREIGN KEY (user_id) REFERENCES users (id)
);

-- User library access table
CREATE TABLE user_libraries (
  user_id TEXT NOT NULL,
  root_path TEXT NOT NULL,
  permission TEXT NOT NULL,
  granted_at TEXT NOT NULL,
  PRIMARY KEY (user_id, root_path),
  FOREIGN KEY (user_id) REFERENCES users (id)
);

-- Indexes for efficient lookups
CREATE INDEX idx_users_username ON users (username);

CREATE INDEX idx_user_libraries_user_id ON user_libraries (user_id);

CREATE INDEX idx_user_libraries_root_path ON user_libraries (root_path);
