-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CLMM_0_7_49_PRE17.sql
-- Raydium CLMM validation after limit-order instruction mapping.
-- Expected coverage listed_entry_count becomes 45 because open/increase/decrease_limit_order are now listed.

-- 1. CLMM 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;

-- 2. New CLMM limit-order 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'
  AND entry_name IN (
      'open_limit_order',
      'increase_limit_order',
      'decrease_limit_order',
      'close_limit_order',
      'settle_limit_order'
  )
ORDER BY entry_name;

-- 3. The formerly residual discriminants must now decode as named events.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(DISTINCT de.transaction_id) AS transaction_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_clmm'
  AND de.event_kind IN (
      'raydium_clmm.open_limit_order',
      'raydium_clmm.increase_limit_order',
      'raydium_clmm.decrease_limit_order'
  )
GROUP BY de.event_kind
ORDER BY de.event_kind;

-- 4. No residual audit rows for the newly mapped 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'
  AND json_extract(payload_json, '$.discriminatorHex') IN (
      '9d20dab7471d1293',
      'b19059ecfaba7d63',
      '759d3c674231a300'
  )
GROUP BY discriminator_hex
ORDER BY residual_audit_count DESC;

-- 5. No trade/candle safety: limit-order maintenance must never produce trade rows.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_clmm'
  AND de.event_kind IN (
      'raydium_clmm.open_limit_order',
      'raydium_clmm.increase_limit_order',
      'raydium_clmm.decrease_limit_order',
      'raydium_clmm.close_limit_order',
      'raydium_clmm.settle_limit_order'
  )
GROUP BY de.event_kind
ORDER BY de.event_kind;

-- 6. Duplicate audit rows should stay empty after mapped-audit cleanup.
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;
