-- 0002_sessions.sql
-- Create sessions table for auth tokens

CREATE TABLE sessions (
    token_id TEXT PRIMARY KEY,
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    token_type TEXT NOT NULL,
    auth_method TEXT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    expires_at TIMESTAMPTZ NOT NULL,
    last_used_at TIMESTAMPTZ,
    user_agent TEXT,
    ip_address TEXT,
    is_used BOOLEAN DEFAULT FALSE
);

-- Index for user session lookups (logout all, etc.)
CREATE INDEX sessions_user_id_idx ON sessions (user_id);

-- Index for expiration cleanup
CREATE INDEX sessions_expires_at_idx ON sessions (expires_at);

-- Index for token type filtering
CREATE INDEX sessions_token_type_idx ON sessions (token_type);
