use crate::models::game::total_war_info::TotalWarInfo;
use serde_json::Value;
use sqlx::SqlitePool;
/// Insert a full JSON array of TotalWarInfo records for a UID.
pub async fn insert_total_war_info(pool: &SqlitePool, data: &Value, uid: i64) -> sqlx::Result<()> {
    let arr = match data.get("totalWarInfo").and_then(|v| v.as_array()) {
        Some(a) => a,
        None => {
            eprintln!("insert_total_war_info: missing or invalid 'totalWarInfo' array");
            return Ok(());
        }
    };

    for entry in arr {
        // Handle repeated nested BattleDamageInfo - extract InvenIndex values
        let score_info_index =
            if let Some(nested_arr) = entry.get("scoreInfo").and_then(|v| v.as_array()) {
                let index: Vec<i64> = nested_arr
                    .iter()
                    .filter_map(|item| item.get("invenIndex").and_then(|v| v.as_i64()))
                    .collect();

                if index.is_empty() {
                    None
                } else {
                    Some(serde_json::to_string(&index).unwrap())
                }
            } else {
                None
            };
        let top_percent = entry
            .get("topPercent")
            .and_then(|v| v.as_i64())
            .unwrap_or_default();
        let top_ranker_score = entry
            .get("topRankerScore")
            .and_then(|v| v.as_i64())
            .unwrap_or_default();
        let engine_type = entry
            .get("engineType")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        sqlx::query(
            r#"
INSERT INTO TotalWarInfo (
    Uid,
    ScoreInfoIndex,
    TopPercent,
    TopRankerScore,
    EngineType
) VALUES (
    ?,
    ?,
    ?,
    ?,
    ?
)
"#,
        )
        .bind(uid)
        .bind(&score_info_index)
        .bind(&top_percent)
        .bind(&top_ranker_score)
        .bind(&engine_type)
        .execute(pool)
        .await?;
    }

    Ok(())
}

/// Add a single TotalWarInfo record from a Rust struct.
pub async fn add_total_war_info(pool: &SqlitePool, data: &TotalWarInfo) -> sqlx::Result<()> {
    sqlx::query(
        r#"
INSERT INTO TotalWarInfo (
    Uid,
    ScoreInfoIndex,
    TopPercent,
    TopRankerScore,
    EngineType
) VALUES (
    ?,
    ?,
    ?,
    ?,
    ?
)
"#,
    )
    .bind(&data.uid)
    .bind(&data.score_info_index)
    .bind(&data.top_percent)
    .bind(&data.top_ranker_score)
    .bind(&data.engine_type)
    .execute(pool)
    .await?;

    Ok(())
}

/// Fetch all records for a given UID.
pub async fn get_total_war_info(pool: &SqlitePool, uid: i64) -> sqlx::Result<Vec<TotalWarInfo>> {
    sqlx::query_as::<_, TotalWarInfo>("SELECT * FROM TotalWarInfo WHERE Uid = ?")
        .bind(uid)
        .fetch_all(pool)
        .await
}

/// Delete all TotalWarInfo rows for a UID.
pub async fn delete_total_war_info(pool: &SqlitePool, uid: i64) -> sqlx::Result<()> {
    sqlx::query("DELETE FROM TotalWarInfo WHERE Uid = ?")
        .bind(uid)
        .execute(pool)
        .await?;
    Ok(())
}
