-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CLMM_0_7_50_RECHECK.sql

-- Raydium CLMM 0.7.50 re-verification pack.
-- Goal: repeat the Launchpad-style closure checks on a dedicated CLMM
-- database, after replay with skipDexDecode='no' and forceDexDecode='yes'.
-- Expected invariants:
-- - no `unknown` or empty event_family except the synthetic `program` row;
-- - no observed discriminator missing from coverage;
-- - no residual upstream fallback for locally-covered entries;
-- - no successful, actionable decoded event left without its materialized row;
-- - no failed transaction materialized into business tables;
-- - decoded-only entries such as CPMM anchor IDL management and CLMM router/program-data
--   corroboration events are excluded from materialization shortfall checks.

-- 01. Coverage detail.
SELECT
    entry_name,
    entry_kind,
    event_family,
    expected_db_target,
    proof_status,
    local_event_kind,
    discriminator_hex,
    observed_count,
    materialized_count,
    trade_count
FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'raydium_clmm'
ORDER BY entry_kind, entry_name, discriminator_hex;

-- 02. Coverage summary.
SELECT
    decoder_code,
    COUNT(*) AS listed_entry_count,
    SUM(CASE WHEN local_event_kind IS NOT NULL AND local_event_kind <> '' THEN 1 ELSE 0 END) AS decoded_entry_count,
    SUM(CASE WHEN observed_count > 0 THEN 1 ELSE 0 END) AS observed_entry_count,
    SUM(CASE WHEN materialized_count > 0 THEN 1 ELSE 0 END) AS materialized_entry_count,
    COALESCE(SUM(observed_count), 0) AS total_observed_count,
    COALESCE(SUM(materialized_count), 0) AS total_materialized_count,
    COALESCE(SUM(trade_count), 0) AS trade_count
FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'raydium_clmm'
GROUP BY decoder_code;

-- 03. Coverage family normalization guard.
-- Expected: zero rows, or only the synthetic `program` row if it intentionally has no family.
SELECT
    entry_name,
    entry_kind,
    event_family,
    expected_db_target,
    proof_status,
    local_event_kind,
    discriminator_hex,
    observed_count,
    materialized_count,
    trade_count
FROM k_sol_dex_event_coverage_entries
WHERE decoder_code = 'raydium_clmm'
  AND (
       event_family IS NULL
    OR event_family = ''
    OR event_family = 'unknown'
  )
  AND entry_kind <> 'program'
ORDER BY entry_kind, entry_name;

-- 04. Instruction observations by discriminator.
SELECT
    instruction_name,
    discriminator_hex,
    COUNT(*) AS observed_count,
    COUNT(DISTINCT signature) AS tx_count
FROM k_sol_instruction_observations
WHERE decoder_code = 'raydium_clmm'
GROUP BY instruction_name, discriminator_hex
ORDER BY observed_count DESC, instruction_name, discriminator_hex;

-- 05. Observed discriminators absent from coverage.
-- Expected: zero rows.
SELECT
    io.discriminator_hex,
    MIN(io.instruction_name) AS sample_instruction_name,
    MIN(io.decoded_event_kind) AS sample_decoded_event_kind,
    COUNT(*) AS observed_count,
    COUNT(DISTINCT io.signature) AS tx_count
FROM k_sol_instruction_observations io
LEFT JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_clmm'
   AND ce.discriminator_hex = io.discriminator_hex
WHERE io.decoder_code = 'raydium_clmm'
  AND io.discriminator_hex IS NOT NULL
  AND io.discriminator_hex <> ''
  AND ce.id IS NULL
GROUP BY io.discriminator_hex
ORDER BY observed_count DESC, io.discriminator_hex;

-- 06. Decoded event distribution and materialization links.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(le.id) AS liquidity_count,
    COUNT(fe.id) AS fee_count,
    COUNT(re.id) AS reward_count,
    COUNT(ae.id) AS admin_count,
    COUNT(la.id) AS launch_count,
    COUNT(pe.id) AS lifecycle_count,
    COUNT(oe.id) AS orderbook_count,
    COUNT(ta.id) AS token_account_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_liquidity_events le ON le.decoded_event_id = de.id
LEFT JOIN k_sol_fee_events fe ON fe.decoded_event_id = de.id
LEFT JOIN k_sol_reward_events re ON re.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events ae ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_launch_events la ON la.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe ON pe.decoded_event_id = de.id
LEFT JOIN k_sol_orderbook_events oe ON oe.decoded_event_id = de.id
LEFT JOIN k_sol_token_account_events ta ON ta.decoded_event_id = de.id
LEFT JOIN k_sol_trade_events te ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_clmm'
GROUP BY de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

-- 07. Successful decoded events not materialized where coverage target says they should be.
-- Expected: zero rows, except expected_db_target='k_sol_dex_decoded_events_only' and failed tx rows.
SELECT
    de.event_kind,
    ce.entry_name,
    ce.entry_kind,
    ce.event_family,
    ce.expected_db_target,
    COUNT(*) AS decoded_count,
    COUNT(CASE WHEN tx.err_json IS NOT NULL AND tx.err_json <> '' AND tx.err_json <> 'null' THEN de.id END) AS failed_tx_count,
    COUNT(le.id) AS liquidity_count,
    COUNT(fe.id) AS fee_count,
    COUNT(re.id) AS reward_count,
    COUNT(ae.id) AS admin_count,
    COUNT(la.id) AS launch_count,
    COUNT(pe.id) AS lifecycle_count,
    COUNT(oe.id) AS orderbook_count,
    COUNT(ta.id) AS token_account_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
JOIN k_sol_chain_transactions tx ON tx.id = de.transaction_id
LEFT JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_clmm'
   AND ce.local_event_kind = de.event_kind
LEFT JOIN k_sol_liquidity_events le ON le.decoded_event_id = de.id
LEFT JOIN k_sol_fee_events fe ON fe.decoded_event_id = de.id
LEFT JOIN k_sol_reward_events re ON re.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events ae ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_launch_events la ON la.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe ON pe.decoded_event_id = de.id
LEFT JOIN k_sol_orderbook_events oe ON oe.decoded_event_id = de.id
LEFT JOIN k_sol_token_account_events ta ON ta.decoded_event_id = de.id
LEFT JOIN k_sol_trade_events te ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_clmm'
GROUP BY de.event_kind, ce.entry_name, ce.entry_kind, ce.event_family, ce.expected_db_target
HAVING
    COALESCE(ce.expected_db_target, '') <> 'k_sol_dex_decoded_events_only'
    AND (liquidity_count + fee_count + reward_count + admin_count + launch_count + lifecycle_count + orderbook_count + token_account_count + trade_count + failed_tx_count) < decoded_count
ORDER BY decoded_count DESC, de.event_kind;

-- 08. Failed transaction materialization guard.
-- Expected: zero rows.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(le.id) AS liquidity_count,
    COUNT(fe.id) AS fee_count,
    COUNT(re.id) AS reward_count,
    COUNT(ae.id) AS admin_count,
    COUNT(la.id) AS launch_count,
    COUNT(pe.id) AS lifecycle_count,
    COUNT(oe.id) AS orderbook_count,
    COUNT(ta.id) AS token_account_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
JOIN k_sol_chain_transactions tx ON tx.id = de.transaction_id
LEFT JOIN k_sol_liquidity_events le ON le.decoded_event_id = de.id
LEFT JOIN k_sol_fee_events fe ON fe.decoded_event_id = de.id
LEFT JOIN k_sol_reward_events re ON re.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events ae ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_launch_events la ON la.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe ON pe.decoded_event_id = de.id
LEFT JOIN k_sol_orderbook_events oe ON oe.decoded_event_id = de.id
LEFT JOIN k_sol_token_account_events ta ON ta.decoded_event_id = de.id
LEFT JOIN k_sol_trade_events te ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_clmm'
  AND tx.err_json IS NOT NULL
  AND tx.err_json <> ''
  AND tx.err_json <> 'null'
GROUP BY de.event_kind
HAVING
       COUNT(le.id) > 0
    OR COUNT(fe.id) > 0
    OR COUNT(re.id) > 0
    OR COUNT(ae.id) > 0
    OR COUNT(la.id) > 0
    OR COUNT(pe.id) > 0
    OR COUNT(oe.id) > 0
    OR COUNT(ta.id) > 0
    OR COUNT(te.id) > 0
ORDER BY de.event_kind;

-- 09. Residual instruction audit rows.
-- Expected: zero rows after full direct mapping.
SELECT
    json_extract(payload_json, '$.discriminatorHex') AS discriminator_hex,
    COUNT(*) AS audit_count,
    COUNT(DISTINCT transaction_id) AS tx_count
FROM k_sol_dex_decoded_events
WHERE protocol_name = 'raydium_clmm'
  AND event_kind = 'raydium_clmm.instruction_audit'
GROUP BY discriminator_hex
ORDER BY audit_count DESC, discriminator_hex;

-- 10. Redundant upstream fallback matches for locally covered entries.
-- Expected: zero rows.
SELECT
    json_extract(ug.payload_json, '$.upstreamDecoderCode') AS upstream_decoder_code,
    json_extract(ug.payload_json, '$.upstreamEntryName') AS entry_name,
    json_extract(ug.payload_json, '$.upstreamDiscriminatorHex') AS discriminator_hex,
    json_extract(ug.payload_json, '$.upstreamSourceRepo') AS source_repo,
    COUNT(*) AS fallback_count,
    COUNT(DISTINCT ug.transaction_id) AS tx_count
FROM k_sol_dex_decoded_events ug
JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = json_extract(ug.payload_json, '$.upstreamDecoderCode')
   AND ce.entry_name = json_extract(ug.payload_json, '$.upstreamEntryName')
   AND ce.discriminator_hex = json_extract(ug.payload_json, '$.upstreamDiscriminatorHex')
   AND ce.local_event_kind IS NOT NULL
   AND ce.local_event_kind <> ''
WHERE ug.protocol_name = 'upstream_git'
  AND ug.event_kind = 'upstream_git.instruction_match'
  AND json_extract(ug.payload_json, '$.upstreamDecoderCode') = 'raydium_clmm'
GROUP BY upstream_decoder_code, entry_name, discriminator_hex, source_repo
ORDER BY fallback_count DESC, entry_name;

-- 11. Decoded event kinds without a coverage entry.
-- Expected: zero rows, excluding explicit `.instruction_audit` if it remains by design.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_clmm'
   AND ce.local_event_kind = de.event_kind
WHERE de.protocol_name = 'raydium_clmm'
  AND ce.id IS NULL
GROUP BY de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

-- 12. Non-swap decoded events that created trades.
-- Expected: zero rows.
SELECT
    de.event_kind,
    ce.event_family,
    COUNT(*) AS decoded_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_clmm'
   AND ce.local_event_kind = de.event_kind
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_clmm'
GROUP BY de.event_kind, ce.event_family
HAVING ce.event_family <> 'swap'
   AND COUNT(te.id) > 0
ORDER BY trade_count DESC, de.event_kind;
