-- Reinitialize all users with current server props
-- Use: psql -d chattyness -f reinitialize_all_users.sql

DO $$
DECLARE
    v_user RECORD;
    v_count INT := 0;
BEGIN
    FOR v_user IN SELECT id, username FROM auth.users
    LOOP
        -- Clear existing data
        DELETE FROM auth.active_avatars WHERE user_id = v_user.id;
        DELETE FROM auth.avatars WHERE user_id = v_user.id;
        DELETE FROM auth.inventory WHERE user_id = v_user.id;

        -- Reinitialize with current server props
        PERFORM auth.initialize_new_user(v_user.id);

        v_count := v_count + 1;
        RAISE NOTICE 'Reinitialized user: % (%)', v_user.username, v_user.id;
    END LOOP;

    RAISE NOTICE 'Total users reinitialized: %', v_count;
END;
$$;
