//! Test: Graceful shutdown doesn't lose tasks.

use buquet::models::TaskStatus;
use buquet::queue::SubmitOptions;
use buquet::worker::{claim_task, ClaimResult};
use serde_json::json;

use crate::common::{test_queue, unique_task_type};

#[tokio::test]
#[cfg_attr(
    not(feature = "integration"),
    ignore = "requires S3 (enable feature integration)"
)]
async fn test_graceful_shutdown_requeues_task() {
    let queue = test_queue().await;
    let task_type = unique_task_type();

    // Submit task
    let task = queue
        .submit(&task_type, json!({}), SubmitOptions::default())
        .await
        .expect("submit");

    // Claim task (simulating worker picking it up)
    let result = claim_task(&queue, task.id, "shutdown-test-worker")
        .await
        .expect("claim");
    let claimed = match result {
        ClaimResult::Claimed { task, .. } => *task,
        _ => panic!("Expected Claimed"),
    };

    assert_eq!(claimed.status, TaskStatus::Running);

    // Simulate shutdown by manually requeueing (what Worker::run does on shutdown)
    // In a real test, we'd test the actual shutdown logic
    // For now, verify the task exists and is running
    let (current, _) = queue.get(task.id).await.expect("get").expect("task exists");
    assert_eq!(current.status, TaskStatus::Running);
}
