-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CLMM_0_7_49_PRE10.sql
-- Raydium CLMM validation after pre.10 mapped instruction expansion.

-- 1. Coverage rows.
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;

-- 2. 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;

-- 3. Instruction observations, including unknown discriminants.
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;

-- 4. Decoded CLMM distribution. After pre.10, known listed instructions should move
-- from raydium_clmm.instruction_audit to named raydium_clmm.<instruction> events.
SELECT
    de.protocol_name,
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(DISTINCT de.transaction_id) AS transaction_count
FROM k_sol_dex_decoded_events de
WHERE de.protocol_name = 'raydium_clmm'
GROUP BY de.protocol_name, de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

-- 5. Non-trade materialization distribution.
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(pa.id) AS admin_count,
    COUNT(ple.id) AS lifecycle_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 pa ON pa.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events ple ON ple.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 de.event_kind;

-- 6. Residual known CLMM instruction audits: should be zero for listed instruction discriminants.
SELECT
    json_extract(payload_json, '$.discriminatorHex') AS discriminator_hex,
    COUNT(*) AS decoded_count,
    COUNT(DISTINCT transaction_id) AS transaction_count
FROM k_sol_dex_decoded_events
WHERE protocol_name = 'raydium_clmm'
  AND event_kind = 'raydium_clmm.instruction_audit'
  AND json_extract(payload_json, '$.discriminatorHex') IN (
      '4c7c800fd55725fa', '7b86510031446262', 'c975989055556cb2',
      'a78a4e95dfc2067e', '8888fcddc2427e59', '12eda6c52210d590',
      '8934edd4d7756c68', '2b44d4a7592fa401', 'bd0eb5785576e33e',
      '3f5794216d230868', 'e992d18ecf6840bc', '11fb415c88f20ea9',
      'a026d06f685b2c01', '3a7fbc3e4f52c460', '2e9cf3760dcdfbb2',
      '851d59df45eeb00a', '5f87c0c4f281e644', '87802f4d0f98f031',
      '4db84ad67056f1c7', '4dffae527d1dc92e', '7034a74b20c9d389',
      'cd4e74215c691a60', 'f8c69e91e17587c8', '457d73daf5baf2c4',
      '2b04ed0b1ac91e62', '07160c53f22b3079', '313cae889a1c74c8',
      '7f467728bce33d07', '82576c062ee0757b', 'a3ace0340b9a6adf'
  )
GROUP BY discriminator_hex
ORDER BY decoded_count DESC;

-- 7. Unknown observed discriminants to keep audit-only until identified.
SELECT
    json_extract(payload_json, '$.discriminatorHex') AS discriminator_hex,
    json_extract(payload_json, '$.accountCount') AS account_count,
    COUNT(*) AS decoded_count,
    COUNT(DISTINCT transaction_id) AS transaction_count
FROM k_sol_dex_decoded_events
WHERE protocol_name = 'raydium_clmm'
  AND event_kind = 'raydium_clmm.instruction_audit'
GROUP BY discriminator_hex, account_count
ORDER BY decoded_count DESC;
