//! Integration tests for M7P4 Operational Visibility — metrics feature flag.
//!
//! - Task 07: Metrics feature flag (zero-overhead base types)
#![allow(clippy::too_many_lines, clippy::unwrap_used)]

use std::time::Duration;

use tidaldb::{
    TidalDb,
    schema::{DecaySpec, EntityKind, Window},
};

// ── Shared test schema ───────────────────────────────────────────────────────

fn build_test_schema() -> tidaldb::schema::Schema {
    use tidaldb::{AgentPolicy, schema::SchemaBuilder};

    let mut builder = SchemaBuilder::new();
    let _ = builder
        .signal(
            "view",
            EntityKind::Item,
            DecaySpec::Exponential {
                half_life: Duration::from_secs(7 * 24 * 3600),
            },
        )
        .windows(&[Window::OneHour])
        .add();
    let _ = builder
        .signal(
            "like",
            EntityKind::Item,
            DecaySpec::Exponential {
                half_life: Duration::from_secs(30 * 24 * 3600),
            },
        )
        .windows(&[Window::OneHour])
        .add();
    builder.session_policy(
        "default",
        AgentPolicy {
            allowed_signals: vec!["view".to_string(), "like".to_string()],
            denied_signals: vec![],
            max_session_duration: Duration::from_secs(3600),
            max_signals_per_session: 1000,
        },
    );
    builder.build().unwrap()
}

// ── Task 07: Feature Flag ────────────────────────────────────────────────────

/// `QueryStats` is always available regardless of the `metrics` feature flag.
/// This test intentionally has NO `#[cfg(feature = "metrics")]` annotation.
#[test]
fn query_stats_available_without_metrics_feature() {
    use tidaldb::query::stats::QueryStats;

    let stats = QueryStats::new("my_profile".to_owned());
    assert_eq!(stats.profile_name, "my_profile");
    assert_eq!(stats.total_time_us, 0);
    assert_eq!(stats.candidates_considered, 0);
    assert_eq!(stats.filters_applied, 0);
    assert_eq!(stats.degradation_level, 0);
}

/// `MetricsState` base fields (`uptime_seconds`, `health_ok_value`) work
/// unconditionally — they are NOT gated behind the `metrics` feature.
#[test]
fn metrics_state_base_fields_always_available() {
    let schema = build_test_schema();
    let db = TidalDb::builder()
        .ephemeral()
        .with_schema(schema)
        .open()
        .unwrap();

    // These methods exist and compile even with --no-default-features.
    assert!(db.metrics().uptime_seconds() >= 0.0);
    assert!((db.metrics().health_ok_value() - 1.0).abs() < f64::EPSILON);
}
