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

//! Queries for `k_sol_pool_tokens`.

/// Inserts or updates one normalized pool token composition row.
pub async fn query_pool_tokens_upsert(
    database: &crate::Database,
    dto: &crate::PoolTokenDto,
) -> Result<i64, crate::Error> {
    match database.connection() {
        crate::DatabaseConnection::Sqlite(pool) => {
            let query_result = sqlx::query(
                r#"
INSERT INTO k_sol_pool_tokens (
    pool_id,
    token_id,
    role,
    vault_address,
    token_order,
    created_at,
    updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(pool_id, token_id, role) DO UPDATE SET
    vault_address = excluded.vault_address,
    token_order = excluded.token_order,
    updated_at = excluded.updated_at
                "#,
            )
            .bind(dto.pool_id)
            .bind(dto.token_id)
            .bind(dto.role.to_i16())
            .bind(dto.vault_address.clone())
            .bind(dto.token_order)
            .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_pool_tokens on sqlite: {}",
                    error
                )));
            }
            let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
                r#"
SELECT id
FROM k_sol_pool_tokens
WHERE pool_id = ? AND token_id = ? AND role = ?
LIMIT 1
                "#,
            )
            .bind(dto.pool_id)
            .bind(dto.token_id)
            .bind(dto.role.to_i16())
            .fetch_one(pool)
            .await;
            match id_result {
                Ok(id) => return Ok(id),
                Err(error) => {
                    return Err(crate::Error::Db(format!(
                        "cannot fetch k_sol_pool_tokens id on sqlite: {}",
                        error
                    )));
                },
            }
        },
    }
}

/// Lists normalized pool token rows by pool id.
pub async fn query_pool_tokens_list_by_pool_id(
    database: &crate::Database,
    pool_id: i64,
) -> Result<std::vec::Vec<crate::PoolTokenDto>, crate::Error> {
    match database.connection() {
        crate::DatabaseConnection::Sqlite(pool) => {
            let query_result = sqlx::query_as::<sqlx::Sqlite, crate::PoolTokenEntity>(
                r#"
SELECT
    id,
    pool_id,
    token_id,
    role,
    vault_address,
    token_order,
    created_at,
    updated_at
FROM k_sol_pool_tokens
WHERE pool_id = ?
ORDER BY token_order ASC, id ASC
                "#,
            )
            .bind(pool_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_pool_tokens for pool_id '{}' on sqlite: {}",
                        pool_id, error
                    )));
                },
            };
            let mut dtos = std::vec::Vec::new();
            for entity in entities {
                let dto_result = crate::PoolTokenDto::try_from(entity);
                let dto = match dto_result {
                    Ok(dto) => dto,
                    Err(error) => return Err(error),
                };
                dtos.push(dto);
            }
            return Ok(dtos);
        },
    }
}

#[cfg(test)]
mod tests {
    #[tokio::test]
    async fn pool_token_roundtrip_works() {
        let tempdir = tempfile::tempdir().expect("tempdir must succeed");
        let database_path = tempdir.path().join("pool_token_roundtrip.sqlite3");
        let config = crate::DatabaseConfig {
            enabled: true,
            backend: crate::DatabaseBackend::Sqlite,
            sqlite: crate::SqliteDatabaseConfig {
                path: database_path.to_string_lossy().to_string(),
                create_if_missing: true,
                busy_timeout_ms: 5000,
                max_connections: 1,
                auto_initialize_schema: true,
                use_wal: true,
            },
        };
        let database = crate::Database::connect_and_initialize(&config)
            .await
            .expect("database init must succeed");
        let dex_id = crate::query_dexs_upsert(
            &database,
            &crate::DexDto::new("raydium_amm_v4".to_string(), "Raydium AMM v4".to_string(), None, None, true),
        )
        .await
        .expect("dex upsert must succeed");
        let token_id = crate::query_tokens_upsert(
            &database,
            &crate::TokenDto::new(
                "Base111111111111111111111111111111111111111".to_string(),
                Some("BASE".to_string()),
                Some("Base Token".to_string()),
                Some(6),
                crate::SPL_TOKEN_PROGRAM_ID.to_string(),
                false,
            ),
        )
        .await
        .expect("token upsert must succeed");
        let pool_id = crate::query_pools_upsert(
            &database,
            &crate::PoolDto::new(
                dex_id,
                "Pool111111111111111111111111111111111111111".to_string(),
                crate::PoolKind::Amm,
                crate::PoolStatus::Active,
            ),
        )
        .await
        .expect("pool upsert must succeed");
        let pool_token_id = crate::query_pool_tokens_upsert(
            &database,
            &crate::PoolTokenDto::new(
                pool_id,
                token_id,
                crate::PoolTokenRole::Base,
                Some("Vault111111111111111111111111111111111111".to_string()),
                Some(0),
            ),
        )
        .await
        .expect("pool token upsert must succeed");
        assert!(pool_token_id > 0);
        let pool_tokens = crate::query_pool_tokens_list_by_pool_id(&database, pool_id)
            .await
            .expect("list pool tokens must succeed");
        assert_eq!(pool_tokens.len(), 1);
        assert_eq!(pool_tokens[0].role, crate::PoolTokenRole::Base);
    }
}
