CREATE TABLE IF NOT EXISTS events (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    chat_id INTEGER NOT NULL,
    event_type TEXT NOT NULL CHECK (event_type IN ('in', 'out')),
    occurred_at INTEGER NOT NULL CHECK (occurred_at > 0),
    note TEXT NOT NULL DEFAULT '',
    created_at INTEGER NOT NULL DEFAULT (unixepoch())
);

CREATE INDEX IF NOT EXISTS idx_events_chat_occurred
ON events(chat_id, occurred_at);

CREATE TABLE IF NOT EXISTS settings (
    chat_id INTEGER NOT NULL,
    key TEXT NOT NULL,
    value TEXT NOT NULL DEFAULT '',
    PRIMARY KEY (chat_id, key)
);

CREATE TABLE IF NOT EXISTS days_off (
    chat_id INTEGER NOT NULL,
    date TEXT NOT NULL CHECK (date = strftime('%Y-%m-%d', date)),
    reason TEXT NOT NULL DEFAULT '',
    created_at INTEGER NOT NULL DEFAULT (unixepoch()),
    PRIMARY KEY (chat_id, date)
);
