use axum::{body::{to_bytes, Body}, http::{Request, StatusCode}};
use chrono::Utc;
use hermes_backend::{app_state::AppState, build_router, config::AppConfig};
use serde_json as json;
use uuid::Uuid;
use tower::ServiceExt;

#[tokio::test]
async fn health_returns_ok() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let response = app
        .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap())
    .await
    .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let json: json::Value = json::from_slice(&body).unwrap();
    assert!(json["server_time"].as_str().is_some());
}

#[tokio::test]
async fn audit_logging_records_session_and_bet() {
    let state = AppState::new(AppConfig::default(), None, None);
    let app = build_router(state.clone());

    let session_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from("{}"))
                .unwrap(),
        )
        .await
        .unwrap();

    let session_body = to_bytes(session_response.into_body(), usize::MAX).await.unwrap();
    let session_json: json::Value = json::from_slice(&session_body).unwrap();
    let session_id = session_json["session_id"].as_str().unwrap().to_string();

    let event_response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap())
        .await
        .unwrap();

    let event_body = to_bytes(event_response.into_body(), usize::MAX).await.unwrap();
    let event_json: json::Value = json::from_slice(&event_body).unwrap();
    let event_id = event_json["id"].as_str().unwrap().to_string();

    let markets_response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/markets"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap();
    let markets_json: json::Value = json::from_slice(&markets_body).unwrap();
    let market_id = markets_json[0]["id"].as_str().unwrap().to_string();
    let outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();

    let request = json::json!({
        "session_id": session_id,
        "event_id": event_id,
        "market_id": market_id,
        "outcome_id": outcome_id,
        "idempotency_key": "audit-bet-001",
        "client_sent_at": Utc::now().to_rfc3339(),
    })
    .to_string();

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/bets/intent")
                .header("content-type", "application/json")
                .body(Body::from(request))
                .unwrap(),
        )
        .await
        .unwrap();

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

    let (audit_events, audit_attributes) = state.audit_counts().await;
    assert!(audit_events >= 2);
    assert!(audit_attributes >= 10);
}

#[tokio::test]
async fn session_start_and_me_work() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from("{}"))
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let json: json::Value = json::from_slice(&body).unwrap();
    assert_eq!(json["locale_code"], "en");

    let response = app
        .oneshot(Request::builder().uri("/api/v1/session/me").body(Body::empty()).unwrap())
        .await
        .unwrap();

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

#[tokio::test]
async fn participant_ref_reuses_the_same_user() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let body = json::json!({
        "external_ref": "participant-001",
        "locale_code": "sv"
    })
    .to_string();

    let first = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from(body.clone()))
                .unwrap(),
        )
        .await
        .unwrap();

    let first_body = to_bytes(first.into_body(), usize::MAX).await.unwrap();
    let first_json: json::Value = json::from_slice(&first_body).unwrap();

    let second = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from(body))
                .unwrap(),
        )
        .await
        .unwrap();

    let second_body = to_bytes(second.into_body(), usize::MAX).await.unwrap();
    let second_json: json::Value = json::from_slice(&second_body).unwrap();

    assert_eq!(first_json["user_id"], second_json["user_id"]);
    assert_eq!(first_json["locale_code"], "sv");
}

#[tokio::test]
async fn feed_next_returns_a_manifestable_event() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap())
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let event: json::Value = json::from_slice(&body).unwrap();
    let event_id = event["id"].as_str().unwrap().to_string();

    assert_eq!(event["status"], "prefetch_ready");

    let response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/manifest"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let manifest: json::Value = json::from_slice(&body).unwrap();

    assert_eq!(manifest["event"]["id"], event_id);
    assert!(manifest["media"].as_array().unwrap().len() >= 1);
    assert!(manifest["markets"].as_array().unwrap().len() >= 1);
}

#[tokio::test]
async fn bet_intent_accepts_and_is_idempotent() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let session_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from("{}"))
                .unwrap(),
        )
        .await
        .unwrap();

    let session_body = to_bytes(session_response.into_body(), usize::MAX).await.unwrap();
    let session_json: json::Value = json::from_slice(&session_body).unwrap();
    let session_id = session_json["session_id"].as_str().unwrap().to_string();

    let event_response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap())
        .await
        .unwrap();

    let event_body = to_bytes(event_response.into_body(), usize::MAX).await.unwrap();
    let event_json: json::Value = json::from_slice(&event_body).unwrap();
    let event_id = event_json["id"].as_str().unwrap().to_string();

    let markets_response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/markets"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap();
    let markets_json: json::Value = json::from_slice(&markets_body).unwrap();
    let market_id = markets_json[0]["id"].as_str().unwrap().to_string();
    let outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();

    let request = json::json!({
        "session_id": session_id,
        "event_id": event_id,
        "market_id": market_id,
        "outcome_id": outcome_id,
        "idempotency_key": "bet-001",
        "client_sent_at": Utc::now().to_rfc3339(),
    })
    .to_string();

    let response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/bets/intent")
                .header("content-type", "application/json")
                .body(Body::from(request.clone()))
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let first_json: json::Value = json::from_slice(&body).unwrap();
    let bet_id = first_json["id"].as_str().unwrap().to_string();
    assert_eq!(first_json["accepted"], true);
    assert_eq!(first_json["acceptance_code"], "accepted");

    let response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/bets/{bet_id}"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let lookup_json: json::Value = json::from_slice(&body).unwrap();
    assert_eq!(lookup_json["id"], bet_id);
    assert_eq!(lookup_json["accepted"], true);

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/bets/intent")
                .header("content-type", "application/json")
                .body(Body::from(request))
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let duplicate_json: json::Value = json::from_slice(&body).unwrap();
    assert_eq!(duplicate_json["id"], bet_id);
    assert_eq!(duplicate_json["accepted"], true);
}

#[tokio::test]
async fn event_result_returns_settlement() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let event_response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap())
        .await
        .unwrap();

    let event_body = to_bytes(event_response.into_body(), usize::MAX).await.unwrap();
    let event_json: json::Value = json::from_slice(&event_body).unwrap();
    let event_id = event_json["id"].as_str().unwrap().to_string();

    let markets_response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/markets"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap();
    let markets_json: json::Value = json::from_slice(&markets_body).unwrap();
    let market_id = markets_json[0]["id"].as_str().unwrap().to_string();
    let winning_outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();

    let response = app
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/result"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let result_json: json::Value = json::from_slice(&body).unwrap();

    assert_eq!(result_json["market_id"], market_id);
    assert_eq!(result_json["winning_outcome_id"], winning_outcome_id);
}

#[tokio::test]
async fn experiments_and_localization_work() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let session_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "experiment_variant": "modern"
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(session_response.status(), StatusCode::CREATED);

    let config_response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/experiments/config").body(Body::empty()).unwrap())
        .await
        .unwrap();

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

    let config_body = to_bytes(config_response.into_body(), usize::MAX).await.unwrap();
    let config_json: json::Value = json::from_slice(&config_body).unwrap();
    assert_eq!(config_json["variant"], "modern");
    assert_eq!(config_json["feature_flags"]["modern_mode"], true);

    let localization_response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/localization/sv").body(Body::empty()).unwrap())
        .await
        .unwrap();

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

    let localization_body = to_bytes(localization_response.into_body(), usize::MAX).await.unwrap();
    let localization_json: json::Value = json::from_slice(&localization_body).unwrap();
    assert_eq!(localization_json["locale_code"], "sv");
    assert_eq!(localization_json["values"]["common.continue"], "Fortsätt");

    let localization_en_response = app
        .oneshot(Request::builder().uri("/api/v1/localization/en").body(Body::empty()).unwrap())
        .await
        .unwrap();

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

#[tokio::test]
async fn analytics_batch_is_recorded() {
    let state = AppState::new(AppConfig::default(), None, None);
    let app = build_router(state.clone());

    let session_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from("{}"))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(session_response.status(), StatusCode::CREATED);

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/analytics/batch")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "events": [
                            {
                                "event_name": "screen_viewed",
                                "occurred_at": Utc::now().to_rfc3339(),
                                "attributes": [
                                    {"key": "screen_name", "value": "feed"}
                                ]
                            },
                            {
                                "event_name": "cta_pressed",
                                "occurred_at": Utc::now().to_rfc3339()
                            }
                        ]
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

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

    let (event_count, attribute_count) = state.analytics_counts().await;
    assert_eq!(event_count, 2);
    assert_eq!(attribute_count, 1);
}

#[tokio::test]
async fn admin_endpoints_publish_round_data() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let event_id = Uuid::new_v4();
    let market_id = Uuid::new_v4();
    let home_outcome_id = Uuid::new_v4();
    let away_outcome_id = Uuid::new_v4();
    let odds_version_id = Uuid::new_v4();

    let event_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/admin/events")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "event": {
                            "id": event_id,
                            "sport_type": "football",
                            "source_ref": "admin-event-001",
                            "title_en": "Admin created event",
                            "title_sv": "Adminskapad händelse",
                            "status": "scheduled",
                            "preview_start_ms": 0,
                            "preview_end_ms": 1000,
                            "reveal_start_ms": 2000,
                            "reveal_end_ms": 3000,
                            "lock_at": "2099-01-01T01:10:00Z",
                            "settle_at": "2099-01-01T01:25:00Z"
                        },
                        "media": [
                            {
                                "id": Uuid::new_v4(),
                                "event_id": event_id,
                                "media_type": "hls_main",
                                "hls_master_url": "https://cdn.example.com/admin/master.m3u8",
                                "poster_url": "https://cdn.example.com/admin/poster.jpg",
                                "duration_ms": 3000,
                                "preview_start_ms": 0,
                                "preview_end_ms": 1000,
                                "reveal_start_ms": 2000,
                                "reveal_end_ms": 3000
                            }
                        ],
                        "markets": []
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(event_response.status(), StatusCode::CREATED);

    let market_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/admin/markets")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "id": market_id,
                        "event_id": event_id,
                        "question_key": "market.admin.winner",
                        "market_type": "winner",
                        "status": "open",
                        "lock_at": "2099-01-01T01:10:00Z",
                        "settlement_rule_key": "settle_on_match_winner",
                        "outcomes": [
                            {
                                "id": home_outcome_id,
                                "market_id": market_id,
                                "outcome_code": "home",
                                "label_key": "outcome.home",
                                "sort_order": 1
                            },
                            {
                                "id": away_outcome_id,
                                "market_id": market_id,
                                "outcome_code": "away",
                                "label_key": "outcome.away",
                                "sort_order": 2
                            }
                        ]
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(market_response.status(), StatusCode::CREATED);

    let odds_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/admin/odds")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "id": odds_version_id,
                        "market_id": market_id,
                        "version_no": 1,
                        "created_at": "2099-01-01T01:00:00Z",
                        "is_current": true,
                        "odds": [
                            {
                                "id": Uuid::new_v4(),
                                "odds_version_id": odds_version_id,
                                "outcome_id": home_outcome_id,
                                "decimal_odds": 1.9,
                                "fractional_num": 9,
                                "fractional_den": 10
                            },
                            {
                                "id": Uuid::new_v4(),
                                "odds_version_id": odds_version_id,
                                "outcome_id": away_outcome_id,
                                "decimal_odds": 2.1,
                                "fractional_num": 11,
                                "fractional_den": 10
                            }
                        ]
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(odds_response.status(), StatusCode::CREATED);

    let settlement_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/admin/settlements")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "id": Uuid::new_v4(),
                        "market_id": market_id,
                        "settled_at": "2099-01-01T01:25:00Z",
                        "winning_outcome_id": home_outcome_id
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(settlement_response.status(), StatusCode::CREATED);

    let manifest_response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/manifest"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let manifest_body = to_bytes(manifest_response.into_body(), usize::MAX).await.unwrap();
    let manifest_json: json::Value = json::from_slice(&manifest_body).unwrap();
    assert_eq!(manifest_json["markets"].as_array().unwrap().len(), 1);

    let odds_response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/markets/{market_id}/odds/current"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let odds_body = to_bytes(odds_response.into_body(), usize::MAX).await.unwrap();
    let odds_json: json::Value = json::from_slice(&odds_body).unwrap();
    assert_eq!(odds_json["id"], odds_version_id.to_string());

    let result_response = app
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/result"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let result_body = to_bytes(result_response.into_body(), usize::MAX).await.unwrap();
    let result_json: json::Value = json::from_slice(&result_body).unwrap();
    assert_eq!(result_json["market_id"], market_id.to_string());
    assert_eq!(result_json["winning_outcome_id"], home_outcome_id.to_string());
}

#[tokio::test]
async fn event_markets_and_current_odds_work() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let event_response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap())
        .await
        .unwrap();

    let event_body = to_bytes(event_response.into_body(), usize::MAX).await.unwrap();
    let event: json::Value = json::from_slice(&event_body).unwrap();
    let event_id = event["id"].as_str().unwrap().to_string();

    let markets_response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/markets"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap();
    let markets: json::Value = json::from_slice(&markets_body).unwrap();
    let market_id = markets[0]["id"].as_str().unwrap().to_string();

    assert_eq!(markets[0]["outcomes"].as_array().unwrap().len(), 2);

    let odds_response = app
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/markets/{market_id}/odds/current"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let odds_body = to_bytes(odds_response.into_body(), usize::MAX).await.unwrap();
    let odds: json::Value = json::from_slice(&odds_body).unwrap();

    assert_eq!(odds["market_id"], market_id);
    assert_eq!(odds["is_current"], true);
    assert_eq!(odds["odds"].as_array().unwrap().len(), 2);
}

#[tokio::test]
async fn bets_reject_after_market_lock() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let session_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from("{}"))
                .unwrap(),
        )
        .await
        .unwrap();

    let session_body = to_bytes(session_response.into_body(), usize::MAX).await.unwrap();
    let session_json: json::Value = json::from_slice(&session_body).unwrap();
    let session_id = session_json["session_id"].as_str().unwrap().to_string();

    let event_id = Uuid::new_v4();
    let market_id = Uuid::new_v4();
    let home_outcome_id = Uuid::new_v4();
    let away_outcome_id = Uuid::new_v4();

    let event_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/admin/events")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "event": {
                            "id": event_id,
                            "sport_type": "football",
                            "source_ref": "locked-event-001",
                            "title_en": "Locked event",
                            "title_sv": "Låst händelse",
                            "status": "scheduled",
                            "preview_start_ms": 0,
                            "preview_end_ms": 1000,
                            "reveal_start_ms": 2000,
                            "reveal_end_ms": 3000,
                            "lock_at": "2000-01-01T00:00:00Z",
                            "settle_at": "2000-01-01T00:15:00Z"
                        },
                        "media": [],
                        "markets": []
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(event_response.status(), StatusCode::CREATED);

    let market_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/admin/markets")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "id": market_id,
                        "event_id": event_id,
                        "question_key": "market.locked.winner",
                        "market_type": "winner",
                        "status": "locked",
                        "lock_at": "2000-01-01T00:00:00Z",
                        "settlement_rule_key": "settle_on_match_winner",
                        "outcomes": [
                            {
                                "id": home_outcome_id,
                                "market_id": market_id,
                                "outcome_code": "home",
                                "label_key": "outcome.home",
                                "sort_order": 1
                            },
                            {
                                "id": away_outcome_id,
                                "market_id": market_id,
                                "outcome_code": "away",
                                "label_key": "outcome.away",
                                "sort_order": 2
                            }
                        ]
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(market_response.status(), StatusCode::CREATED);

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/bets/intent")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "session_id": session_id,
                        "event_id": event_id,
                        "market_id": market_id,
                        "outcome_id": home_outcome_id,
                        "idempotency_key": "late-bet-001",
                        "client_sent_at": Utc::now().to_rfc3339(),
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let bet_json: json::Value = json::from_slice(&body).unwrap();

    assert_eq!(bet_json["accepted"], false);
    assert_eq!(bet_json["acceptance_code"], "rejected_too_late");
    assert!(bet_json["accepted_odds_version_id"].is_null());
}

#[tokio::test]
async fn bets_reject_with_invalid_session() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let session_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from("{}"))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(session_response.status(), StatusCode::CREATED);

    let event_response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap())
        .await
        .unwrap();

    let event_body = to_bytes(event_response.into_body(), usize::MAX).await.unwrap();
    let event_json: json::Value = json::from_slice(&event_body).unwrap();
    let event_id = event_json["id"].as_str().unwrap().to_string();

    let markets_response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/markets"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap();
    let markets_json: json::Value = json::from_slice(&markets_body).unwrap();
    let market_id = markets_json[0]["id"].as_str().unwrap().to_string();
    let outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/bets/intent")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "session_id": Uuid::new_v4(),
                        "event_id": event_id,
                        "market_id": market_id,
                        "outcome_id": outcome_id,
                        "idempotency_key": "invalid-session-001",
                        "client_sent_at": Utc::now().to_rfc3339(),
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let bet_json: json::Value = json::from_slice(&body).unwrap();

    assert_eq!(bet_json["accepted"], false);
    assert_eq!(bet_json["acceptance_code"], "rejected_invalid_session");
    assert!(bet_json["accepted_odds_version_id"].is_null());
}

#[tokio::test]
async fn bets_reject_with_invalid_market() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let session_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from("{}"))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(session_response.status(), StatusCode::CREATED);

    let session_body = to_bytes(session_response.into_body(), usize::MAX).await.unwrap();
    let session_json: json::Value = json::from_slice(&session_body).unwrap();
    let session_id = session_json["session_id"].as_str().unwrap().to_string();

    let event_response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap())
        .await
        .unwrap();

    let event_body = to_bytes(event_response.into_body(), usize::MAX).await.unwrap();
    let event_json: json::Value = json::from_slice(&event_body).unwrap();
    let event_id = event_json["id"].as_str().unwrap().to_string();

    let markets_response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/markets"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap();
    let markets_json: json::Value = json::from_slice(&markets_body).unwrap();
    let outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/bets/intent")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "session_id": session_id,
                        "event_id": event_id,
                        "market_id": Uuid::new_v4(),
                        "outcome_id": outcome_id,
                        "idempotency_key": "invalid-market-001",
                        "client_sent_at": Utc::now().to_rfc3339(),
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    let bet_json: json::Value = json::from_slice(&body).unwrap();

    assert_eq!(bet_json["accepted"], false);
    assert_eq!(bet_json["acceptance_code"], "rejected_invalid_market");
    assert!(bet_json["accepted_odds_version_id"].is_null());
}

#[tokio::test]
async fn settlement_override_is_reflected_in_results() {
    let app = build_router(AppState::new(AppConfig::default(), None, None));

    let event_response = app
        .clone()
        .oneshot(Request::builder().uri("/api/v1/feed/next").body(Body::empty()).unwrap())
        .await
        .unwrap();

    let event_body = to_bytes(event_response.into_body(), usize::MAX).await.unwrap();
    let event_json: json::Value = json::from_slice(&event_body).unwrap();
    let event_id = event_json["id"].as_str().unwrap().to_string();

    let markets_response = app
        .clone()
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/markets"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

    let markets_body = to_bytes(markets_response.into_body(), usize::MAX).await.unwrap();
    let markets_json: json::Value = json::from_slice(&markets_body).unwrap();
    let market_id = markets_json[0]["id"].as_str().unwrap().to_string();
    let home_outcome_id = markets_json[0]["outcomes"][0]["id"].as_str().unwrap().to_string();
    let away_outcome_id = markets_json[0]["outcomes"][1]["id"].as_str().unwrap().to_string();

    let first_settlement_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/admin/settlements")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "id": Uuid::new_v4(),
                        "market_id": market_id,
                        "settled_at": "2099-01-01T01:25:00Z",
                        "winning_outcome_id": home_outcome_id
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(first_settlement_response.status(), StatusCode::CREATED);

    let second_settlement_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/admin/settlements")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "id": Uuid::new_v4(),
                        "market_id": market_id,
                        "settled_at": "2099-01-01T01:30:00Z",
                        "winning_outcome_id": away_outcome_id
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(second_settlement_response.status(), StatusCode::CREATED);

    let result_response = app
        .oneshot(
            Request::builder()
                .uri(format!("/api/v1/events/{event_id}/result"))
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let result_body = to_bytes(result_response.into_body(), usize::MAX).await.unwrap();
    let result_json: json::Value = json::from_slice(&result_body).unwrap();

    assert_eq!(result_json["market_id"], market_id);
    assert_eq!(result_json["winning_outcome_id"], away_outcome_id);
}

#[tokio::test]
async fn analytics_records_key_user_flow_events() {
    let state = AppState::new(AppConfig::default(), None, None);
    let app = build_router(state.clone());

    let session_response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/session/start")
                .header("content-type", "application/json")
                .body(Body::from("{}"))
                .unwrap(),
        )
        .await
        .unwrap();

    assert_eq!(session_response.status(), StatusCode::CREATED);

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/api/v1/analytics/batch")
                .header("content-type", "application/json")
                .body(Body::from(
                    json::json!({
                        "events": [
                            {
                                "event_name": "session_started",
                                "occurred_at": Utc::now().to_rfc3339(),
                                "attributes": [
                                    {"key": "locale_code", "value": "en"},
                                    {"key": "experiment_variant", "value": "modern"}
                                ]
                            },
                            {
                                "event_name": "feed_viewed",
                                "occurred_at": Utc::now().to_rfc3339(),
                                "attributes": [
                                    {"key": "screen_name", "value": "feed"}
                                ]
                            },
                            {
                                "event_name": "round_loaded",
                                "occurred_at": Utc::now().to_rfc3339(),
                                "attributes": [
                                    {"key": "event_id", "value": Uuid::new_v4().to_string()}
                                ]
                            },
                            {
                                "event_name": "outcome_selected",
                                "occurred_at": Utc::now().to_rfc3339(),
                                "attributes": [
                                    {"key": "market_id", "value": Uuid::new_v4().to_string()},
                                    {"key": "outcome_id", "value": Uuid::new_v4().to_string()}
                                ]
                            },
                            {
                                "event_name": "selection_submitted",
                                "occurred_at": Utc::now().to_rfc3339()
                            },
                            {
                                "event_name": "result_viewed",
                                "occurred_at": Utc::now().to_rfc3339(),
                                "attributes": [
                                    {"key": "screen_name", "value": "result"}
                                ]
                            }
                        ]
                    })
                    .to_string(),
                ))
                .unwrap(),
        )
        .await
        .unwrap();

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

    let (event_count, attribute_count) = state.analytics_counts().await;
    assert_eq!(event_count, 6);
    assert_eq!(attribute_count, 7);
}
