-- file: validation_sql/SQL_VALIDATION_RAYDIUM_AMM_V4_0_7_51_PRE3_MATERIALIZATION_EXPLANATIONS.sql
-- Raydium AMM v4 0.7.51-pre.3 materialization explanation checks.

-- 1. AMM v4 legacy generic swap must stay absent.
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_amm_v4'
  AND de.event_kind = 'raydium_amm_v4.swap'
GROUP BY de.event_kind;

-- 2. AMM v4 decoded events must all have coverage entries.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_amm_v4'
   AND ce.local_event_kind = de.event_kind
WHERE de.protocol_name = 'raydium_amm_v4'
  AND ce.id IS NULL
GROUP BY de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

-- 3. AMM v4 observations must use one-byte discriminators.
SELECT
    discriminator_hex,
    COUNT(*) AS observation_count,
    COUNT(DISTINCT signature) AS tx_count
FROM k_sol_instruction_observations
WHERE decoder_code = 'raydium_amm_v4'
  AND discriminator_hex IS NOT NULL
  AND length(discriminator_hex) > 2
GROUP BY discriminator_hex
ORDER BY observation_count DESC, discriminator_hex;

-- 4. Non-swap AMM v4 events must never materialize as trades.
SELECT
    de.event_kind,
    ce.event_family,
    COUNT(*) AS decoded_count,
    COUNT(te.id) AS trade_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_dex_event_coverage_entries ce
    ON ce.decoder_code = 'raydium_amm_v4'
   AND ce.local_event_kind = de.event_kind
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_amm_v4'
GROUP BY de.event_kind, ce.event_family
HAVING ce.event_family <> 'swap'
   AND COUNT(te.id) > 0
ORDER BY trade_count DESC, de.event_kind;

-- 5. Failed transactions must not materialize AMM v4 trades.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_failed_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_trade_events te
    ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_amm_v4'
  AND tx.err_json IS NOT NULL
  AND tx.err_json <> ''
  AND tx.err_json <> 'null'
GROUP BY de.event_kind
HAVING COUNT(te.id) > 0
ORDER BY trade_count DESC, de.event_kind;

-- 6. Successful AMM v4 decoded events that are not materialized must carry an explanation.
SELECT
    de.event_kind,
    COUNT(*) AS unexplained_count
FROM k_sol_dex_decoded_events de
JOIN k_sol_chain_transactions tx
    ON tx.id = de.transaction_id
LEFT JOIN k_sol_trade_events te
    ON te.decoded_event_id = de.id
LEFT JOIN k_sol_liquidity_events le
    ON le.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events pe
    ON pe.decoded_event_id = de.id
LEFT JOIN k_sol_fee_events fe
    ON fe.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events ae
    ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_orderbook_events oe
    ON oe.decoded_event_id = de.id
LEFT JOIN k_sol_token_account_events tae
    ON tae.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_amm_v4'
  AND (
      tx.err_json IS NULL
      OR tx.err_json = ''
      OR tx.err_json = 'null'
  )
  AND te.id IS NULL
  AND le.id IS NULL
  AND pe.id IS NULL
  AND fe.id IS NULL
  AND ae.id IS NULL
  AND oe.id IS NULL
  AND tae.id IS NULL
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipTradeReason')), '') = ''
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipLiquidityReason')), '') = ''
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipLifecycleReason')), '') = ''
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipCatalogReason')), '') = ''
  AND de.event_kind NOT IN (
      'raydium_amm_v4.simulate_info'
  )
GROUP BY de.event_kind
ORDER BY unexplained_count DESC, de.event_kind;

-- 7. pre_initialize should materialize successful legacy/deprecated instructions as lifecycle audit rows.
SELECT
    COUNT(*) AS decoded_success_count,
    COUNT(pe.id) AS lifecycle_count
FROM k_sol_dex_decoded_events de
JOIN k_sol_chain_transactions tx
    ON tx.id = de.transaction_id
LEFT JOIN k_sol_pool_lifecycle_events pe
    ON pe.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_amm_v4'
  AND de.event_kind = 'raydium_amm_v4.pre_initialize'
  AND (
      tx.err_json IS NULL
      OR tx.err_json = ''
      OR tx.err_json = 'null'
  );

-- 8. Deposit context by real catalog keys.
SELECT
    de.pool_account,
    COUNT(DISTINCT de.id) AS decoded_count,
    COUNT(DISTINCT le.id) AS liquidity_count,
    COUNT(DISTINCT p.id) AS pool_catalog_count,
    COUNT(DISTINCT pair.id) AS pair_catalog_count,
    GROUP_CONCAT(DISTINCT pair.id) AS pair_ids
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_pools p
    ON p.address = de.pool_account
LEFT JOIN k_sol_pairs pair
    ON pair.pool_id = p.id
WHERE de.protocol_name = 'raydium_amm_v4'
  AND de.event_kind = 'raydium_amm_v4.deposit'
GROUP BY de.pool_account
ORDER BY decoded_count DESC, de.pool_account;
