//! Inference loop edge case tests.
//!
//! Tests the scenarios that are covered by Codex (64 tests) and Gemini CLI
//! (111 tests) but were missing from Koda. Uses the existing MockProvider +
//! Env harness with real in-memory SQLite — no new mock infrastructure needed.
//!
//! Run with: `cargo test -p koda-core --features test-support --test inference_edge_test`

use koda_core::{engine::EngineEvent, persistence::Persistence};
use koda_test_utils::{Env, MockProvider, MockResponse};

// ── Rate limit retry ─────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn rate_limit_then_success() {
    let env = Env::new().await;
    env.insert_user_message("hello").await;

    // First call: rate limited. Second call: success.
    let provider = MockProvider::new(vec![
        MockResponse::RateLimit,
        MockResponse::Text("recovered!".into()),
    ]);
    let events = env.run_inference(&provider).await;

    // Should see a warning about rate limiting.
    let has_rate_warn = events
        .iter()
        .any(|e| matches!(e, EngineEvent::Warn { message } if message.contains("Rate limited")));
    assert!(has_rate_warn, "expected rate limit warning in: {events:?}");

    // Should still produce a response.
    let has_text = events
        .iter()
        .any(|e| matches!(e, EngineEvent::TextDelta { .. }));
    assert!(has_text, "expected text after retry");

    // DB should have the response.
    let last = env
        .db
        .last_assistant_message(&env.session_id)
        .await
        .unwrap();
    assert!(last.contains("recovered!"), "DB: {last}");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn rate_limit_exhausted_returns_error() {
    let env = Env::new().await;
    env.insert_user_message("hello").await;

    // All retries are rate limited.
    let provider = MockProvider::new(vec![
        MockResponse::RateLimit,
        MockResponse::RateLimit,
        MockResponse::RateLimit,
        MockResponse::RateLimit,
        MockResponse::RateLimit,
    ]);
    let (result, _events) = env.run_inference_result(&provider).await;

    assert!(result.is_err(), "should fail after max retries");
    let err = format!("{:#}", result.unwrap_err());
    assert!(
        err.contains("429") || err.contains("Rate limit") || err.contains("Too Many Requests"),
        "error should mention rate limit: {err}"
    );
}

// ── Network error mid-stream ─────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn network_error_mid_stream_discards_response() {
    let env = Env::new().await;
    env.insert_user_message("hello").await;

    let provider = MockProvider::new(vec![MockResponse::NetworkError {
        partial_text: "partial respo".into(),
        error: "connection reset by peer".into(),
    }]);
    let (result, events) = env.run_inference_result(&provider).await;

    // Should end gracefully (not crash).
    assert!(result.is_ok(), "network error should be graceful");

    // Should emit a warning about the connection drop.
    let has_warn = events
        .iter()
        .any(|e| matches!(e, EngineEvent::Warn { message } if message.contains("Connection lost")));
    assert!(has_warn, "expected connection warning in: {events:?}");

    // The partial response must NOT be persisted (would corrupt session).
    let messages = env.db.load_context(&env.session_id).await.unwrap();
    let assistant_msgs: Vec<_> = messages
        .iter()
        .filter(|m| m.role.as_str() == "assistant")
        .collect();
    assert!(
        assistant_msgs.is_empty(),
        "partial response should not be persisted: {assistant_msgs:?}"
    );
}

// ── Empty response retry ─────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn empty_response_after_tool_use_retries_once() {
    let env = Env::new().await;
    env.insert_user_message("do something").await;

    let provider = MockProvider::new(vec![
        // First: tool call.
        MockResponse::tool_call("Bash", serde_json::json!({"command": "echo hi"})),
        // Second: empty response (model hiccup).
        MockResponse::Text("".into()),
        // Third: actual response after retry.
        MockResponse::Text("Done!".into()),
    ]);
    let events = env.run_inference(&provider).await;

    // Should contain the retry spinner.
    let has_retry = events.iter().any(|e| {
        matches!(e, EngineEvent::SpinnerStart { message } if message.contains("retry"))
            || matches!(e, EngineEvent::SpinnerStart { message } if message.contains("Retry"))
    });
    assert!(has_retry, "expected retry on empty response: {events:?}");

    // Final response should be persisted.
    let last = env
        .db
        .last_assistant_message(&env.session_id)
        .await
        .unwrap();
    assert!(last.contains("Done!"), "DB: {last}");
}

// ── max_tokens truncation ────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn max_tokens_continues_loop() {
    let env = Env::new().await;
    env.insert_user_message("write a long essay").await;

    let provider = MockProvider::new(vec![
        // First: truncated response.
        MockResponse::TextMaxTokens("The essay begins...".into()),
        // Second: model continues (inference loop re-enters).
        MockResponse::Text("And that's the conclusion.".into()),
    ]);
    let events = env.run_inference(&provider).await;

    // Should warn about max_tokens.
    let has_warn = events
        .iter()
        .any(|e| matches!(e, EngineEvent::Warn { message } if message.contains("max_tokens")));
    assert!(has_warn, "expected max_tokens warning: {events:?}");

    // The second response should be the final one persisted.
    let last = env
        .db
        .last_assistant_message(&env.session_id)
        .await
        .unwrap();
    assert!(
        last.contains("conclusion"),
        "should have continued after truncation: {last}"
    );
}

// ── Loop detection ───────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn loop_detection_injects_feedback_then_stops() {
    let env = Env::new().await;
    env.insert_user_message("keep trying").await;

    // Produce the same tool call repeatedly. The loop detector fires after
    // 5 consecutive identical calls (matching Gemini CLI's threshold).
    // First detection: injects feedback and continues.
    // Second detection: hard stops.
    // Need 10 repeated calls: 5 to trigger feedback + 5 more to hard stop.
    let repeated_call =
        MockResponse::tool_call("Bash", serde_json::json!({"command": "echo stuck"}));
    let provider = MockProvider::new(vec![
        repeated_call.clone(), // 1st consecutive
        repeated_call.clone(), // 2nd
        repeated_call.clone(), // 3rd
        repeated_call.clone(), // 4th
        repeated_call.clone(), // 5th → InjectFeedback, clear, continue
        repeated_call.clone(), // 1st consecutive (reset)
        repeated_call.clone(), // 2nd
        repeated_call.clone(), // 3rd
        repeated_call.clone(), // 4th
        repeated_call.clone(), // 5th → HardStop
        MockResponse::Text("unreachable".into()),
    ]);
    let events = env.run_inference(&provider).await;

    // Should see the feedback injection warning first — with count + tool name
    let feedback_msg = events.iter().find_map(|e| match e {
        EngineEvent::Warn { message } if message.contains("Loop detected") => {
            Some(message.as_str())
        }
        _ => None,
    });
    assert!(
        feedback_msg.is_some(),
        "expected feedback injection warning: {events:?}"
    );
    let feedback_msg = feedback_msg.unwrap();
    assert!(
        feedback_msg.contains("5"),
        "feedback warning should include repeat count: {feedback_msg}"
    );
    assert!(
        feedback_msg.contains("Bash"),
        "feedback warning should name the culprit tool: {feedback_msg}"
    );

    // Should see the hard stop warning second — also with count + tool name
    let hard_stop_msg = events.iter().find_map(|e| match e {
        EngineEvent::Warn { message } if message.contains("Loop guard") => Some(message.as_str()),
        _ => None,
    });
    assert!(
        hard_stop_msg.is_some(),
        "expected hard stop warning: {events:?}"
    );
    let hard_stop_msg = hard_stop_msg.unwrap();
    assert!(
        hard_stop_msg.contains("5"),
        "hard stop warning should include repeat count: {hard_stop_msg}"
    );
    assert!(
        hard_stop_msg.contains("Bash"),
        "hard stop warning should name the culprit tool: {hard_stop_msg}"
    );
}

// ── Eager tool execution (ToolCallReady) ─────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn eager_execution_of_read_only_tools() {
    let env = Env::new().await;
    let test_file = env.root.join("eagerly_read.txt");
    std::fs::write(&test_file, "eagerly read content").unwrap();
    env.insert_user_message("read the file").await;

    // Use ToolCallsEager to simulate Anthropic's per-block ToolCallReady.
    let provider = MockProvider::new(vec![
        MockResponse::ToolCallsEager(vec![koda_core::providers::ToolCall {
            id: "eager_1".into(),
            function_name: "Read".into(),
            arguments: serde_json::json!({"file_path": test_file.to_string_lossy()}).to_string(),
            thought_signature: None,
        }]),
        MockResponse::Text("I read the file.".into()),
    ]);
    let events = env.run_inference(&provider).await;

    // Should have a tool result with the file content.
    let tool_result = events.iter().find_map(|e| {
        if let EngineEvent::ToolCallResult { output, name, .. } = e
            && name == "Read"
        {
            Some(output.clone())
        } else {
            None
        }
    });
    assert!(
        tool_result.is_some(),
        "expected Read tool result: {events:?}"
    );
    assert!(
        tool_result.unwrap().contains("eagerly read content"),
        "should contain file content"
    );
}

// ── Multiple tool calls in parallel ──────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn multiple_read_only_tools_dispatch() {
    let env = Env::new().await;
    let f1 = env.root.join("file1.txt");
    let f2 = env.root.join("file2.txt");
    std::fs::write(&f1, "content1").unwrap();
    std::fs::write(&f2, "content2").unwrap();
    env.insert_user_message("read both files").await;

    // Two read-only tool calls in one batch.
    let provider = MockProvider::new(vec![
        MockResponse::ToolCalls(vec![
            koda_core::providers::ToolCall {
                id: "tc_1".into(),
                function_name: "Read".into(),
                arguments: serde_json::json!({"file_path": f1.to_string_lossy()}).to_string(),
                thought_signature: None,
            },
            koda_core::providers::ToolCall {
                id: "tc_2".into(),
                function_name: "Read".into(),
                arguments: serde_json::json!({"file_path": f2.to_string_lossy()}).to_string(),
                thought_signature: None,
            },
        ]),
        MockResponse::Text("Both files read.".into()),
    ]);
    let events = env.run_inference(&provider).await;

    // Both tool results should appear.
    let tool_results: Vec<_> = events
        .iter()
        .filter_map(|e| {
            if let EngineEvent::ToolCallResult { output, name, .. } = e
                && name == "Read"
            {
                Some(output.clone())
            } else {
                None
            }
        })
        .collect();
    assert_eq!(tool_results.len(), 2, "expected 2 Read results: {events:?}");
    assert!(tool_results.iter().any(|o| o.contains("content1")));
    assert!(tool_results.iter().any(|o| o.contains("content2")));
}

// ── Cancel during tool execution ─────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn cancel_during_tool_execution() {
    let env = Env::new().await;
    env.insert_user_message("run a slow command").await;

    // Tool call that takes a while. Cancel during execution.
    let provider = MockProvider::new(vec![
        MockResponse::tool_call("Bash", serde_json::json!({"command": "sleep 10"})),
        MockResponse::Text("should not reach this".into()),
    ]);

    // **#1109 F3**: was `tokio::spawn(sleep(200ms); cancel())`. Replaced
    // with event-driven cancel: fire as soon as the engine emits
    // ToolCallStart for our Bash invocation. Deterministic and faster.
    let (result, _events) = env
        .run_inference_cancel_on_event(&provider, |ev| {
            matches!(ev, koda_core::engine::EngineEvent::ToolCallStart { name, .. } if name == "Bash")
        })
        .await;

    assert!(result.is_ok(), "cancel should be graceful");
    // Should finish quickly (not wait 10 seconds).
    // The Interrupted warning might not appear if cancel fires during tool dispatch,
    // but the loop should exit without error.
}

// ── Server error graceful exit ───────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn server_error_exits_gracefully() {
    let env = Env::new().await;
    env.insert_user_message("hello").await;

    // Simulate a 500 Internal Server Error.
    let provider = MockProvider::new(vec![MockResponse::Error(
        "LLM API returned 500: Internal Server Error".into(),
    )]);
    let (result, events) = env.run_inference_result(&provider).await;

    // Should exit gracefully, not crash.
    assert!(result.is_ok(), "server error should be handled gracefully");

    let has_warn = events
        .iter()
        .any(|e| matches!(e, EngineEvent::Warn { message } if message.contains("server error")));
    assert!(has_warn, "expected server error warning: {events:?}");
}

// ── Context overflow recovery ────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn context_overflow_attempts_compact_and_retry() {
    let env = Env::new().await;

    // Fill the context with enough messages to make compaction meaningful.
    for i in 0..20 {
        env.insert_user_message(&format!("Question {i}: Tell me about topic {i}"))
            .await;
        env.db
            .insert_message(
                &env.session_id,
                &koda_core::db::Role::Assistant,
                Some(
                    &"Answer: Here's a long response about this topic that fills up context. "
                        .repeat(5),
                ),
                None,
                None,
                None,
            )
            .await
            .unwrap();
    }
    env.insert_user_message("final question").await;

    // First call: context overflow. Second: success (after compaction).
    let provider = MockProvider::new(vec![
        MockResponse::ContextOverflow,
        MockResponse::Text("recovered after compaction!".into()),
    ]);
    let (result, events) = env.run_inference_result(&provider).await;

    // Compaction may succeed or fail depending on whether the compact provider
    // can handle the history. Either way, the loop should handle it gracefully.
    // If it worked, we'll see a Compacted info event.
    // If it failed, we'll see an error (which is OK — the test validates the
    // recovery path was attempted).
    let _attempted_recovery = events.iter().any(|e| {
        matches!(e, EngineEvent::Warn { message } if message.contains("overflow"))
            || matches!(e, EngineEvent::Info { message } if message.contains("Compact"))
    });

    // At minimum, the overflow should not crash.
    if let Err(err) = result {
        let msg = err.to_string();
        assert!(
            msg.contains("compaction") || msg.contains("overflow") || msg.contains("context"),
            "error should be about context/compaction: {msg}"
        );
    }
}

// ── Token usage tracking ─────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn footer_includes_token_usage() {
    let env = Env::new().await;
    env.insert_user_message("hello").await;

    let provider = MockProvider::new(vec![MockResponse::Text("Hi there!".into())]);
    let events = env.run_inference(&provider).await;

    let footer = events.iter().find_map(|e| {
        if let EngineEvent::Footer {
            prompt_tokens,
            completion_tokens,
            ..
        } = e
        {
            Some((*prompt_tokens, *completion_tokens))
        } else {
            None
        }
    });
    assert!(footer.is_some(), "expected Footer event: {events:?}");
    let (prompt, completion) = footer.unwrap();
    assert!(prompt > 0, "prompt tokens should be > 0");
    assert!(completion > 0, "completion tokens should be > 0");
}

// ── Multi-turn tool use ──────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn multi_step_tool_chain_persists_all() {
    let env = Env::new().await;
    let target = env.root.join("chained.txt");
    env.insert_user_message("create then read a file").await;

    let provider = MockProvider::new(vec![
        // Step 1: Write a file.
        MockResponse::tool_call(
            "Write",
            serde_json::json!({
                "file_path": target.to_string_lossy(),
                "content": "chain test"
            }),
        ),
        // Step 2: Read it back.
        MockResponse::tool_call(
            "Read",
            serde_json::json!({"file_path": target.to_string_lossy()}),
        ),
        // Step 3: Final response.
        MockResponse::Text("Done! File created and verified.".into()),
    ]);
    let events = env.run_inference(&provider).await;

    // Both tool calls should appear.
    let tool_names: Vec<_> = events
        .iter()
        .filter_map(|e| {
            if let EngineEvent::ToolCallStart { name, .. } = e {
                Some(name.clone())
            } else {
                None
            }
        })
        .collect();
    assert!(
        tool_names.contains(&"Write".to_string()),
        "tools: {tool_names:?}"
    );
    assert!(
        tool_names.contains(&"Read".to_string()),
        "tools: {tool_names:?}"
    );

    // File should exist with correct content.
    let content = std::fs::read_to_string(&target).unwrap();
    assert_eq!(content, "chain test");

    // Session history should have user, assistant (write), tool result,
    // assistant (read), tool result, assistant (final).
    let messages = env.db.load_context(&env.session_id).await.unwrap();
    assert!(
        messages.len() >= 5,
        "expected at least 5 messages, got {}",
        messages.len()
    );
}
