// Performance budget contract for final server-to-RCT observations.
//
// Scope: model the accepted server-origin RCT result envelope as deterministic
// thresholds used by lab summaries.
// Targets: scripts/manual/run_server_to_rc_mode_matrix.sh and
// client/src/sync_probe/analyze/report.rs.
// Why: sync and freshness budgets are product commitments, not just prose from
// one successful run, and future tuning should ratchet them from here.

const MATRIX_SCRIPT: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/manual/run_server_to_rc_mode_matrix.sh"
));
const SYNC_REPORT: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/sync_probe/analyze/report.rs"
));

#[derive(Clone, Copy)]
struct ServerRctObservation {
    p95_skew_ms: f64,
    freshness_p95_ms: f64,
    coded_pairs: u32,
    expected_pairs: u32,
    smoothness_warnings: u32,
}

fn representative_good_server_rct_observations() -> Vec<ServerRctObservation> {
    vec![
        ServerRctObservation {
            p95_skew_ms: 18.1,
            freshness_p95_ms: 229.9,
            coded_pairs: 16,
            expected_pairs: 16,
            smoothness_warnings: 1,
        },
        ServerRctObservation {
            p95_skew_ms: 20.1,
            freshness_p95_ms: 284.0,
            coded_pairs: 16,
            expected_pairs: 16,
            smoothness_warnings: 2,
        },
        ServerRctObservation {
            p95_skew_ms: 7.3,
            freshness_p95_ms: 277.8,
            coded_pairs: 16,
            expected_pairs: 16,
            smoothness_warnings: 0,
        },
        ServerRctObservation {
            p95_skew_ms: 4.1,
            freshness_p95_ms: 200.4,
            coded_pairs: 16,
            expected_pairs: 16,
            smoothness_warnings: 1,
        },
    ]
}

#[test]
fn server_rct_budget_thresholds_remain_preferred_when_possible_and_acceptable_when_needed() {
    assert!(SYNC_REPORT.contains("VERDICT_PREFERRED_P95_ABS_SKEW_MS"));
    assert!(SYNC_REPORT.contains("VERDICT_ACCEPTABLE_P95_ABS_SKEW_MS"));
    assert!(MATRIX_SCRIPT.contains("LESAVKA_SERVER_RC_FRESHNESS_MAX_AGE_MS"));

    for observation in representative_good_server_rct_observations() {
        assert!(
            observation.p95_skew_ms < 35.0,
            "preferred server-to-RCT sync should stay under 35ms where possible"
        );
        assert!(
            observation.p95_skew_ms < 80.0,
            "acceptable server-to-RCT sync must stay under 80ms"
        );
        assert!(
            observation.freshness_p95_ms < 350.0,
            "server-origin RCT freshness must stay under 350ms"
        );
    }
}

#[test]
fn server_rct_budget_tracks_coded_completeness_and_smoothness_warnings() {
    for observation in representative_good_server_rct_observations() {
        assert_eq!(
            observation.coded_pairs, observation.expected_pairs,
            "server-origin probe should target 16/16 coded event completeness"
        );
        assert!(
            observation.smoothness_warnings <= 2,
            "smoothness warnings stay tracked and bounded, not ignored"
        );
    }
}
