-- Web CAD Backend – SQLite Schema
-- Version 1.0

PRAGMA journal_mode = WAL;
PRAGMA foreign_keys = ON;

-- ─── Users ──────────────────────────────────────────
CREATE TABLE IF NOT EXISTS users (
  id TEXT PRIMARY KEY,
  email TEXT UNIQUE NOT NULL,
  password_hash TEXT NOT NULL,
  name TEXT NOT NULL,
  role TEXT NOT NULL DEFAULT 'planer' CHECK(role IN ('admin','planer','betrachter','gast')),
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

-- ─── Default User (seed) ────────────────────────────
INSERT OR IGNORE INTO users (id, email, password_hash, name, role) VALUES ('user-default', 'default@webcad.local', '', 'Default User', 'admin');

-- ─── Projects ───────────────────────────────────────
CREATE TABLE IF NOT EXISTS projects (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  description TEXT,
  owner_id TEXT NOT NULL,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE CASCADE
);

-- ─── Drawings ───────────────────────────────────────
CREATE TABLE IF NOT EXISTS drawings (
  id TEXT PRIMARY KEY,
  project_id TEXT NOT NULL,
  name TEXT NOT NULL,
  data_json TEXT NOT NULL DEFAULT '{}',
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

-- ─── Layers ─────────────────────────────────────────
CREATE TABLE IF NOT EXISTS layers (
  id TEXT PRIMARY KEY,
  drawing_id TEXT NOT NULL,
  name TEXT NOT NULL,
  visible INTEGER NOT NULL DEFAULT 1,
  locked INTEGER NOT NULL DEFAULT 0,
  color TEXT NOT NULL DEFAULT '#ffffff',
  line_type TEXT NOT NULL DEFAULT 'solid',
  transparency REAL NOT NULL DEFAULT 0,
  sort_order INTEGER NOT NULL DEFAULT 0,
  parent_id TEXT,
  FOREIGN KEY (drawing_id) REFERENCES drawings(id) ON DELETE CASCADE
);

-- ─── Elements ───────────────────────────────────────
CREATE TABLE IF NOT EXISTS elements (
  id TEXT PRIMARY KEY,
  drawing_id TEXT NOT NULL,
  layer_id TEXT NOT NULL,
  type TEXT NOT NULL,
  x REAL NOT NULL,
  y REAL NOT NULL,
  width REAL NOT NULL,
  height REAL NOT NULL,
  properties_json TEXT NOT NULL DEFAULT '{}',
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY (drawing_id) REFERENCES drawings(id) ON DELETE CASCADE,
  FOREIGN KEY (layer_id) REFERENCES layers(id) ON DELETE CASCADE
);

-- ─── Block Definitions ──────────────────────────────
CREATE TABLE IF NOT EXISTS blocks (
  id TEXT PRIMARY KEY,
  drawing_id TEXT NOT NULL,
  name TEXT NOT NULL,
  description TEXT,
  category TEXT NOT NULL DEFAULT 'Allgemein',
  elements_json TEXT NOT NULL DEFAULT '[]',
  thumbnail TEXT,
  FOREIGN KEY (drawing_id) REFERENCES drawings(id) ON DELETE CASCADE
);

-- ─── Settings ───────────────────────────────────────
CREATE TABLE IF NOT EXISTS settings (
  key TEXT PRIMARY KEY,
  value TEXT NOT NULL,
  updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

-- ─── Indexes ────────────────────────────────────────
CREATE INDEX IF NOT EXISTS idx_drawings_project ON drawings(project_id);
CREATE INDEX IF NOT EXISTS idx_layers_drawing ON layers(drawing_id);
CREATE INDEX IF NOT EXISTS idx_elements_drawing ON elements(drawing_id);
CREATE INDEX IF NOT EXISTS idx_elements_layer ON elements(layer_id);
CREATE INDEX IF NOT EXISTS idx_blocks_drawing ON blocks(drawing_id);
