-- file: validation_sql/SQL_VALIDATION_RAYDIUM_LAUNCHPAD_0_7_50_PRE9.sql
-- Raydium Launchpad pre9 validation additions.

-- 1. Coverage entries, including late Carbon instructions and launch-event target table.
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_launchpad'
ORDER BY entry_kind, entry_name, discriminator_hex;

-- 2. Launchpad-specific materialized events.
SELECT
    le.event_kind,
    le.event_role,
    le.pool_account,
    COUNT(*) AS launch_event_count,
    COUNT(DISTINCT le.signature) AS tx_count
FROM k_sol_launch_events le
WHERE le.protocol_name = 'raydium_launchpad'
GROUP BY le.event_kind, le.event_role, le.pool_account
ORDER BY launch_event_count DESC, le.event_kind, le.pool_account;

-- 3. Fee materialization for Launchpad fee instructions.
SELECT
    fe.event_kind,
    fe.pool_account,
    COUNT(*) AS fee_event_count,
    COUNT(DISTINCT fe.signature) AS tx_count
FROM k_sol_fee_events fe
WHERE fe.protocol_name = 'raydium_launchpad'
GROUP BY fe.event_kind, fe.pool_account
ORDER BY fee_event_count DESC, fe.event_kind;

-- 4. Admin/config/access materialization for Launchpad admin instructions.
SELECT
    ae.event_kind,
    ae.admin_action,
    ae.pool_account,
    COUNT(*) AS admin_event_count,
    COUNT(DISTINCT ae.signature) AS tx_count
FROM k_sol_pool_admin_events ae
WHERE ae.protocol_name = 'raydium_launchpad'
GROUP BY ae.event_kind, ae.admin_action, ae.pool_account
ORDER BY admin_event_count DESC, ae.event_kind;

-- 5. Late Carbon instruction corpus identifiers.
SELECT
    instruction_name,
    discriminator_hex,
    COUNT(*) AS observed_count,
    COUNT(DISTINCT signature) AS tx_count
FROM k_sol_instruction_observations
WHERE decoder_code = 'raydium_launchpad'
  AND discriminator_hex IN (
      '7bb4b8816fb9bb3b', -- close_platform_global_access
      'a25b92c75d85eaed', -- create_platform_global_access
      '9247ad4562130f6a', -- create_platform_vesting_account
      'e445a52e51cb9a1d'  -- cpi_event / Anchor self-CPI transport
  )
GROUP BY instruction_name, discriminator_hex
ORDER BY observed_count DESC, discriminator_hex;

-- 6. Residual Launchpad decoded events that are still only decoded, not materialized.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(fe.id) AS fee_count,
    COUNT(ae.id) AS admin_count,
    COUNT(le.id) AS launch_count,
    COUNT(pe.id) AS lifecycle_count,
    COUNT(te.id) AS trade_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_admin_events ae ON ae.decoded_event_id = de.id
LEFT JOIN k_sol_launch_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_trade_events te ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_launchpad'
GROUP BY de.event_kind
ORDER BY decoded_count DESC, de.event_kind;

-- 7. Residual decoded-only Launchpad events split by failed/success transaction.
SELECT
    de.event_kind,
    COUNT(*) AS decoded_count,
    COUNT(CASE WHEN tx.err_json IS NOT NULL AND tx.err_json <> '' AND tx.err_json <> 'null' THEN de.id END) AS failed_tx_count,
    COUNT(fe.id) AS fee_count,
    COUNT(ae.id) AS admin_count,
    COUNT(le.id) AS launch_count,
    COUNT(pe.id) AS lifecycle_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_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_launch_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_trade_events te ON te.decoded_event_id = de.id
WHERE de.protocol_name = 'raydium_launchpad'
GROUP BY de.event_kind
HAVING (fee_count + admin_count + launch_count + lifecycle_count + trade_count) < decoded_count
ORDER BY decoded_count DESC, de.event_kind;
