-- file: validation_sql/SQL_VALIDATION_RAYDIUM_CLMM_0_7_49_PRE18.sql
-- Raydium CLMM validation after pre.18 orderbook-event schema and materialization.

-- 1. Schema must contain the new orderbook materialization table.
SELECT
    name
FROM sqlite_master
WHERE type = 'table'
  AND name = 'k_sol_orderbook_events';

-- 2. Cleanup any stale mapped CLMM instruction audits left from earlier runs.
-- This is safe: the discriminants below are now decoded as named raydium_clmm.* events.
DELETE FROM k_sol_dex_decoded_events
WHERE protocol_name = 'raydium_clmm'
  AND event_kind = 'raydium_clmm.instruction_audit'
  AND COALESCE(
        json_extract(payload_json, '$.discriminatorHex'),
        json_extract(payload_json, '$.discriminator_hex'),
        json_extract(payload_json, '$.instructionDiscriminatorHex'),
        json_extract(payload_json, '$.instruction_discriminator_hex')
      ) 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',
        '759d3c674231a300','9d20dab7471d1293','b19059ecfaba7d63'
      );

-- 3. Duplicate audits should be zero.
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;

-- 4. Limit-order decoded events must materialize into k_sol_orderbook_events and never trades.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(DISTINCT de.transaction_id) AS tx_count,
    COUNT(oe.id) AS orderbook_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_orderbook_events oe
    ON oe.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 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;

-- 5. No failed transaction should be orderbook-materialized.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(oe.id) AS orderbook_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_orderbook_events oe
    ON oe.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
ORDER BY decoded_count DESC, de.event_kind;

-- 6. CLMM coverage summary after 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;
