\set ON_ERROR_STOP on
begin;

do $$
declare
  inserted_nodes int;
  inserted_anchors int;
begin
  if not exists (
    select 1 from public.agents where id = '22222222-2222-2222-2222-222222222222'::uuid and handle = 'rio'
  ) then
    raise exception 'Rio strategy packet requires rio agent % to exist', '22222222-2222-2222-2222-222222222222';
  end if;

  if (
    select count(*) from public.claims where 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))
  ) <> 3 then
    raise exception 'Rio strategy packet requires mapped base claims A/C/D to exist before strategy context insert';
  end if;

  if not exists (select 1 from public.shared_root where id = '6248818e-fe16-4d3b-8008-ef9752b80632'::uuid) then
    raise exception 'Rio strategy packet requires Back shared root % to exist', '6248818e-fe16-4d3b-8008-ef9752b80632';
  end if;

  if exists (select 1 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))) then
    raise exception 'Rio strategy packet generated strategy node id already exists';
  end if;

  if exists (select 1 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))) then
    raise exception 'Rio strategy packet generated strategy anchor id already exists';
  end if;

  if exists (
    select 1
    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'
      )
  ) then
    raise exception 'Rio strategy packet refuses to create duplicate exact Rio strategy title';
  end if;

  insert into public.strategy_nodes
    (id, agent_id, node_type, title, body, rank, status, horizon, measure, source_ref, metadata)
  values
    ('4f3e3d77-50fa-520a-8c8b-5a4269d94a9b'::uuid, '22222222-2222-2222-2222-222222222222'::uuid, 'policy', 'Rio capital deployment layer', 'Rio treats capital allocation as a civilizational steering layer: resources should back builders through review-gated, market-aware mechanisms that serve Teleo''s Back function.', 1, 'active', 'current strategy context', 'Capital-deployment analysis can name the claim evidence, review boundary, and Back shared root it serves before recommending resource allocation.', 'mapped-rich-proposal-14fa5ecc:rio-capital-deployment-layer', '{"base_packet_required_first": "production-apply-packet-mapped-20260709", "proposal_id": "14fa5ecc-ac7a-41c1-807d-a2e85b936617", "source_fragments": ["capital_allocation_is_civilizational_steering supports rio:capital-deployment-layer", "capital_allocation_is_civilizational_steering relates teleo telos / fund-build paths toward better attractors"]}'::jsonb),
    ('063528a6-a07c-5c14-abdf-dc12a0b10fcf'::uuid, '22222222-2222-2222-2222-222222222222'::uuid, 'objective', 'Rio internet-finance capital product', 'Rio may evaluate internet-finance mechanisms as capital-product candidates, but only as instrumental mechanisms. The product must stay subordinate to Teleo''s capital-allocation telos and cannot become the telos itself.', 2, 'active', 'current strategy context', 'Any internet-finance product thesis states its structural advantages, its mechanism-not-telos boundary, and how it serves the capital deployment layer.', 'mapped-rich-proposal-14fa5ecc:rio-active-strategy-v1-internet-finance-capital-product', '{"base_packet_required_first": "production-apply-packet-mapped-20260709", "proposal_id": "14fa5ecc-ac7a-41c1-807d-a2e85b936617", "source_fragments": ["internet_finance_structural_advantages_capital_allocation relates rio:active strategy v1 / internet-finance capital product", "internet_finance_is_mechanism_not_telos constrains rio:active strategy v1 / internet-finance capital product"]}'::jsonb);

  get diagnostics inserted_nodes = row_count;
  if inserted_nodes <> 2 then
    raise exception 'Rio strategy packet inserted % strategy node row(s), expected 2', inserted_nodes;
  end if;

  insert into public.strategy_node_anchors
    (id, from_node_id, anchor_role, to_node_id, to_shared_root_id, belief_id, claim_id, source_id, weight, note)
  values
    ('3a0e2228-a74f-5971-b467-fb527ccdfb37'::uuid, '4f3e3d77-50fa-520a-8c8b-5a4269d94a9b'::uuid, 'justified_by', null::uuid, null::uuid, null::uuid, '004195cc-ab62-526c-90fb-51cb0f826ec0'::uuid, null::uuid, 0.85, 'Mapped proposal 14fa5ecc: Claim A justifies the Rio capital deployment layer because capital allocation steers which futures receive resources.'),
    ('3f7a9f84-eb2e-5d94-9ca4-afc3bad47f96'::uuid, '4f3e3d77-50fa-520a-8c8b-5a4269d94a9b'::uuid, 'serves', null::uuid, '6248818e-fe16-4d3b-8008-ef9752b80632'::uuid, null::uuid, null::uuid, null::uuid, 0.8, 'Maps the fund-build / better-attractors fragment to the existing Back shared root instead of forcing it into public.claim_edges.'),
    ('d18eaa71-d458-53cc-baac-e5306a1779e2'::uuid, '063528a6-a07c-5c14-abdf-dc12a0b10fcf'::uuid, 'justified_by', null::uuid, null::uuid, null::uuid, '67003e10-e1ee-5905-8b51-1b0cb263ec11'::uuid, null::uuid, 0.7, 'Mapped proposal 14fa5ecc: the internet-finance capital product is justified by the structural-advantages claim, not by the old compressed claim.'),
    ('33d9eb08-c9c2-5e2d-a9a7-3d7f1b5f0f0c'::uuid, '063528a6-a07c-5c14-abdf-dc12a0b10fcf'::uuid, 'justified_by', null::uuid, null::uuid, null::uuid, 'e6ce4e36-15d2-53bc-8b74-dda0efc095c1'::uuid, null::uuid, 0.85, 'Represents the proposal''s constrains fragment within the current anchor-role enum: internet finance is a candidate mechanism, not Teleo''s telos.'),
    ('3b2a8908-120d-59a5-bbd5-d2711a123aa3'::uuid, '063528a6-a07c-5c14-abdf-dc12a0b10fcf'::uuid, 'serves', '4f3e3d77-50fa-520a-8c8b-5a4269d94a9b'::uuid, null::uuid, null::uuid, null::uuid, null::uuid, 0.75, 'The internet-finance product objective serves Rio''s capital deployment layer rather than standing as a standalone telos.');

  get diagnostics inserted_anchors = row_count;
  if inserted_anchors <> 5 then
    raise exception 'Rio strategy packet inserted % strategy anchor row(s), expected 5', inserted_anchors;
  end if;
end $$;

commit;
