-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CLMM_0_7_49_PRE15.sql
-- Raydium CLMM validation after pre.15 final-refresh audit cleanup.

-- 1. Coverage summary should stay populated after cleanup + refresh.
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;

-- 2. Duplicate audit rows should be zero after pre.15.
SELECT
    json_extract(audit.payload_json, '$.discriminatorHex') AS discriminator_hex,
    audit.event_kind AS audit_event_kind,
    named.event_kind AS named_event_kind,
    COUNT(*) AS duplicate_count
FROM k_sol_dex_decoded_events audit
JOIN k_sol_dex_decoded_events named
    ON named.transaction_id = audit.transaction_id
   AND named.protocol_name = 'raydium_clmm'
   AND named.event_kind <> 'raydium_clmm.instruction_audit'
   AND COALESCE(
        json_extract(named.payload_json, '$.instructionDiscriminatorHex'),
        json_extract(named.payload_json, '$.instruction_discriminator_hex'),
        json_extract(named.payload_json, '$.discriminatorHex'),
        json_extract(named.payload_json, '$.discriminator_hex')
   ) = COALESCE(
        json_extract(audit.payload_json, '$.discriminatorHex'),
        json_extract(audit.payload_json, '$.discriminator_hex'),
        json_extract(audit.payload_json, '$.instructionDiscriminatorHex'),
        json_extract(audit.payload_json, '$.instruction_discriminator_hex')
   )
WHERE audit.protocol_name = 'raydium_clmm'
  AND audit.event_kind = 'raydium_clmm.instruction_audit'
GROUP BY discriminator_hex, audit_event_kind, named_event_kind
ORDER BY duplicate_count DESC;

-- 3. Residual audits should mostly be unknown discriminants.
SELECT
    json_extract(payload_json, '$.discriminatorHex') AS discriminator_hex,
    COUNT(*) AS residual_audit_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
ORDER BY residual_audit_count DESC, discriminator_hex;

-- 4. Manual cleanup equivalent: run only if query 2 still returns rows.
-- DELETE FROM k_sol_dex_decoded_events
-- WHERE protocol_name = 'raydium_clmm'
--   AND event_kind = 'raydium_clmm.instruction_audit'
--   AND EXISTS (
--         SELECT 1
--         FROM k_sol_dex_decoded_events named
--         WHERE named.transaction_id = k_sol_dex_decoded_events.transaction_id
--           AND named.protocol_name = 'raydium_clmm'
--           AND named.event_kind <> 'raydium_clmm.instruction_audit'
--           AND COALESCE(
--                 json_extract(named.payload_json, '$.instructionDiscriminatorHex'),
--                 json_extract(named.payload_json, '$.instruction_discriminator_hex'),
--                 json_extract(named.payload_json, '$.discriminatorHex'),
--                 json_extract(named.payload_json, '$.discriminator_hex')
--               ) = COALESCE(
--                 json_extract(k_sol_dex_decoded_events.payload_json, '$.discriminatorHex'),
--                 json_extract(k_sol_dex_decoded_events.payload_json, '$.discriminator_hex'),
--                 json_extract(k_sol_dex_decoded_events.payload_json, '$.instructionDiscriminatorHex'),
--                 json_extract(k_sol_dex_decoded_events.payload_json, '$.instruction_discriminator_hex')
--               )
--       );
