-- Create the sessions table.
-- session_hash is the PRIMARY KEY (SHA-256 hash of the raw session token).
-- The raw token is never stored; only the hash.

CREATE TABLE sessions (
    session_hash   TEXT PRIMARY KEY,
    user_id        UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    created_at     TIMESTAMPTZ NOT NULL DEFAULT now(),
    expires_at     TIMESTAMPTZ NOT NULL,
    last_active_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    ip_address     TEXT,
    user_agent     TEXT
);

CREATE INDEX idx_sessions_user_id ON sessions(user_id);
CREATE INDEX idx_sessions_expires_at ON sessions(expires_at);
