//! HTTP integration tests for the `/v1/feed` endpoint.

#![allow(clippy::expect_used)]

mod common;

use axum::{
    body::Body,
    http::{Request, StatusCode},
};
use tower::ServiceExt;

use stemedb_api::create_router;

// ============================================================================
// Feed Endpoint Tests
// ============================================================================

#[tokio::test]
async fn test_feed_empty_db() {
    let env = common::create_test_env().await;
    let app = create_router(env.state);

    let request =
        Request::builder().uri("/v1/feed").method("GET").body(Body::empty()).expect("Request");

    let response = app.oneshot(request).await.expect("Request");

    assert_eq!(response.status(), StatusCode::OK);

    let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.expect("Body");
    let json: serde_json::Value = serde_json::from_slice(&body).expect("JSON");

    assert_eq!(json["assertions"], serde_json::json!([]));
    assert_eq!(json["total_count"], 0);
    assert_eq!(json["has_more"], false);
}

#[tokio::test]
async fn test_feed_with_limit_and_offset() {
    let env = common::create_test_env().await;
    let app = create_router(env.state);

    let request = Request::builder()
        .uri("/v1/feed?limit=10&offset=0")
        .method("GET")
        .body(Body::empty())
        .expect("Request");

    let response = app.oneshot(request).await.expect("Request");

    assert_eq!(response.status(), StatusCode::OK);

    let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.expect("Body");
    let json: serde_json::Value = serde_json::from_slice(&body).expect("JSON");

    assert_eq!(json["total_count"], 0);
    assert_eq!(json["has_more"], false);
}

#[tokio::test]
async fn test_feed_default_params() {
    let env = common::create_test_env().await;
    let app = create_router(env.state);

    // No query params at all — should use defaults (limit=50, offset=0)
    let request =
        Request::builder().uri("/v1/feed").method("GET").body(Body::empty()).expect("Request");

    let response = app.oneshot(request).await.expect("Request");

    assert_eq!(response.status(), StatusCode::OK);
}

#[tokio::test]
async fn test_query_no_subject_returns_ok() {
    let env = common::create_test_env().await;
    let app = create_router(env.state);

    // GET /v1/query with no subject should return 200 with empty results
    let request =
        Request::builder().uri("/v1/query").method("GET").body(Body::empty()).expect("Request");

    let response = app.oneshot(request).await.expect("Request");

    assert_eq!(response.status(), StatusCode::OK);

    let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.expect("Body");
    let json: serde_json::Value = serde_json::from_slice(&body).expect("JSON");

    assert_eq!(json["assertions"], serde_json::json!([]));
    assert_eq!(json["total_count"], 0);
}
