#![cfg(feature = "runtime-benchmarks")]

use super::*;
use frame_benchmarking::v2::*;

#[instance_benchmarks]
mod benchmarks {
    use super::*;
    use frame_support::dispatch::RawOrigin;
    use pallet_ranked_collective::Pallet as Club;

    #[benchmark]
    fn claim() {
        let i = 1337u32;
        let ethereum_secret_key =
            libsecp256k1::SecretKey::parse(&keccak_256(&i.to_le_bytes())).unwrap();
        let eth_address = crate::secp_utils::eth(&ethereum_secret_key);

        let balance = CurrencyOf::<T, I>::minimum_balance();
        let pseudo_account: T::AccountId = Pallet::<T, I>::into_account_id(eth_address).unwrap();
        let _ = CurrencyOf::<T, I>::deposit_creating(&pseudo_account, balance);
        Total::<T, I>::put(balance);

        let pseudo_rank = 5u16;
        let _ = Club::<T, I>::do_add_member_to_rank(pseudo_account.clone(), pseudo_rank, false);

        let user_account: T::AccountId = account("user", i, 0);
        let signature =
            crate::secp_utils::sig::<T, I>(&ethereum_secret_key, &user_account.encode());

        let prev_balance = CurrencyOf::<T, I>::free_balance(&user_account);
        let prev_rank = Club::<T, I>::rank_of(&user_account);

        #[extrinsic_call]
        claim(
            RawOrigin::Signed(user_account.clone()),
            eth_address,
            signature,
        );

        assert_eq!(
            CurrencyOf::<T, I>::free_balance(&user_account),
            prev_balance + balance
        );
        assert_eq!(
            CurrencyOf::<T, I>::free_balance(&pseudo_account),
            balance - balance
        );

        let rank = match prev_rank {
            Some(current_rank) if pseudo_rank <= current_rank => Some(current_rank),
            _ => Some(pseudo_rank),
        };
        assert_eq!(Club::<T, I>::rank_of(&user_account), rank);
        assert_eq!(Club::<T, I>::rank_of(&pseudo_account), None);
    }

    impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test,);
}
