-- Optional operator-only read-only checks after applying migration 003.
-- Does not impersonate a customer, change a request, call external services,
-- or execute the separate pending admin login/MFA migration.
begin transaction read only;
select
 exists(select 1 from information_schema.columns where table_schema='marketplace_private' and table_name='tokenization_requests' and column_name='wizard_step') as has_saved_stage,
 to_regprocedure('marketplace_private.tokenization_progress(uuid)') is not null as has_progress,
 not has_function_privilege('authenticated','marketplace_private.tokenization_progress(uuid)','execute') as progress_is_private,
 position('customer_access_assert' in pg_get_functiondef('public.marketplace_tokenization_customer(text,jsonb)'::regprocedure))>0 as customer_access_guard_preserved,
 position('wizard_step' in pg_get_functiondef('marketplace_private.access_base_marketplace_tokenization_customer(text,jsonb)'::regprocedure))>0 as save_accepts_stage,
 position('where p_admin or' in pg_get_functiondef('marketplace_private.access_base_marketplace_tokenization_state(boolean)'::regprocedure))>0 as admin_drafts_visible;

select status,count(*) as saved_requests,
 count(*) filter(where wizard_step is not null) as with_explicit_stage
from marketplace_private.tokenization_requests group by status order by status;

select progress->>'current_step' as current_step,count(*) as unfinished_requests
from (select marketplace_private.tokenization_progress(id) progress
 from marketplace_private.tokenization_requests where status in('draft','changes_requested')) q
group by progress->>'current_step' order by current_step;
rollback;
