// file: kb_lib/src/db/queries/wallet_holding.rs

//! Queries for `k_sol_wallet_holdings`.

/// Inserts or updates one wallet-holding row and returns its stable internal id.
pub async fn query_wallet_holdings_upsert(
    database: &crate::Database,
    dto: &crate::WalletHoldingDto,
) -> Result<i64, crate::Error> {
    match database.connection() {
        crate::DatabaseConnection::Sqlite(pool) => {
            let query_result = sqlx::query(
                r#"
INSERT INTO k_sol_wallet_holdings (
    wallet_id,
    token_id,
    first_transaction_id,
    last_transaction_id,
    last_decoded_event_id,
    last_pool_id,
    last_pair_id,
    last_role,
    balance_raw,
    last_slot_observed,
    source_kind,
    source_endpoint_name,
    created_at,
    updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(wallet_id, token_id) DO UPDATE SET
    last_transaction_id = excluded.last_transaction_id,
    last_decoded_event_id = excluded.last_decoded_event_id,
    last_pool_id = excluded.last_pool_id,
    last_pair_id = excluded.last_pair_id,
    last_role = excluded.last_role,
    balance_raw = COALESCE(excluded.balance_raw, k_sol_wallet_holdings.balance_raw),
    last_slot_observed = excluded.last_slot_observed,
    source_kind = excluded.source_kind,
    source_endpoint_name = excluded.source_endpoint_name,
    updated_at = excluded.updated_at
                "#,
            )
            .bind(dto.wallet_id)
            .bind(dto.token_id)
            .bind(dto.first_transaction_id)
            .bind(dto.last_transaction_id)
            .bind(dto.last_decoded_event_id)
            .bind(dto.last_pool_id)
            .bind(dto.last_pair_id)
            .bind(dto.last_role.clone())
            .bind(dto.balance_raw.clone())
            .bind(dto.last_slot_observed)
            .bind(dto.source_kind.to_i16())
            .bind(dto.source_endpoint_name.clone())
            .bind(dto.created_at.to_rfc3339())
            .bind(dto.updated_at.to_rfc3339())
            .execute(pool)
            .await;
            if let Err(error) = query_result {
                return Err(crate::Error::Db(format!(
                    "cannot upsert k_sol_wallet_holdings on sqlite: {}",
                    error
                )));
            }
            let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
                r#"
SELECT id
FROM k_sol_wallet_holdings
WHERE wallet_id = ? AND token_id = ?
LIMIT 1
                "#,
            )
            .bind(dto.wallet_id)
            .bind(dto.token_id)
            .fetch_one(pool)
            .await;
            match id_result {
                Ok(id) => return Ok(id),
                Err(error) => {
                    return Err(crate::Error::Db(format!(
                        "cannot fetch k_sol_wallet_holdings id for wallet_id '{}' token_id '{}' on sqlite: {}",
                        dto.wallet_id, dto.token_id, error
                    )));
                },
            }
        },
    }
}

/// Returns one wallet-holding row identified by `(wallet_id, token_id)`, if it exists.
pub async fn query_wallet_holdings_get_by_wallet_and_token(
    database: &crate::Database,
    wallet_id: i64,
    token_id: i64,
) -> Result<std::option::Option<crate::WalletHoldingDto>, crate::Error> {
    match database.connection() {
        crate::DatabaseConnection::Sqlite(pool) => {
            let query_result = sqlx::query_as::<sqlx::Sqlite, crate::WalletHoldingEntity>(
                r#"
SELECT
    id,
    wallet_id,
    token_id,
    first_transaction_id,
    last_transaction_id,
    last_decoded_event_id,
    last_pool_id,
    last_pair_id,
    last_role,
    balance_raw,
    last_slot_observed,
    source_kind,
    source_endpoint_name,
    created_at,
    updated_at
FROM k_sol_wallet_holdings
WHERE wallet_id = ? AND token_id = ?
LIMIT 1
                    "#,
            )
            .bind(wallet_id)
            .bind(token_id)
            .fetch_optional(pool)
            .await;
            let entity_option = match query_result {
                Ok(entity_option) => entity_option,
                Err(error) => {
                    return Err(crate::Error::Db(format!(
                        "cannot read k_sol_wallet_holdings by wallet_id '{}' token_id '{}' on sqlite: {}",
                        wallet_id, token_id, error
                    )));
                },
            };
            match entity_option {
                Some(entity) => return crate::WalletHoldingDto::try_from(entity).map(Some),
                None => return Ok(None),
            }
        },
    }
}

/// Lists wallet-holding rows for one wallet id.
pub async fn query_wallet_holdings_list_by_wallet_id(
    database: &crate::Database,
    wallet_id: i64,
) -> Result<std::vec::Vec<crate::WalletHoldingDto>, crate::Error> {
    match database.connection() {
        crate::DatabaseConnection::Sqlite(pool) => {
            let query_result = sqlx::query_as::<sqlx::Sqlite, crate::WalletHoldingEntity>(
                r#"
SELECT
    id,
    wallet_id,
    token_id,
    first_transaction_id,
    last_transaction_id,
    last_decoded_event_id,
    last_pool_id,
    last_pair_id,
    last_role,
    balance_raw,
    last_slot_observed,
    source_kind,
    source_endpoint_name,
    created_at,
    updated_at
FROM k_sol_wallet_holdings
WHERE wallet_id = ?
ORDER BY token_id ASC, id ASC
                    "#,
            )
            .bind(wallet_id)
            .fetch_all(pool)
            .await;
            let entities = match query_result {
                Ok(entities) => entities,
                Err(error) => {
                    return Err(crate::Error::Db(format!(
                        "cannot list k_sol_wallet_holdings by wallet_id '{}' on sqlite: {}",
                        wallet_id, error
                    )));
                },
            };
            let mut dtos = std::vec::Vec::new();
            for entity in entities {
                let dto_result = crate::WalletHoldingDto::try_from(entity);
                let dto = match dto_result {
                    Ok(dto) => dto,
                    Err(error) => return Err(error),
                };
                dtos.push(dto);
            }
            return Ok(dtos);
        },
    }
}
