-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CLMM_0_7_49_PRE7.sql
-- Raydium CLMM pre.7 validation: direct materialization + audit cleanup.

-- 1. Confirm corpus-observed CLMM non-trades are materialized.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(fe.id) AS fee_count,
    COUNT(ple.id) AS lifecycle_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_fee_events fe
    ON fe.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events ple
    ON ple.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_clmm'
  AND de.event_kind IN (
      'raydium_clmm.create_pool',
      'raydium_clmm.collect_protocol_fee'
  )
GROUP BY de.event_kind
ORDER BY de.event_kind;

-- 2. Confirm residual audits for the same discriminants are gone.
SELECT
    json_extract(payload_json, '$.instructionDiscriminatorHex') AS instruction_discriminator_hex,
    json_extract(payload_json, '$.instruction_discriminator_hex') AS instruction_discriminator_hex_snake,
    json_extract(payload_json, '$.discriminatorHex') AS discriminator_hex,
    json_extract(payload_json, '$.discriminator_hex') AS discriminator_hex_snake,
    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'
  AND (
      json_extract(payload_json, '$.instructionDiscriminatorHex') IN ('e992d18ecf6840bc', '8888fcddc2427e59')
      OR json_extract(payload_json, '$.instruction_discriminator_hex') IN ('e992d18ecf6840bc', '8888fcddc2427e59')
      OR json_extract(payload_json, '$.discriminatorHex') IN ('e992d18ecf6840bc', '8888fcddc2427e59')
      OR json_extract(payload_json, '$.discriminator_hex') IN ('e992d18ecf6840bc', '8888fcddc2427e59')
  )
GROUP BY
    instruction_discriminator_hex,
    instruction_discriminator_hex_snake,
    discriminator_hex,
    discriminator_hex_snake,
    account_count
ORDER BY decoded_count DESC;

-- 3. Full CLMM distribution with materialization counters.
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;

-- 4. Coverage summary after sync/replay.
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;
