-- database/migrations/001_initial.sql

CREATE TABLE IF NOT EXISTS topics (
    id TEXT PRIMARY KEY,
    topic TEXT UNIQUE NOT NULL,
    approved INTEGER NOT NULL DEFAULT 0,
    last_seen TEXT,
    alarm_status TEXT NOT NULL DEFAULT 'unknown',
    alarm_threshold_hours REAL NOT NULL DEFAULT 0.0,
    created_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS sensors (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    description TEXT NOT NULL DEFAULT '',
    location TEXT NOT NULL DEFAULT '',
    sensor_type TEXT NOT NULL DEFAULT '',
    active INTEGER NOT NULL DEFAULT 1,
    topic_id TEXT REFERENCES topics(id),
    display_config TEXT NOT NULL DEFAULT '{}',
    created_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username TEXT UNIQUE NOT NULL,
    password_hash TEXT NOT NULL,
    role TEXT NOT NULL DEFAULT 'user',
    totp_secret TEXT,
    totp_enabled INTEGER NOT NULL DEFAULT 0,
    created_at TEXT NOT NULL
);
