-- Persist recently opened cases per user for bottom bar quick access

CREATE TABLE IF NOT EXISTS sag_recent_cases (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
    sag_id INTEGER NOT NULL REFERENCES sag_sager(id) ON DELETE CASCADE,
    opened_at TIMESTAMP NOT NULL DEFAULT NOW(),
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    CONSTRAINT uq_sag_recent_cases_user_sag UNIQUE (user_id, sag_id)
);

CREATE INDEX IF NOT EXISTS idx_sag_recent_cases_user_opened
    ON sag_recent_cases (user_id, opened_at DESC, id DESC);
