-- Pump.fun 0.7.54 materialization follow-up validation.
-- Run after a forced replay with skipDexDecode=no, forceDexDecode=yes, deferInstructionObservations=yes.

-- 01. Materialization summary by decoded event kind.
SELECT
    de.event_kind,
    COUNT(DISTINCT de.id) AS decoded_count,
    COUNT(DISTINCT te.id) AS trade_count,
    COUNT(DISTINCT lae.id) AS launch_count,
    COUNT(DISTINCT fee.id) AS fee_count,
    COUNT(DISTINCT rew.id) AS reward_count,
    COUNT(DISTINCT adm.id) AS admin_count,
    COUNT(DISTINCT ple.id) AS lifecycle_count,
    COUNT(DISTINCT tae.id) AS token_account_count,
    MIN(tx.signature) AS sample_signature
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_launch_events lae ON lae.decoded_event_id = de.id
LEFT JOIN k_sol_fee_events fee ON fee.decoded_event_id = de.id
LEFT JOIN k_sol_reward_events rew ON rew.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events adm ON adm.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events ple ON ple.decoded_event_id = de.id
LEFT JOIN k_sol_token_account_events tae ON tae.decoded_event_id = de.id
WHERE de.protocol_name = 'pump_fun'
GROUP BY de.event_kind
ORDER BY de.event_kind;

-- 02. Hard safety: no materialization on failed transactions.
SELECT
    de.event_kind,
    COUNT(DISTINCT de.id) AS decoded_failed_count,
    COUNT(DISTINCT te.id) AS trade_count,
    COUNT(DISTINCT lae.id) AS launch_count,
    COUNT(DISTINCT fee.id) AS fee_count,
    COUNT(DISTINCT rew.id) AS reward_count,
    COUNT(DISTINCT adm.id) AS admin_count,
    COUNT(DISTINCT ple.id) AS lifecycle_count,
    MIN(tx.signature) AS sample_signature
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_launch_events lae ON lae.decoded_event_id = de.id
LEFT JOIN k_sol_fee_events fee ON fee.decoded_event_id = de.id
LEFT JOIN k_sol_reward_events rew ON rew.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events adm ON adm.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events ple ON ple.decoded_event_id = de.id
WHERE de.protocol_name = 'pump_fun'
  AND tx.err_json IS NOT NULL
  AND tx.err_json <> ''
  AND tx.err_json <> 'null'
GROUP BY de.event_kind
HAVING trade_count > 0
    OR launch_count > 0
    OR fee_count > 0
    OR reward_count > 0
    OR admin_count > 0
    OR lifecycle_count > 0
ORDER BY de.event_kind;

-- 03. Hard safety: one decoded event must not feed multiple business targets.
SELECT
    de.event_kind,
    COUNT(DISTINCT de.id) AS decoded_count,
    COUNT(DISTINCT te.id) AS trade_count,
    COUNT(DISTINCT lae.id) AS launch_count,
    COUNT(DISTINCT fee.id) AS fee_count,
    COUNT(DISTINCT rew.id) AS reward_count,
    COUNT(DISTINCT adm.id) AS admin_count,
    COUNT(DISTINCT ple.id) AS lifecycle_count,
    (
        CASE WHEN COUNT(DISTINCT te.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT lae.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT fee.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT rew.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT adm.id) > 0 THEN 1 ELSE 0 END
      + CASE WHEN COUNT(DISTINCT ple.id) > 0 THEN 1 ELSE 0 END
    ) AS target_count
FROM k_sol_dex_decoded_events de
LEFT JOIN k_sol_trade_events te ON te.decoded_event_id = de.id
LEFT JOIN k_sol_launch_events lae ON lae.decoded_event_id = de.id
LEFT JOIN k_sol_fee_events fee ON fee.decoded_event_id = de.id
LEFT JOIN k_sol_reward_events rew ON rew.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events adm ON adm.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events ple ON ple.decoded_event_id = de.id
WHERE de.protocol_name = 'pump_fun'
GROUP BY de.event_kind
HAVING target_count > 1
ORDER BY target_count DESC, de.event_kind;

-- 04. Trade duplicate safety: do not materialize trade_event when an instruction trade in the same tx was materialized.
WITH materialized_pump_fun_trades AS (
    SELECT
        tx.signature,
        de.event_kind,
        COUNT(te.id) AS trade_count
    FROM k_sol_trade_events te
    JOIN k_sol_dex_decoded_events de ON de.id = te.decoded_event_id
    JOIN k_sol_chain_transactions tx ON tx.id = de.transaction_id
    WHERE de.protocol_name = 'pump_fun'
    GROUP BY tx.signature, de.event_kind
)
SELECT
    signature,
    SUM(CASE WHEN event_kind = 'pump_fun.trade_event' THEN trade_count ELSE 0 END) AS trade_event_materialized,
    SUM(CASE WHEN event_kind IN (
        'pump_fun.buy',
        'pump_fun.sell',
        'pump_fun.buy_v2',
        'pump_fun.sell_v2',
        'pump_fun.buy_exact_sol_in',
        'pump_fun.buy_exact_quote_in_v2'
    ) THEN trade_count ELSE 0 END) AS instruction_trade_materialized
FROM materialized_pump_fun_trades
GROUP BY signature
HAVING trade_event_materialized > 0
   AND instruction_trade_materialized > 0
ORDER BY signature;

-- 05. Residual successful materializable rows without a business target and without skip reason.
SELECT
    de.event_kind,
    COUNT(*) AS unexplained_count,
    MIN(tx.signature) AS sample_signature
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_launch_events lae ON lae.decoded_event_id = de.id
LEFT JOIN k_sol_fee_events fee ON fee.decoded_event_id = de.id
LEFT JOIN k_sol_reward_events rew ON rew.decoded_event_id = de.id
LEFT JOIN k_sol_pool_admin_events adm ON adm.decoded_event_id = de.id
LEFT JOIN k_sol_pool_lifecycle_events ple ON ple.decoded_event_id = de.id
WHERE de.protocol_name = 'pump_fun'
  AND (tx.err_json IS NULL OR tx.err_json = '' OR tx.err_json = 'null')
  AND te.id IS NULL
  AND lae.id IS NULL
  AND fee.id IS NULL
  AND rew.id IS NULL
  AND adm.id IS NULL
  AND ple.id IS NULL
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipTradeReason')), '') = ''
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipCandleReason')), '') = ''
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipLifecycleReason')), '') = ''
  AND COALESCE(TRIM(json_extract(de.payload_json, '$.skipCatalogReason')), '') = ''
GROUP BY de.event_kind
ORDER BY unexplained_count DESC, de.event_kind;
