// file: kb_lib/src/db/dtos/local_pipeline_diagnostics.rs

//! DTOs for local pipeline diagnostics.

/// Local pipeline diagnostics summary.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalPipelineDiagnosticSummaryDto {
    /// Total persisted chain transactions.
    pub transaction_count: i64,
    /// Total successful chain transactions.
    pub ok_transaction_count: i64,
    /// Total failed chain transactions.
    pub failed_transaction_count: i64,
    /// Total decoded DEX events.
    pub decoded_event_count: i64,
    /// Total decoded DEX trade candidates.
    pub decoded_trade_candidate_count: i64,
    /// Total decoded DEX candle candidates.
    pub decoded_candle_candidate_count: i64,
    /// Total decoded useful non-trade events.
    pub decoded_non_trade_useful_event_count: i64,
    /// Total decoded swap-like events that are intentionally non-actionable.
    pub decoded_non_actionable_trade_event_count: i64,
    /// Total decoded events with unknown classification.
    pub decoded_unknown_event_count: i64,
    /// Total persisted liquidity events.
    pub liquidity_event_count: i64,
    /// Total persisted pool lifecycle events.
    pub pool_lifecycle_event_count: i64,
    /// Total persisted fee events.
    pub fee_event_count: i64,
    /// Total persisted reward events.
    pub reward_event_count: i64,
    /// Total persisted pool administration events.
    pub pool_admin_event_count: i64,
    /// Event coverage entries listed from upstream registry sources.
    pub event_coverage_listed_entry_count: u64,
    /// Event coverage entries that have a local decoder event kind mapping.
    pub event_coverage_decoded_entry_count: u64,
    /// Event coverage entries observed at least once in the local corpus.
    pub event_coverage_observed_entry_count: u64,
    /// Event coverage entries materialized at least once into a DB target.
    pub event_coverage_materialized_entry_count: u64,
    /// Sum of decoded-event observations across coverage entries.
    pub event_coverage_total_observed_count: u64,
    /// Sum of materialized rows across coverage entries.
    pub event_coverage_total_materialized_count: u64,
    /// Sum of trade rows linked to coverage entries.
    pub event_coverage_trade_count: u64,
    /// Coverage entries intentionally expected to remain audit-only.
    pub event_coverage_audit_only_entry_count: u64,
    /// Coverage entries whose DB target is still missing or undecided.
    pub event_coverage_missing_db_target_entry_count: u64,
    /// Coverage entries still marked as upstream Git unverified.
    pub event_coverage_upstream_git_unverified_entry_count: u64,
    /// Coverage entries mapped to local semantics but not observed locally yet.
    pub event_coverage_upstream_git_mapped_unverified_entry_count: u64,
    /// Coverage entries observed in local corpus but not necessarily materialized.
    pub event_coverage_upstream_git_local_corpus_observed_entry_count: u64,
    /// Coverage entries observed and materialized from local corpus.
    pub event_coverage_upstream_git_local_corpus_materialized_entry_count: u64,
    /// Event coverage summaries grouped by decoder.
    pub event_coverage_summaries: std::vec::Vec<crate::DexEventCoverageSummaryDto>,
    /// Whether the local persisted pipeline has no blocking diagnostic issue.
    pub diagnostics_clean: bool,
    /// Number of blocking diagnostic issues.
    ///
    /// This currently includes actionable missing trade events, invalid trade
    /// events, duplicate decoded-event trade rows, and duplicate candle buckets.
    pub blocking_issue_count: i64,
    /// Total decoded DEX events that are trade candidates but have no trade event,
    /// including intentionally ignored failed transactions.
    pub missing_trade_event_count: i64,
    /// Total persisted trade events.
    pub trade_event_count: i64,
    /// Explicit alias for decoded trade candidates without linked trade event.
    pub decoded_trade_candidate_without_trade_event_count: i64,
    /// Trade candidates without linked trade event on successful transactions.
    pub decoded_trade_candidate_without_trade_event_on_ok_transaction_count: i64,
    /// Trade candidates without linked trade event on failed transactions.
    pub decoded_trade_candidate_without_trade_event_on_failed_transaction_count: i64,
    /// Actionable missing trade events on successful transactions.
    pub actionable_missing_trade_event_count: i64,
    /// Ignored missing trade events caused by failed transactions.
    pub ignored_failed_transaction_trade_candidate_count: i64,
    /// Trade candidates without linked trade event and without explicit base/quote payload amounts.
    pub decoded_trade_candidate_without_amount_payload_count: i64,
    /// Total trade events with missing or invalid pricing fields.
    pub invalid_trade_event_count: i64,
    /// Total persisted pair candles.
    pub pair_candle_count: i64,
    /// Real duplicate trade rows grouped by decoded event id.
    pub duplicate_decoded_event_trade_count: i64,
    /// Multi-trade groups sharing the same signature and pair id.
    pub multi_trade_signature_pair_count: i64,
    /// Total duplicate candle buckets.
    pub duplicate_candle_bucket_count: i64,
    /// Total known tokens.
    pub token_count: i64,
    /// Total tokens missing symbol or name.
    pub token_metadata_missing_count: i64,
    /// Total tokens used by trade-materialized pairs that still miss symbol or name.
    pub tradable_token_metadata_missing_count: i64,
    /// Total quote-side tokens used by pairs that still miss symbol or name.
    pub quote_token_metadata_missing_count: i64,
    /// Total pairs whose display symbol is missing or still falls back to raw mints.
    pub pair_symbol_fallback_count: i64,
    /// Total pairs whose display symbol is present and does not include raw mints.
    pub pair_symbol_resolved_count: i64,
    /// Total pairs whose quote token is WSOL.
    pub wsol_quote_pair_count: i64,
    /// Total pairs whose quote token is a known stable quote.
    pub stable_quote_pair_count: i64,
    /// Total known pools.
    pub pool_count: i64,
    /// Total known pairs.
    pub pair_count: i64,
    /// Stable explanation for legacy pair gap counters.
    ///
    /// `pair_without_trade_count` and `pair_without_candle_count` are blocking
    /// actionable gap counters, not literal catalog-wide counters.
    pub pair_gap_counter_semantics: std::string::String,
    /// Total pairs without any persisted trade event, including catalog-only and partial DEX pairs.
    pub literal_pair_without_trade_count: i64,
    /// Total pairs without any persisted candle, including catalog-only and partial DEX pairs.
    pub literal_pair_without_candle_count: i64,
    /// Total pairs that have at least one persisted trade event.
    pub trade_materialized_pair_count: i64,
    /// Total pairs that have at least one persisted candle bucket.
    pub candle_materialized_pair_count: i64,
    /// Total pairs with at least one successful decoded trade candidate.
    pub actionable_pair_count: i64,
    /// Total distinct candle timeframes currently materialized.
    pub candle_bucket_timeframe_count: i64,
    /// Whether candle rows are bucketed aggregates rather than one row per trade.
    pub candles_are_bucketed: bool,
    /// Total pairs without trade event among actionable successful trade candidates.
    pub blocking_pair_without_trade_count: i64,
    /// Total pairs without candle among actionable successful candle candidates.
    pub blocking_pair_without_candle_count: i64,
    /// Total pairs without trade event.
    ///
    /// Legacy alias kept for compatibility. It has the same semantics as
    /// `blocking_pair_without_trade_count`.
    pub pair_without_trade_count: i64,
    /// Total pairs without candle.
    ///
    /// Legacy alias kept for compatibility. It has the same semantics as
    /// `blocking_pair_without_candle_count`.
    pub pair_without_candle_count: i64,
    /// Diagnostics grouped by DEX.
    pub dex_summaries: std::vec::Vec<crate::LocalDexDiagnosticSummaryDto>,
    /// Raydium surface diagnostics derived from the matrix and observed instructions.
    pub raydium_surface_summaries: std::vec::Vec<crate::LocalRaydiumSurfaceDiagnosticSummaryDto>,
    /// Diagnostics grouped by pair.
    pub pair_summaries: std::vec::Vec<crate::LocalPairDiagnosticSummaryDto>,
    /// Diagnostics grouped by pair materialization/actionability class.
    pub pair_actionability_summaries:
        std::vec::Vec<crate::LocalPairActionabilityDiagnosticSummaryDto>,
    /// Diagnostics grouped by pair trading readiness class.
    pub pair_trading_readiness_summaries:
        std::vec::Vec<crate::LocalPairTradingReadinessDiagnosticSummaryDto>,
    /// Diagnostics grouped by decoded event kind.
    pub decoded_event_summaries: std::vec::Vec<crate::LocalDecodedEventDiagnosticSummaryDto>,
    /// Diagnostics grouped by decoded event category, lifecycle kind and actionability.
    pub event_classification_summaries:
        std::vec::Vec<crate::LocalEventClassificationDiagnosticSummaryDto>,
    /// Missing trade events grouped by diagnostic reason.
    pub missing_trade_event_reason_summaries:
        std::vec::Vec<crate::LocalMissingTradeEventReasonSummaryDto>,
    /// Samples of launch-origin attributions.
    pub launch_origin_samples: std::vec::Vec<crate::LocalLaunchOriginDiagnosticSampleDto>,
    /// Samples of pool-origin rows and their optional launch linkage.
    pub pool_origin_samples: std::vec::Vec<crate::LocalPoolOriginDiagnosticSampleDto>,
    /// Prioritized samples of tokens whose display metadata is still incomplete.
    pub token_metadata_gap_samples: std::vec::Vec<crate::LocalTokenMetadataGapDiagnosticSampleDto>,
    /// Total pairs with only non-actionable missing trade events.
    pub non_actionable_pair_count: i64,
    /// Pair summaries for non-actionable missing trade events.
    pub non_actionable_pair_summaries:
        std::vec::Vec<crate::LocalNonActionablePairDiagnosticSummaryDto>,
    /// Samples of decoded trade candidates without linked trade event.
    pub missing_trade_event_samples:
        std::vec::Vec<crate::LocalMissingTradeEventDiagnosticSampleDto>,
    /// Samples of duplicated trade rows by decoded event id.
    pub duplicate_decoded_event_trade_samples:
        std::vec::Vec<crate::LocalDuplicateDecodedEventTradeDiagnosticSampleDto>,
    /// Samples of multi-trade signature/pair groups.
    pub multi_trade_signature_pair_samples:
        std::vec::Vec<crate::LocalMultiTradeSignaturePairDiagnosticSampleDto>,
    /// Samples of pairs without trade.
    pub pair_without_trade_samples: std::vec::Vec<crate::LocalPairGapDiagnosticSampleDto>,
    /// Samples of pairs without candle.
    pub pair_without_candle_samples: std::vec::Vec<crate::LocalPairGapDiagnosticSampleDto>,
}

/// Local DEX diagnostics summary.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalDexDiagnosticSummaryDto {
    /// DEX code or protocol name.
    pub dex_code: std::string::String,
    /// Total known pools for this DEX.
    pub pool_count: i64,
    /// Total known pairs for this DEX.
    pub pair_count: i64,
    /// Total decoded events for this DEX.
    pub decoded_event_count: i64,
    /// Total decoded trade candidates for this DEX.
    pub decoded_trade_candidate_count: i64,
    /// Total decoded candle candidates for this DEX.
    pub decoded_candle_candidate_count: i64,
    /// Total persisted trade events for this DEX.
    pub trade_event_count: i64,
    /// Total persisted candles for this DEX.
    pub pair_candle_count: i64,
}

/// Local diagnostics for one Raydium surface from the DEX-first matrix.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalRaydiumSurfaceDiagnosticSummaryDto {
    /// DEX matrix code, such as `raydium_clmm` or `raydium_amm_v4`.
    pub dex_code: std::string::String,
    /// Human readable surface name.
    pub display_name: std::string::String,
    /// DEX-first role attached to this surface.
    pub surface_role: std::string::String,
    /// Program id from the support matrix, when known.
    pub program_id: std::option::Option<std::string::String>,
    /// Program id verification status from the support matrix.
    pub program_id_status: std::string::String,
    /// Support status from the support matrix.
    pub status: std::string::String,
    /// Whether this surface is enabled in the local DEX catalog.
    pub catalog_enabled: bool,
    /// Total projected instructions for this program in the current database.
    pub instruction_count: i64,
    /// Total distinct projected transactions for this program in the current database.
    pub transaction_count: i64,
    /// Total decoded events for this DEX code.
    pub decoded_event_count: i64,
    /// Total materialized trade events for this DEX code.
    pub trade_event_count: i64,
    /// Total candle buckets for this DEX code.
    pub pair_candle_count: i64,
    /// Latest slot where this program was observed, when present.
    pub latest_slot: std::option::Option<i64>,
    /// Latest signature where this program was observed, when present.
    pub latest_signature: std::option::Option<std::string::String>,
    /// Whether projected instructions prove this program exists in the current corpus.
    pub observed_in_current_corpus: bool,
    /// Whether this DEX code has decoded events in the current corpus.
    pub decoded_in_current_corpus: bool,
    /// Whether this DEX code has materialized trades in the current corpus.
    pub trade_materialized_in_current_corpus: bool,
}

/// Projected instruction diagnostics grouped by Raydium program id.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalRaydiumProgramInstructionDiagnosticSummaryDto {
    /// Program id.
    pub program_id: std::string::String,
    /// Total projected instruction rows.
    pub instruction_count: i64,
    /// Total distinct transactions.
    pub transaction_count: i64,
    /// Latest slot where the program appears, when present.
    pub latest_slot: std::option::Option<i64>,
    /// Latest signature where the program appears, when present.
    pub latest_signature: std::option::Option<std::string::String>,
}

/// Local pair diagnostics summary.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalPairDiagnosticSummaryDto {
    /// Pair id.
    pub pair_id: i64,
    /// Pool address.
    pub pool_address: std::string::String,
    /// DEX code.
    pub dex_code: std::string::String,
    /// Base token mint.
    pub base_mint: std::string::String,
    /// Base token symbol.
    pub base_symbol: std::option::Option<std::string::String>,
    /// Quote token mint.
    pub quote_mint: std::string::String,
    /// Quote token symbol.
    pub quote_symbol: std::option::Option<std::string::String>,
    /// Pair symbol.
    pub pair_symbol: std::option::Option<std::string::String>,
    /// Total decoded events attached to the pool.
    pub decoded_event_count: i64,
    /// Total decoded trade candidates attached to the pool.
    pub decoded_trade_candidate_count: i64,
    /// Total decoded candle candidates attached to the pool.
    pub decoded_candle_candidate_count: i64,
    /// Total trade events attached to the pair.
    pub trade_event_count: i64,
    /// Total invalid trade events attached to the pair.
    pub invalid_trade_event_count: i64,
    /// Total candle buckets attached to the pair.
    pub pair_candle_count: i64,
    /// Last known price.
    pub last_price_quote_per_base: std::option::Option<f64>,
    /// Pair trading-readiness class derived from base/quote orientation.
    pub pair_trading_readiness: std::string::String,
    /// Quote asset class used by the readiness classifier.
    pub quote_asset_class: std::string::String,
    /// Whether the pair likely requires a router or aggregator before direct bot execution.
    pub trading_route_required: bool,
}

/// Local pair diagnostics grouped by materialization/actionability class.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalPairActionabilityDiagnosticSummaryDto {
    /// Pair actionability or materialization class.
    pub pair_actionability: std::string::String,
    /// Total pairs in this class.
    pub pair_count: i64,
    /// Total decoded events attached to pairs in this class.
    pub decoded_event_count: i64,
    /// Total decoded trade candidates attached to pairs in this class.
    pub decoded_trade_candidate_count: i64,
    /// Total decoded trade candidates on successful transactions attached to pairs in this class.
    pub actionable_trade_candidate_count: i64,
    /// Total decoded trade candidates on failed transactions attached to pairs in this class.
    pub failed_trade_candidate_count: i64,
    /// Total persisted trade events attached to pairs in this class.
    pub trade_event_count: i64,
    /// Total persisted candle buckets attached to pairs in this class.
    pub pair_candle_count: i64,
}

/// Local pair diagnostics grouped by trading readiness class.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalPairTradingReadinessDiagnosticSummaryDto {
    /// Pair trading-readiness class.
    pub pair_trading_readiness: std::string::String,
    /// Quote asset class attached to this readiness group.
    pub quote_asset_class: std::string::String,
    /// Whether the group requires a router or aggregator before direct execution.
    pub trading_route_required: bool,
    /// Total pairs in this readiness group.
    pub pair_count: i64,
    /// Total decoded events attached to pairs in this readiness group.
    pub decoded_event_count: i64,
    /// Total decoded trade candidates attached to pairs in this readiness group.
    pub decoded_trade_candidate_count: i64,
    /// Total persisted trade events attached to pairs in this readiness group.
    pub trade_event_count: i64,
    /// Total persisted candle buckets attached to pairs in this readiness group.
    pub pair_candle_count: i64,
}

/// Local decoded-event diagnostics summary.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalDecodedEventDiagnosticSummaryDto {
    /// Protocol name.
    pub protocol_name: std::string::String,
    /// Event kind.
    pub event_kind: std::string::String,
    /// Event category.
    pub event_category: std::option::Option<std::string::String>,
    /// Event lifecycle kind.
    pub event_lifecycle_kind: std::option::Option<std::string::String>,
    /// Event actionability class.
    pub event_actionability: std::option::Option<std::string::String>,
    /// Whether payload says this event is a useful non-trade event.
    pub non_trade_useful: std::option::Option<bool>,
    /// Whether payload says this event is a trade candidate.
    pub trade_candidate: std::option::Option<bool>,
    /// Whether payload says this event is a candle candidate.
    pub candle_candidate: std::option::Option<bool>,
    /// Event count.
    pub event_count: i64,
    /// Trade-event count linked to these decoded events.
    pub trade_event_count: i64,
}

/// Local decoded-event classification summary.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalEventClassificationDiagnosticSummaryDto {
    /// Event category.
    pub event_category: std::string::String,
    /// Event lifecycle kind.
    pub event_lifecycle_kind: std::string::String,
    /// Event actionability class.
    pub event_actionability: std::string::String,
    /// Whether payload says this event is a useful non-trade event.
    pub non_trade_useful: bool,
    /// Total decoded events in this classification group.
    pub event_count: i64,
    /// Total decoded trade candidates in this classification group.
    pub decoded_trade_candidate_count: i64,
    /// Total decoded candle candidates in this classification group.
    pub decoded_candle_candidate_count: i64,
    /// Total linked trade events in this classification group.
    pub trade_event_count: i64,
}

/// Missing trade event diagnostics grouped by reason.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalMissingTradeEventReasonSummaryDto {
    /// Diagnostic reason.
    pub reason: std::string::String,
    /// Whether grouped source transactions failed.
    pub transaction_failed: bool,
    /// Whether grouped missing trade events are actionable.
    pub actionable: bool,
    /// Total missing trade events in this group.
    pub event_count: i64,
    /// Total events in this group with an explicit base amount payload.
    pub has_base_amount_payload_count: i64,
    /// Total events in this group with an explicit quote amount payload.
    pub has_quote_amount_payload_count: i64,
    /// Total events in this group with an explicit price payload.
    pub has_price_payload_count: i64,
}

/// Local pair diagnostics for pairs whose missing trade events are non-actionable.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalNonActionablePairDiagnosticSummaryDto {
    /// Pair id.
    pub pair_id: i64,
    /// Pool address.
    pub pool_address: std::string::String,
    /// DEX code.
    pub dex_code: std::string::String,
    /// Base token mint.
    pub base_mint: std::string::String,
    /// Base token symbol.
    pub base_symbol: std::option::Option<std::string::String>,
    /// Quote token mint.
    pub quote_mint: std::string::String,
    /// Quote token symbol.
    pub quote_symbol: std::option::Option<std::string::String>,
    /// Pair symbol.
    pub pair_symbol: std::option::Option<std::string::String>,
    /// Total decoded trade candidates attached to the pool.
    pub decoded_trade_candidate_count: i64,
    /// Total non-actionable missing trade events attached to the pair.
    pub non_actionable_missing_trade_event_count: i64,
    /// Total missing trade candidates caused by failed transactions.
    pub failed_transaction_candidate_count: i64,
    /// Total successful missing trade candidates without token mints.
    pub ok_transaction_without_token_mints_count: i64,
    /// Total successful missing trade candidates without amount payload.
    pub ok_transaction_without_amount_payload_count: i64,
    /// Distinct non-actionable diagnostic reasons seen for this pair.
    pub reason_summary: std::option::Option<std::string::String>,
    /// Total trade events attached to the pair.
    pub trade_event_count: i64,
    /// Total candle buckets attached to the pair.
    pub pair_candle_count: i64,
}

/// Internal flat counter row for local diagnostics.
#[derive(Debug, Clone)]
pub struct LocalPipelineDiagnosticCountersDto {
    /// Total persisted chain transactions.
    pub transaction_count: i64,
    /// Total successful chain transactions.
    pub ok_transaction_count: i64,
    /// Total failed chain transactions.
    pub failed_transaction_count: i64,
    /// Total decoded DEX events.
    pub decoded_event_count: i64,
    /// Total decoded DEX trade candidates.
    pub decoded_trade_candidate_count: i64,
    /// Total decoded DEX candle candidates.
    pub decoded_candle_candidate_count: i64,
    /// Total decoded useful non-trade events.
    pub decoded_non_trade_useful_event_count: i64,
    /// Total decoded swap-like events that are intentionally non-actionable.
    pub decoded_non_actionable_trade_event_count: i64,
    /// Total decoded events with unknown classification.
    pub decoded_unknown_event_count: i64,
    /// Total persisted liquidity events.
    pub liquidity_event_count: i64,
    /// Total persisted pool lifecycle events.
    pub pool_lifecycle_event_count: i64,
    /// Total persisted fee events.
    pub fee_event_count: i64,
    /// Total persisted reward events.
    pub reward_event_count: i64,
    /// Total persisted pool administration events.
    pub pool_admin_event_count: i64,
    /// Total decoded trade candidates without trade event, including ignored failed transactions.
    pub missing_trade_event_count: i64,
    /// Explicit alias for decoded trade candidates without linked trade event.
    pub decoded_trade_candidate_without_trade_event_count: i64,
    /// Trade candidates without linked trade event on successful transactions.
    pub decoded_trade_candidate_without_trade_event_on_ok_transaction_count: i64,
    /// Trade candidates without linked trade event on failed transactions.
    pub decoded_trade_candidate_without_trade_event_on_failed_transaction_count: i64,
    /// Actionable missing trade events on successful transactions.
    pub actionable_missing_trade_event_count: i64,
    /// Ignored missing trade events caused by failed transactions.
    pub ignored_failed_transaction_trade_candidate_count: i64,
    /// Trade candidates without linked trade event and without explicit base/quote payload amounts.
    pub decoded_trade_candidate_without_amount_payload_count: i64,
    /// Total persisted trade events.
    pub trade_event_count: i64,
    /// Total invalid trade events.
    pub invalid_trade_event_count: i64,
    /// Total persisted candles.
    pub pair_candle_count: i64,
    /// Real duplicate trade rows grouped by decoded event id.
    pub duplicate_decoded_event_trade_count: i64,
    /// Multi-trade groups sharing the same signature and pair id.
    pub multi_trade_signature_pair_count: i64,
    /// Total duplicate candle groups.
    pub duplicate_candle_bucket_count: i64,
    /// Total known tokens.
    pub token_count: i64,
    /// Total tokens missing metadata.
    pub token_metadata_missing_count: i64,
    /// Total tokens used by trade-materialized pairs that still miss metadata.
    pub tradable_token_metadata_missing_count: i64,
    /// Total quote-side tokens used by pairs that still miss metadata.
    pub quote_token_metadata_missing_count: i64,
    /// Total pairs whose display symbol is missing or falls back to raw mints.
    pub pair_symbol_fallback_count: i64,
    /// Total pairs whose display symbol is resolved without raw mints.
    pub pair_symbol_resolved_count: i64,
    /// Total pairs whose quote token is WSOL.
    pub wsol_quote_pair_count: i64,
    /// Total pairs whose quote token is a known stable quote.
    pub stable_quote_pair_count: i64,
    /// Total known pools.
    pub pool_count: i64,
    /// Total known pairs.
    pub pair_count: i64,
    /// Total pairs without any persisted trade event, including catalog-only and partial DEX pairs.
    pub literal_pair_without_trade_count: i64,
    /// Total pairs without any persisted candle, including catalog-only and partial DEX pairs.
    pub literal_pair_without_candle_count: i64,
    /// Total pairs that have at least one persisted trade event.
    pub trade_materialized_pair_count: i64,
    /// Total pairs that have at least one persisted candle bucket.
    pub candle_materialized_pair_count: i64,
    /// Total pairs with at least one successful decoded trade candidate.
    pub actionable_pair_count: i64,
    /// Total distinct candle timeframes currently materialized.
    pub candle_bucket_timeframe_count: i64,
    /// Total pairs with only non-actionable missing trade events.
    pub non_actionable_pair_count: i64,
    /// Total pairs without trade among actionable successful trade candidates.
    pub blocking_pair_without_trade_count: i64,
    /// Total pairs without candle among actionable successful candle candidates.
    pub blocking_pair_without_candle_count: i64,
    /// Total pairs without trade.
    pub pair_without_trade_count: i64,
    /// Total pairs without candle.
    pub pair_without_candle_count: i64,
}

/// SQL row for global local pipeline diagnostic counters.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalPipelineDiagnosticCountersRow {
    pub(crate) transaction_count: i64,
    pub(crate) ok_transaction_count: i64,
    pub(crate) failed_transaction_count: i64,
    pub(crate) decoded_event_count: i64,
    pub(crate) decoded_trade_candidate_count: i64,
    pub(crate) decoded_candle_candidate_count: i64,
    pub(crate) decoded_non_trade_useful_event_count: i64,
    pub(crate) decoded_non_actionable_trade_event_count: i64,
    pub(crate) decoded_unknown_event_count: i64,
    pub(crate) liquidity_event_count: i64,
    pub(crate) pool_lifecycle_event_count: i64,
    pub(crate) fee_event_count: i64,
    pub(crate) reward_event_count: i64,
    pub(crate) pool_admin_event_count: i64,
    pub(crate) missing_trade_event_count: i64,
    pub(crate) decoded_trade_candidate_without_trade_event_count: i64,
    pub(crate) decoded_trade_candidate_without_trade_event_on_ok_transaction_count: i64,
    pub(crate) decoded_trade_candidate_without_trade_event_on_failed_transaction_count: i64,
    pub(crate) actionable_missing_trade_event_count: i64,
    pub(crate) ignored_failed_transaction_trade_candidate_count: i64,
    pub(crate) decoded_trade_candidate_without_amount_payload_count: i64,
    pub(crate) trade_event_count: i64,
    pub(crate) invalid_trade_event_count: i64,
    pub(crate) pair_candle_count: i64,
    pub(crate) duplicate_decoded_event_trade_count: i64,
    pub(crate) multi_trade_signature_pair_count: i64,
    pub(crate) duplicate_candle_bucket_count: i64,
    pub(crate) token_count: i64,
    pub(crate) token_metadata_missing_count: i64,
    pub(crate) tradable_token_metadata_missing_count: i64,
    pub(crate) quote_token_metadata_missing_count: i64,
    pub(crate) pair_symbol_fallback_count: i64,
    pub(crate) pair_symbol_resolved_count: i64,
    pub(crate) wsol_quote_pair_count: i64,
    pub(crate) stable_quote_pair_count: i64,
    pub(crate) pool_count: i64,
    pub(crate) pair_count: i64,
    pub(crate) literal_pair_without_trade_count: i64,
    pub(crate) literal_pair_without_candle_count: i64,
    pub(crate) trade_materialized_pair_count: i64,
    pub(crate) candle_materialized_pair_count: i64,
    pub(crate) actionable_pair_count: i64,
    pub(crate) candle_bucket_timeframe_count: i64,
    pub(crate) non_actionable_pair_count: i64,
    pub(crate) blocking_pair_without_trade_count: i64,
    pub(crate) blocking_pair_without_candle_count: i64,
    pub(crate) pair_without_trade_count: i64,
    pub(crate) pair_without_candle_count: i64,
}

/// SQL row for local DEX diagnostics.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalDexDiagnosticSummaryRow {
    pub(crate) dex_code: std::string::String,
    pub(crate) pool_count: i64,
    pub(crate) pair_count: i64,
    pub(crate) decoded_event_count: i64,
    pub(crate) decoded_trade_candidate_count: i64,
    pub(crate) decoded_candle_candidate_count: i64,
    pub(crate) trade_event_count: i64,
    pub(crate) pair_candle_count: i64,
}

/// SQL row for Raydium program instruction diagnostics.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalRaydiumProgramInstructionDiagnosticSummaryRow {
    pub(crate) program_id: std::string::String,
    pub(crate) instruction_count: i64,
    pub(crate) transaction_count: i64,
    pub(crate) latest_slot: std::option::Option<i64>,
    pub(crate) latest_signature: std::option::Option<std::string::String>,
}

/// SQL row for local pair diagnostics.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalPairDiagnosticSummaryRow {
    pub(crate) pair_id: i64,
    pub(crate) pool_address: std::string::String,
    pub(crate) dex_code: std::string::String,
    pub(crate) base_mint: std::string::String,
    pub(crate) base_symbol: std::option::Option<std::string::String>,
    pub(crate) quote_mint: std::string::String,
    pub(crate) quote_symbol: std::option::Option<std::string::String>,
    pub(crate) pair_symbol: std::option::Option<std::string::String>,
    pub(crate) decoded_event_count: i64,
    pub(crate) decoded_trade_candidate_count: i64,
    pub(crate) decoded_candle_candidate_count: i64,
    pub(crate) trade_event_count: i64,
    pub(crate) invalid_trade_event_count: i64,
    pub(crate) pair_candle_count: i64,
    pub(crate) last_price_quote_per_base: std::option::Option<f64>,
    pub(crate) pair_trading_readiness: std::string::String,
    pub(crate) quote_asset_class: std::string::String,
    pub(crate) trading_route_required: i64,
}

/// SQL row for local pair actionability diagnostics.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalPairActionabilityDiagnosticSummaryRow {
    pub(crate) pair_actionability: std::string::String,
    pub(crate) pair_count: i64,
    pub(crate) decoded_event_count: i64,
    pub(crate) decoded_trade_candidate_count: i64,
    pub(crate) actionable_trade_candidate_count: i64,
    pub(crate) failed_trade_candidate_count: i64,
    pub(crate) trade_event_count: i64,
    pub(crate) pair_candle_count: i64,
}

/// SQL row for local pair trading-readiness diagnostics.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalPairTradingReadinessDiagnosticSummaryRow {
    pub(crate) pair_trading_readiness: std::string::String,
    pub(crate) quote_asset_class: std::string::String,
    pub(crate) trading_route_required: i64,
    pub(crate) pair_count: i64,
    pub(crate) decoded_event_count: i64,
    pub(crate) decoded_trade_candidate_count: i64,
    pub(crate) trade_event_count: i64,
    pub(crate) pair_candle_count: i64,
}

/// SQL row for local decoded-event diagnostics.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalDecodedEventDiagnosticSummaryRow {
    pub(crate) protocol_name: std::string::String,
    pub(crate) event_kind: std::string::String,
    pub(crate) event_category: std::option::Option<std::string::String>,
    pub(crate) event_lifecycle_kind: std::option::Option<std::string::String>,
    pub(crate) event_actionability: std::option::Option<std::string::String>,
    pub(crate) non_trade_useful: std::option::Option<i64>,
    pub(crate) trade_candidate: std::option::Option<i64>,
    pub(crate) candle_candidate: std::option::Option<i64>,
    pub(crate) event_count: i64,
    pub(crate) trade_event_count: i64,
}

/// SQL row for local decoded-event classification diagnostics.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalEventClassificationDiagnosticSummaryRow {
    pub(crate) event_category: std::string::String,
    pub(crate) event_lifecycle_kind: std::string::String,
    pub(crate) event_actionability: std::string::String,
    pub(crate) non_trade_useful: i64,
    pub(crate) event_count: i64,
    pub(crate) decoded_trade_candidate_count: i64,
    pub(crate) decoded_candle_candidate_count: i64,
    pub(crate) trade_event_count: i64,
}

/// Sample of a decoded trade candidate without linked trade event.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalMissingTradeEventDiagnosticSampleDto {
    /// Decoded event id.
    pub decoded_event_id: i64,
    /// Chain transaction id.
    pub transaction_id: std::option::Option<i64>,
    /// Transaction signature.
    pub signature: std::option::Option<std::string::String>,
    /// Protocol name.
    pub protocol_name: std::string::String,
    /// Event kind.
    pub event_kind: std::string::String,
    /// Pool account.
    pub pool_account: std::option::Option<std::string::String>,
    /// Whether the source transaction failed.
    pub transaction_failed: bool,
    /// Whether this missing trade event is actionable for validation.
    ///
    /// A missing trade event is actionable only when the source transaction
    /// succeeded, the decoded event has a pool account and both token mints,
    /// and the pool is already attached to a known pair. Failed transactions
    /// and incomplete decoded hints remain visible as samples, but they should
    /// not block the non-regression profile.
    pub actionable: bool,
    /// Diagnostic reason explaining why no trade event was linked.
    pub reason: std::string::String,
    /// Whether payload has an explicit base amount.
    pub has_base_amount_payload: bool,
    /// Whether payload has an explicit quote amount.
    pub has_quote_amount_payload: bool,
    /// Whether payload has an explicit price.
    pub has_price_payload: bool,
}

/// Sample of duplicated trade rows grouped by decoded event id.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalDuplicateDecodedEventTradeDiagnosticSampleDto {
    /// Decoded event id.
    pub decoded_event_id: i64,
    /// Protocol name.
    pub protocol_name: std::option::Option<std::string::String>,
    /// Event kind.
    pub event_kind: std::option::Option<std::string::String>,
    /// Pool account.
    pub pool_account: std::option::Option<std::string::String>,
    /// Duplicate trade row count.
    pub trade_event_count: i64,
    /// Trade event ids.
    pub trade_event_ids: std::option::Option<std::string::String>,
    /// Signatures.
    pub signatures: std::option::Option<std::string::String>,
}

/// Sample of multi-trade groups sharing the same signature and pair id.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalMultiTradeSignaturePairDiagnosticSampleDto {
    /// Transaction signature.
    pub signature: std::string::String,
    /// Pair id.
    pub pair_id: i64,
    /// Pool address.
    pub pool_address: std::option::Option<std::string::String>,
    /// DEX code.
    pub dex_code: std::option::Option<std::string::String>,
    /// Trade event count.
    pub trade_event_count: i64,
    /// Distinct decoded event count.
    pub decoded_event_count: i64,
    /// Trade event ids.
    pub trade_event_ids: std::option::Option<std::string::String>,
    /// Decoded event ids.
    pub decoded_event_ids: std::option::Option<std::string::String>,
}

/// Sample of a pair gap.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalPairGapDiagnosticSampleDto {
    /// Pair id.
    pub pair_id: i64,
    /// Pool address.
    pub pool_address: std::string::String,
    /// DEX code.
    pub dex_code: std::string::String,
    /// Base mint.
    pub base_mint: std::string::String,
    /// Base symbol.
    pub base_symbol: std::option::Option<std::string::String>,
    /// Quote mint.
    pub quote_mint: std::string::String,
    /// Quote symbol.
    pub quote_symbol: std::option::Option<std::string::String>,
    /// Pair symbol.
    pub pair_symbol: std::option::Option<std::string::String>,
    /// Decoded event count.
    pub decoded_event_count: i64,
    /// Decoded trade candidate count.
    pub decoded_trade_candidate_count: i64,
    /// Trade event count.
    pub trade_event_count: i64,
    /// Pair candle count.
    pub pair_candle_count: i64,
}

/// Sample of a launch-origin attribution.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalLaunchOriginDiagnosticSampleDto {
    /// Launch attribution id.
    pub launch_attribution_id: i64,
    /// Launch surface code.
    pub launch_surface_code: std::string::String,
    /// Launch surface display name.
    pub launch_surface_name: std::string::String,
    /// Transaction signature.
    pub transaction_signature: std::string::String,
    /// Decoded event id.
    pub decoded_event_id: i64,
    /// Protocol that materialized the decoded event.
    pub protocol_name: std::string::String,
    /// Match kind used to attribute the launch surface.
    pub match_kind: std::string::String,
    /// Matched key value used to attribute the launch surface.
    pub matched_value: std::string::String,
    /// Optional related pool id.
    pub pool_id: std::option::Option<i64>,
    /// Optional related pool address.
    pub pool_address: std::option::Option<std::string::String>,
    /// Optional related pair id.
    pub pair_id: std::option::Option<i64>,
    /// Optional related pair symbol.
    pub pair_symbol: std::option::Option<std::string::String>,
}

/// Sample of a pool-origin row.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalPoolOriginDiagnosticSampleDto {
    /// Pool-origin id.
    pub pool_origin_id: i64,
    /// Effective DEX code attached to the pool.
    pub dex_code: std::string::String,
    /// Pool id.
    pub pool_id: i64,
    /// Pool address.
    pub pool_address: std::string::String,
    /// Optional pair id.
    pub pair_id: std::option::Option<i64>,
    /// Optional pair symbol.
    pub pair_symbol: std::option::Option<std::string::String>,
    /// Optional launch surface code linked through launch attribution.
    pub launch_surface_code: std::option::Option<std::string::String>,
    /// Founding transaction signature.
    pub founding_signature: std::string::String,
    /// Founding effective DEX/protocol name.
    pub founding_protocol_name: std::string::String,
    /// Founding decoded event kind.
    pub founding_event_kind: std::string::String,
}

/// Prioritized sample of an incomplete token metadata row.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LocalTokenMetadataGapDiagnosticSampleDto {
    /// Token id.
    pub token_id: i64,
    /// Mint address.
    pub mint: std::string::String,
    /// Current symbol, when present.
    pub symbol: std::option::Option<std::string::String>,
    /// Current name, when present.
    pub name: std::option::Option<std::string::String>,
    /// Current mint decimals, when known.
    pub decimals: std::option::Option<i64>,
    /// Token program id.
    pub token_program: std::string::String,
    /// Whether this token row is flagged as quote token.
    pub is_quote_token: bool,
    /// Whether the token appears in at least one pair with materialized trades.
    pub used_by_trade_materialized_pair: bool,
    /// Whether the token appears on the quote side of at least one pair.
    pub used_as_quote_token: bool,
    /// Number of trade-materialized pairs using this token.
    pub trade_materialized_pair_count: i64,
    /// Number of catalog pairs using this token.
    pub total_pair_count: i64,
    /// Human-readable prioritization bucket.
    pub priority: std::string::String,
}

/// SQL row for missing trade event reason summaries.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalMissingTradeEventReasonSummaryRow {
    pub(crate) reason: std::string::String,
    pub(crate) transaction_failed: i64,
    pub(crate) actionable: i64,
    pub(crate) event_count: i64,
    pub(crate) has_base_amount_payload_count: i64,
    pub(crate) has_quote_amount_payload_count: i64,
    pub(crate) has_price_payload_count: i64,
}

/// SQL row for non-actionable pair diagnostic summaries.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalNonActionablePairDiagnosticSummaryRow {
    pub(crate) pair_id: i64,
    pub(crate) pool_address: std::string::String,
    pub(crate) dex_code: std::string::String,
    pub(crate) base_mint: std::string::String,
    pub(crate) base_symbol: std::option::Option<std::string::String>,
    pub(crate) quote_mint: std::string::String,
    pub(crate) quote_symbol: std::option::Option<std::string::String>,
    pub(crate) pair_symbol: std::option::Option<std::string::String>,
    pub(crate) decoded_trade_candidate_count: i64,
    pub(crate) non_actionable_missing_trade_event_count: i64,
    pub(crate) failed_transaction_candidate_count: i64,
    pub(crate) ok_transaction_without_token_mints_count: i64,
    pub(crate) ok_transaction_without_amount_payload_count: i64,
    pub(crate) reason_summary: std::option::Option<std::string::String>,
    pub(crate) trade_event_count: i64,
    pub(crate) pair_candle_count: i64,
}

/// SQL row for missing trade event samples.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalMissingTradeEventDiagnosticSampleRow {
    pub(crate) decoded_event_id: i64,
    pub(crate) transaction_id: std::option::Option<i64>,
    pub(crate) signature: std::option::Option<std::string::String>,
    pub(crate) protocol_name: std::string::String,
    pub(crate) event_kind: std::string::String,
    pub(crate) pool_account: std::option::Option<std::string::String>,
    pub(crate) transaction_failed: i64,
    pub(crate) actionable: i64,
    pub(crate) reason: std::string::String,
    pub(crate) has_base_amount_payload: i64,
    pub(crate) has_quote_amount_payload: i64,
    pub(crate) has_price_payload: i64,
}

/// SQL row for duplicated decoded event trade samples.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalDuplicateDecodedEventTradeDiagnosticSampleRow {
    pub(crate) decoded_event_id: i64,
    pub(crate) protocol_name: std::option::Option<std::string::String>,
    pub(crate) event_kind: std::option::Option<std::string::String>,
    pub(crate) pool_account: std::option::Option<std::string::String>,
    pub(crate) trade_event_count: i64,
    pub(crate) trade_event_ids: std::option::Option<std::string::String>,
    pub(crate) signatures: std::option::Option<std::string::String>,
}

/// SQL row for multi-trade signature/pair samples.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalMultiTradeSignaturePairDiagnosticSampleRow {
    pub(crate) signature: std::string::String,
    pub(crate) pair_id: i64,
    pub(crate) pool_address: std::option::Option<std::string::String>,
    pub(crate) dex_code: std::option::Option<std::string::String>,
    pub(crate) trade_event_count: i64,
    pub(crate) decoded_event_count: i64,
    pub(crate) trade_event_ids: std::option::Option<std::string::String>,
    pub(crate) decoded_event_ids: std::option::Option<std::string::String>,
}

/// SQL row for pair gap samples.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalPairGapDiagnosticSampleRow {
    pub(crate) pair_id: i64,
    pub(crate) pool_address: std::string::String,
    pub(crate) dex_code: std::string::String,
    pub(crate) base_mint: std::string::String,
    pub(crate) base_symbol: std::option::Option<std::string::String>,
    pub(crate) quote_mint: std::string::String,
    pub(crate) quote_symbol: std::option::Option<std::string::String>,
    pub(crate) pair_symbol: std::option::Option<std::string::String>,
    pub(crate) decoded_event_count: i64,
    pub(crate) decoded_trade_candidate_count: i64,
    pub(crate) trade_event_count: i64,
    pub(crate) pair_candle_count: i64,
}

/// SQL row for launch-origin diagnostic samples.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalLaunchOriginDiagnosticSampleRow {
    pub(crate) launch_attribution_id: i64,
    pub(crate) launch_surface_code: std::string::String,
    pub(crate) launch_surface_name: std::string::String,
    pub(crate) transaction_signature: std::string::String,
    pub(crate) decoded_event_id: i64,
    pub(crate) protocol_name: std::string::String,
    pub(crate) match_kind: std::string::String,
    pub(crate) matched_value: std::string::String,
    pub(crate) pool_id: std::option::Option<i64>,
    pub(crate) pool_address: std::option::Option<std::string::String>,
    pub(crate) pair_id: std::option::Option<i64>,
    pub(crate) pair_symbol: std::option::Option<std::string::String>,
}

/// SQL row for pool-origin diagnostic samples.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalPoolOriginDiagnosticSampleRow {
    pub(crate) pool_origin_id: i64,
    pub(crate) dex_code: std::string::String,
    pub(crate) pool_id: i64,
    pub(crate) pool_address: std::string::String,
    pub(crate) pair_id: std::option::Option<i64>,
    pub(crate) pair_symbol: std::option::Option<std::string::String>,
    pub(crate) launch_surface_code: std::option::Option<std::string::String>,
    pub(crate) founding_signature: std::string::String,
    pub(crate) founding_protocol_name: std::string::String,
    pub(crate) founding_event_kind: std::string::String,
}

/// SQL row for incomplete token metadata samples.
#[derive(Debug, Clone, sqlx::FromRow)]
pub(crate) struct LocalTokenMetadataGapDiagnosticSampleRow {
    pub(crate) token_id: i64,
    pub(crate) mint: std::string::String,
    pub(crate) symbol: std::option::Option<std::string::String>,
    pub(crate) name: std::option::Option<std::string::String>,
    pub(crate) decimals: std::option::Option<i64>,
    pub(crate) token_program: std::string::String,
    pub(crate) is_quote_token: i64,
    pub(crate) used_by_trade_materialized_pair: i64,
    pub(crate) used_as_quote_token: i64,
    pub(crate) trade_materialized_pair_count: i64,
    pub(crate) total_pair_count: i64,
    pub(crate) priority: std::string::String,
}
