use std::borrow::Cow;

use alloy::rpc::client::NoParams;
use alloy_provider::Provider;
use anyhow::Context;
use rpc::client::Client;

/// Mines a single empty L1 block via the connected execution engine.
pub async fn mine_l1_block(client: &Client) -> anyhow::Result<()> {
    client
        .l1_provider
        .raw_request::<_, String>(Cow::Borrowed("evm_mine"), NoParams::default())
        .await
        .context("mining L1 block")?;
    Ok(())
}

/// Increases L1 time by the specified number of seconds.
pub(crate) async fn increase_l1_time(client: &Client, seconds: u64) -> anyhow::Result<()> {
    client
        .l1_provider
        .raw_request::<_, i64>(Cow::Borrowed("evm_increaseTime"), (seconds,))
        .await
        .context("increasing L1 time via evm_increaseTime")?;
    Ok(())
}

/// Mines multiple L1 blocks at once using Anvil's batch mining.
///
/// This is more efficient than calling `mine_l1_block` in a loop.
pub(crate) async fn mine_l1_blocks(client: &Client, count: usize) -> anyhow::Result<()> {
    client
        .l1_provider
        .raw_request::<_, ()>(Cow::Borrowed("anvil_mine"), (count,))
        .await
        .context("mining L1 blocks via anvil_mine")?;
    Ok(())
}
