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

    for entry in arr {
        // Handle repeated nested PresetInfo - extract InvenIndex values
        let preset_info_index =
            if let Some(nested_arr) = entry.get("presetInfo").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
            };

        sqlx::query(
            r#"
INSERT INTO MonsterHuntPresetInfo (
    Uid,
    PresetInfoIndex
) VALUES (
    ?,
    ?
)
"#,
        )
        .bind(uid)
        .bind(&preset_info_index)
        .execute(pool)
        .await?;
    }

    Ok(())
}

/// Add a single MonsterHuntPresetInfo record from a Rust struct.
pub async fn add_monster_hunt_preset_info(
    pool: &SqlitePool,
    data: &MonsterHuntPresetInfo,
) -> sqlx::Result<()> {
    sqlx::query(
        r#"
INSERT INTO MonsterHuntPresetInfo (
    Uid,
    PresetInfoIndex
) VALUES (
    ?,
    ?
)
"#,
    )
    .bind(&data.uid)
    .bind(&data.preset_info_index)
    .execute(pool)
    .await?;

    Ok(())
}

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

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