\set ON_ERROR_STOP on
-- Rio strategy-context companion preflight. Read-only.
-- Run after the mapped rich proposal packet has been applied.

select 'rio_agent' as check_name, id::text, handle, kind
from public.agents
where id = '22222222-2222-2222-2222-222222222222'::uuid;

select 'required_base_claim' as check_name,
       c.id::text,
       c.status,
       left(c.text, 220) as text
from public.claims c
where c.id in (select id from (values
    ('004195cc-ab62-526c-90fb-51cb0f826ec0'::uuid),
    ('67003e10-e1ee-5905-8b51-1b0cb263ec11'::uuid),
    ('e6ce4e36-15d2-53bc-8b74-dda0efc095c1'::uuid)
  ) as v(id))
order by c.id;

select 'back_shared_root' as check_name,
       id::text,
       title,
       left(body, 220) as body
from public.shared_root
where id = '6248818e-fe16-4d3b-8008-ef9752b80632'::uuid;

select 'existing_exact_rio_strategy_title' as check_name,
       id::text,
       agent_id::text,
       title,
       status
from public.strategy_nodes
where agent_id = '22222222-2222-2222-2222-222222222222'::uuid
  and title in (
    'Rio capital deployment layer',
    'Rio internet-finance capital product'
  )
order by title;

-- Must be 0/0 before authorized apply.
select
  (select count(*) from public.strategy_nodes where id in (select id from (values
    ('4f3e3d77-50fa-520a-8c8b-5a4269d94a9b'::uuid),
    ('063528a6-a07c-5c14-abdf-dc12a0b10fcf'::uuid)
  ) as v(id))) as generated_strategy_node_rows,
  (select count(*) from public.strategy_node_anchors where id in (select id from (values
    ('3a0e2228-a74f-5971-b467-fb527ccdfb37'::uuid),
    ('3f7a9f84-eb2e-5d94-9ca4-afc3bad47f96'::uuid),
    ('d18eaa71-d458-53cc-baac-e5306a1779e2'::uuid),
    ('33d9eb08-c9c2-5e2d-a9a7-3d7f1b5f0f0c'::uuid),
    ('3b2a8908-120d-59a5-bbd5-d2711a123aa3'::uuid)
  ) as v(id))) as generated_strategy_anchor_rows;


select 'governance_gate_known_not_written' as check_name,
       id::text,
       name,
       left(criteria, 180) as criteria,
       left(evidence_bar, 180) as evidence_bar
from public.governance_gates
where id in (select id from (values
    ('fa88e31b-6fca-4801-bf72-f5db81950a11'::uuid),
    ('45695897-2a9c-4279-ac25-e59f166dec6a'::uuid)
  ) as v(id))
order by name;
