-- Per-user default for the type selected on the new-case page.
CREATE TABLE IF NOT EXISTS user_sag_create_preferences (
    user_id INTEGER PRIMARY KEY REFERENCES users(user_id) ON DELETE CASCADE,
    default_case_type VARCHAR(50) NOT NULL DEFAULT 'ticket',
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_user_sag_create_preferences_updated_at
    ON user_sag_create_preferences(updated_at DESC);

-- Make pipeline available in installations that already have the configured list.
UPDATE settings
SET value = ((value::jsonb || '["pipeline"]'::jsonb)::text)
WHERE key = 'case_types'
  AND NOT (value::jsonb ? 'pipeline');
