CREATE TABLE IF NOT EXISTS persons (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    notes TEXT
);

CREATE TABLE IF NOT EXISTS devices (
    mac TEXT PRIMARY KEY,
    person_id INTEGER REFERENCES persons(id),
    label TEXT,
    added_at REAL DEFAULT (unixepoch())
);

CREATE TABLE IF NOT EXISTS signals (
    id INTEGER PRIMARY KEY,
    mac TEXT NOT NULL,
    signal_type TEXT NOT NULL,
    certainty REAL NOT NULL,
    ts REAL DEFAULT (unixepoch())
);

CREATE TABLE IF NOT EXISTS occupancy_log (
    id INTEGER PRIMARY KEY,
    person_id INTEGER REFERENCES persons(id),
    old_status TEXT,
    new_status TEXT NOT NULL,
    ts REAL DEFAULT (unixepoch())
);

CREATE INDEX IF NOT EXISTS idx_signals_mac ON signals(mac);
CREATE INDEX IF NOT EXISTS idx_signals_ts ON signals(ts);
CREATE INDEX IF NOT EXISTS idx_occupancy_person ON occupancy_log(person_id);
CREATE INDEX IF NOT EXISTS idx_devices_person ON devices(person_id);
