\set ON_ERROR_STOP on
-- Preflight for mapped rich proposal apply packet. Read-only.

select id::text, status, proposal_type, reviewed_by_handle, reviewed_at
from kb_stage.kb_proposals
where id in (select id from (values
    ('a64df080-8502-42e2-98f4-9bbdecb8da73'::uuid)
  ) as v(id))
order by id;

with required(id, reason) as (
  values
    ('25a350e0-ebce-4072-8f2a-48427a727553'::uuid, 'claim_edges.to_claim'),
    ('2ef388ab-4102-4416-a6e1-ae6ba8ee02fa'::uuid, 'claim_edges.to_claim'),
    ('8f3e773e-4255-47a7-ba8f-61c57a43bebe'::uuid, 'claim_edges.to_claim'),
    ('93d583d9-5394-4e6a-97ca-bcd48796d94a'::uuid, 'claim_edges.to_claim'),
    ('be59b559-de8b-4dec-a7be-773188c0e632'::uuid, 'claim_edges.to_claim')
)
select 'missing_required_existing_claims' as check_name, id::text, reason
from required
where id is not null
  and not exists (select 1 from public.claims where public.claims.id = required.id)
order by id;

-- Expected generated rows after authorized commit:
select 1::int as expected_reasoning_tools,
       5::int as expected_claims,
       13::int as expected_sources,
       7::int as expected_claim_edges,
       17::int as expected_claim_evidence;

-- Must be all zero before production apply.
select
  (select count(*) from public.reasoning_tools where id in (select id from (values
    ('473ba7b0-ce44-5e77-bc46-ef82a49b6a3e'::uuid)
  ) as v(id))) as generated_reasoning_tool_rows,
  (select count(*) from public.claims where id in (select id from (values
    ('23927224-0d72-5b37-a600-33c63277f6a9'::uuid),
    ('4701f303-ca9c-5da1-aa48-8cf28189c23c'::uuid),
    ('d2b2142b-c2d8-5e33-9a38-14598d94d715'::uuid),
    ('6014c3ea-3ba5-5ed1-bf15-31f6938e3fc3'::uuid),
    ('12d2c4aa-1c23-5173-b666-dea903dd7432'::uuid)
  ) as v(id))) as generated_claim_rows,
  (select count(*) from public.sources where id in (select id from (values
    ('02b8c5ff-04fb-54e7-b132-e731086f1436'::uuid),
    ('4bd41552-8017-551a-84b5-19b0107c20b5'::uuid),
    ('bc081b04-709f-565a-af0b-95d4ca0589fa'::uuid),
    ('dc1c238c-f835-5641-980a-f0fe53f8dfe5'::uuid),
    ('2df4bb44-4853-5455-8049-6dbb8685523b'::uuid),
    ('6fe15936-d5b9-514e-802f-32a2a06f4a23'::uuid),
    ('50e0d1ef-322b-5097-afe6-c9aa5ecea5fc'::uuid),
    ('621bb834-6366-5940-b6c3-c5f82ba35d48'::uuid),
    ('5f9dd7ca-83f8-5ea6-8e00-d9d13003f8fc'::uuid),
    ('97c3fc2f-16ae-5507-ad51-b1f4a1bcfb11'::uuid),
    ('11fa942d-e81a-5142-965d-3c60714a7dca'::uuid),
    ('263f423e-c16f-544c-ae5f-3e370e736e12'::uuid),
    ('0c87a244-afa2-5d08-8e79-9cb792aff553'::uuid)
  ) as v(id))) as generated_source_rows,
  (select count(*) from public.claim_edges e join (values
    ('23927224-0d72-5b37-a600-33c63277f6a9'::uuid, '8f3e773e-4255-47a7-ba8f-61c57a43bebe'::uuid, 'supports'::edge_type),
    ('23927224-0d72-5b37-a600-33c63277f6a9'::uuid, '2ef388ab-4102-4416-a6e1-ae6ba8ee02fa'::uuid, 'relates'::edge_type),
    ('4701f303-ca9c-5da1-aa48-8cf28189c23c'::uuid, '25a350e0-ebce-4072-8f2a-48427a727553'::uuid, 'relates'::edge_type),
    ('d2b2142b-c2d8-5e33-9a38-14598d94d715'::uuid, 'be59b559-de8b-4dec-a7be-773188c0e632'::uuid, 'relates'::edge_type),
    ('6014c3ea-3ba5-5ed1-bf15-31f6938e3fc3'::uuid, '93d583d9-5394-4e6a-97ca-bcd48796d94a'::uuid, 'relates'::edge_type),
    ('12d2c4aa-1c23-5173-b666-dea903dd7432'::uuid, '2ef388ab-4102-4416-a6e1-ae6ba8ee02fa'::uuid, 'supports'::edge_type),
    ('12d2c4aa-1c23-5173-b666-dea903dd7432'::uuid, '8f3e773e-4255-47a7-ba8f-61c57a43bebe'::uuid, 'supports'::edge_type)
  ) as v(from_claim, to_claim, edge_type) using (from_claim, to_claim, edge_type)) as generated_edge_rows,
  (select count(*) from public.claim_evidence ce join (values
    ('23927224-0d72-5b37-a600-33c63277f6a9'::uuid, '5f9dd7ca-83f8-5ea6-8e00-d9d13003f8fc'::uuid, 'illustrates'::evidence_role),
    ('23927224-0d72-5b37-a600-33c63277f6a9'::uuid, 'dc1c238c-f835-5641-980a-f0fe53f8dfe5'::uuid, 'grounds'::evidence_role),
    ('23927224-0d72-5b37-a600-33c63277f6a9'::uuid, '6fe15936-d5b9-514e-802f-32a2a06f4a23'::uuid, 'grounds'::evidence_role),
    ('4701f303-ca9c-5da1-aa48-8cf28189c23c'::uuid, '97c3fc2f-16ae-5507-ad51-b1f4a1bcfb11'::uuid, 'illustrates'::evidence_role),
    ('4701f303-ca9c-5da1-aa48-8cf28189c23c'::uuid, '4bd41552-8017-551a-84b5-19b0107c20b5'::uuid, 'grounds'::evidence_role),
    ('4701f303-ca9c-5da1-aa48-8cf28189c23c'::uuid, 'bc081b04-709f-565a-af0b-95d4ca0589fa'::uuid, 'grounds'::evidence_role),
    ('4701f303-ca9c-5da1-aa48-8cf28189c23c'::uuid, 'dc1c238c-f835-5641-980a-f0fe53f8dfe5'::uuid, 'grounds'::evidence_role),
    ('d2b2142b-c2d8-5e33-9a38-14598d94d715'::uuid, '11fa942d-e81a-5142-965d-3c60714a7dca'::uuid, 'illustrates'::evidence_role),
    ('d2b2142b-c2d8-5e33-9a38-14598d94d715'::uuid, 'dc1c238c-f835-5641-980a-f0fe53f8dfe5'::uuid, 'grounds'::evidence_role),
    ('d2b2142b-c2d8-5e33-9a38-14598d94d715'::uuid, 'bc081b04-709f-565a-af0b-95d4ca0589fa'::uuid, 'grounds'::evidence_role),
    ('d2b2142b-c2d8-5e33-9a38-14598d94d715'::uuid, '6fe15936-d5b9-514e-802f-32a2a06f4a23'::uuid, 'grounds'::evidence_role),
    ('6014c3ea-3ba5-5ed1-bf15-31f6938e3fc3'::uuid, '263f423e-c16f-544c-ae5f-3e370e736e12'::uuid, 'illustrates'::evidence_role),
    ('6014c3ea-3ba5-5ed1-bf15-31f6938e3fc3'::uuid, '2df4bb44-4853-5455-8049-6dbb8685523b'::uuid, 'grounds'::evidence_role),
    ('6014c3ea-3ba5-5ed1-bf15-31f6938e3fc3'::uuid, '6fe15936-d5b9-514e-802f-32a2a06f4a23'::uuid, 'grounds'::evidence_role),
    ('12d2c4aa-1c23-5173-b666-dea903dd7432'::uuid, '0c87a244-afa2-5d08-8e79-9cb792aff553'::uuid, 'illustrates'::evidence_role),
    ('12d2c4aa-1c23-5173-b666-dea903dd7432'::uuid, '4bd41552-8017-551a-84b5-19b0107c20b5'::uuid, 'grounds'::evidence_role),
    ('12d2c4aa-1c23-5173-b666-dea903dd7432'::uuid, 'dc1c238c-f835-5641-980a-f0fe53f8dfe5'::uuid, 'grounds'::evidence_role)
  ) as v(claim_id, source_id, role) using (claim_id, source_id, role)) as generated_evidence_rows;
