use criterion::{Criterion, criterion_group, criterion_main};
use tycho_types::boc::Boc;
use tycho_types::cell::CellBuilder;
use tycho_types::models::{CurrencyCollection, IntAddr};
use tycho_vm::{GasParams, OwnedCellSlice, SmcInfoBase, VmState, tuple};

fn vm_benchmark(c: &mut Criterion) {
    let code = Boc::decode_base64("te6ccgECGgEABQ4AART/APSkE/S88sgLAQIBYgIDAgLLBAUCASAQEQHX0MtDTAwFxsI5EMIAg1yHTHwGCEBeNRRm6kTDhgEDXIfoAMO1E0PoA+kD6QNTU0/8B+GHRUEWhQTT4QchQBvoCUATPFljPFszMy//J7VTg+kD6QDH6ADH0AfoAMfoAATFw+DoC0x8BAdM/ARKBgAdojhkZYOA54tkgUGD+gvABPztRND6APpA+kDU1NP/Afhh0SaCEGQrfQe6jss1NVFhxwXy4EkE+kAh+kQwwADy4U36ANTRINDTHwGCEBeNRRm68uBIgEDXIfoA+kAx+kAx+gAg1wsAmtdLwAEBwAGw8rGRMOJUQxvgOSWCEHvdl9664wIlghAsdrlzuuMCNCQHCAkKAY4hkXKRceL4OSBuk4F4LpEg4iFulDGBfuCRAeJQI6gToHOBBK1w+DygAnD4NhKgAXD4NqBzgQUTghAJZgGAcPg3oLzysCVZfwsB5jUF+gD6QPgo+EEoEDQB2zxvIjD5AHB0yMsCygfL/8nQUAjHBfLgShKhRBRQNvhByFAG+gJQBM8WWM8WzMzL/8ntVPpA0SDXCwHAALOOIsiAEAHLBQHPFnD6AnABy2qCENUydtsByx8BAcs/yYBC+wCRW+IYAdI1XwM0AfpA0gABAdGVyCHPFsmRbeLIgBABywVQBM8WcPoCcAHLaoIQ0XNUAAHLH1AEAcs/I/pEMMAAjp34KPhBEDVBUNs8byIw+QBwdMjLAsoHy//J0BLPFpcxbBJwAcsB4vQAyYBQ+wAYBP6CEGUB81S6jiUwM1FCxwXy4EkC+kDRQAME+EHIUAb6AlAEzxZYzxbMzMv/ye1U4CSCEPuI4Rm6jiQxMwPRUTHHBfLgSYsCQDT4QchQBvoCUAT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QyL3Jhdy9iN2IzMGMzZTk3MGUwNzdlMTFkMDg1Y2M2NzEzYmUDADAzMTU3YzdjYTA4L21ldGFkYXRhLmpzb24=",
        ).unwrap();

    let addr = "0:2a0c78148c73416b63250b990efdfbf9d5897bf3b33e2f5498a2fe0617174bb8"
        .parse::<IntAddr>()
        .unwrap();

    let addr_slice = CellBuilder::build_from(&addr)
        .map(OwnedCellSlice::new_allow_exotic)
        .unwrap();

    c.bench_function("jetton", |b| {
        b.iter(|| {
            let smc_info = SmcInfoBase::new()
                .with_now(1733142533)
                .with_block_lt(50899537000013)
                .with_tx_lt(50899537000013)
                .with_account_balance(CurrencyCollection::new(1931553923))
                .with_account_addr(addr.clone())
                .require_ton_v4();

            let mut vm_state = VmState::builder()
                .with_smc_info(smc_info)
                .with_stack(tuple![
                    slice addr_slice.clone(),
                    int 103289,
                ])
                .with_code(code.clone())
                .with_data(data.clone())
                .with_gas(GasParams::getter())
                .build();

            let result = vm_state.run();
            _ = std::hint::black_box(result);
        });
    });
}

criterion_group!(benches, vm_benchmark);
criterion_main!(benches);
