#[cfg(test)]
mod router_tests {
    use crate::router::{RouterBrain, Complexity};

    fn make_request(model: &str, messages: Vec<serde_json::Value>) -> serde_json::Value {
        serde_json::json!({ "model": model, "messages": messages })
    }

    fn simple_msg(role: &str, content: &str) -> serde_json::Value {
        serde_json::json!({"role": role, "content": content})
    }

    // --- Complexity Classification ---

    #[test]
    fn classify_single_turn_extraction_as_low() {
        let brain = RouterBrain::new();
        let req = make_request("gpt-4o", vec![
            simple_msg("user", "What is 2+2?"),
        ]);
        assert_eq!(brain.classify_complexity(&req), Complexity::Low);
    }

    #[test]
    fn classify_short_system_prompt_as_low() {
        let brain = RouterBrain::new();
        let req = make_request("gpt-4o", vec![
            simple_msg("system", "Extract names from text"),
            simple_msg("user", "My name is Alice"),
        ]);
        assert_eq!(brain.classify_complexity(&req), Complexity::Low);
    }

    #[test]
    fn classify_multi_turn_conversation_as_high() {
        let brain = RouterBrain::new();
        let mut msgs = vec![];
        for i in 0..12 {
            msgs.push(simple_msg("user", &format!("Question {}", i)));
            msgs.push(simple_msg("assistant", &format!("Answer {}", i)));
        }
        let req = make_request("gpt-4o", msgs);
        assert_eq!(brain.classify_complexity(&req), Complexity::High);
    }

    #[test]
    fn classify_reasoning_keywords_as_high() {
        let brain = RouterBrain::new();
        let req = make_request("gpt-4o", vec![
            simple_msg("system", "Analyze and compare these two architectural approaches"),
            simple_msg("user", "Option A vs Option B"),
        ]);
        assert_eq!(brain.classify_complexity(&req), Complexity::High);
    }

    #[test]
    fn classify_medium_system_prompt_as_medium() {
        let brain = RouterBrain::new();
        let long_prompt = "You are a helpful assistant. ".repeat(30); // >500 chars
        let req = make_request("gpt-4o", vec![
            simple_msg("system", &long_prompt),
            simple_msg("user", "Hello"),
        ]);
        assert_eq!(brain.classify_complexity(&req), Complexity::Medium);
    }

    #[test]
    fn classify_4_turn_conversation_as_medium() {
        let brain = RouterBrain::new();
        let req = make_request("gpt-4o", vec![
            simple_msg("user", "Q1"),
            simple_msg("assistant", "A1"),
            simple_msg("user", "Q2"),
            simple_msg("assistant", "A2"),
        ]);
        assert_eq!(brain.classify_complexity(&req), Complexity::Medium);
    }

    #[test]
    fn classify_empty_messages_as_low() {
        let brain = RouterBrain::new();
        let req = serde_json::json!({"model": "gpt-4o"});
        assert_eq!(brain.classify_complexity(&req), Complexity::Low);
    }

    // --- Routing Decisions ---

    #[tokio::test]
    async fn low_complexity_gpt4o_routes_to_mini() {
        let brain = RouterBrain::new();
        let req = make_request("gpt-4o", vec![
            simple_msg("user", "What is 2+2?"),
        ]);
        let decision = brain.route("org-1", &req).await;
        assert_eq!(decision.model, Some("gpt-4o-mini".to_string()));
        assert_eq!(decision.strategy, "cheapest");
        assert!(decision.cost_delta > 0.0);
    }

    #[tokio::test]
    async fn low_complexity_non_gpt4_passes_through() {
        let brain = RouterBrain::new();
        let req = make_request("gpt-3.5-turbo", vec![
            simple_msg("user", "Hello"),
        ]);
        let decision = brain.route("org-1", &req).await;
        assert_eq!(decision.model, None);
        assert_eq!(decision.strategy, "passthrough");
    }

    #[tokio::test]
    async fn high_complexity_always_passes_through() {
        let brain = RouterBrain::new();
        let mut msgs = vec![];
        for i in 0..15 {
            msgs.push(simple_msg("user", &format!("msg {}", i)));
        }
        let req = make_request("gpt-4o", msgs);
        let decision = brain.route("org-1", &req).await;
        assert_eq!(decision.model, None);
        assert_eq!(decision.strategy, "passthrough");
        assert_eq!(decision.cost_delta, 0.0);
    }

    #[tokio::test]
    async fn medium_complexity_passes_through() {
        let brain = RouterBrain::new();
        let long_prompt = "You are a helpful assistant. ".repeat(30);
        let req = make_request("gpt-4o", vec![
            simple_msg("system", &long_prompt),
            simple_msg("user", "Help me"),
        ]);
        let decision = brain.route("org-1", &req).await;
        assert_eq!(decision.model, None);
        assert_eq!(decision.strategy, "passthrough");
    }

    // --- Cost Delta ---

    #[test]
    fn cost_delta_gpt4o_to_mini_is_positive() {
        let delta = super::super::router::estimate_cost_delta("gpt-4o", "gpt-4o-mini");
        assert!(delta > 0.004, "Expected significant savings, got {}", delta);
    }

    #[test]
    fn cost_delta_same_model_is_zero() {
        let delta = super::super::router::estimate_cost_delta("gpt-4o", "gpt-4o");
        assert_eq!(delta, 0.0);
    }

    #[test]
    fn cost_delta_claude_opus_to_haiku() {
        let delta = super::super::router::estimate_cost_delta("claude-3-opus", "claude-3-haiku");
        assert!(delta > 0.01, "Opus→Haiku should save >$0.01/1K tokens");
    }
}
