-- Presence intelligence schema — Phase 1 (WiFi-only)
-- Load with: sqlite3 presence.db < presence_schema.sql

PRAGMA journal_mode=WAL;

CREATE TABLE IF NOT EXISTS persons (
    mac TEXT PRIMARY KEY,
    hostname TEXT,
    vendor TEXT,
    first_seen REAL NOT NULL,
    last_seen REAL NOT NULL,
    state TEXT NOT NULL DEFAULT 'ABSENT',
    certainty REAL NOT NULL DEFAULT 0.0,
    session_count INTEGER NOT NULL DEFAULT 1,
    total_minutes REAL NOT NULL DEFAULT 0.0
);

CREATE TABLE IF NOT EXISTS signals (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    mac TEXT NOT NULL,
    signal_type TEXT NOT NULL,  -- mdns | arp
    certainty REAL NOT NULL,
    observed_at REAL NOT NULL
);

CREATE TABLE IF NOT EXISTS events (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    mac TEXT NOT NULL,
    hostname TEXT,
    event_type TEXT NOT NULL,  -- arrived | departed | updated
    certainty REAL NOT NULL,
    timestamp REAL NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_signals_mac ON signals(mac);
CREATE INDEX IF NOT EXISTS idx_signals_mac_time ON signals(mac, observed_at);
CREATE INDEX IF NOT EXISTS idx_events_mac ON events(mac);
CREATE INDEX IF NOT EXISTS idx_events_timestamp ON events(timestamp);
