use {
    super::{
        setup_actors, setup_validator_whitelist_account,
        setup_validator_whitelist_program, setup_vote_account,
        setup_whitelist_entry, SvmContext, NEW_AUTHORITY_NAME,
        VALIDATOR_WHITELIST_ACCOUNT_NAME, VOTE_ACCOUNT_1_NAME,
        VOTE_ACCOUNT_2_NAME, WHITELIST_ENTRY_1_NAME, WHITELIST_ENTRY_2_NAME,
    },
    solana_signer::Signer,
    spherenet_validator_whitelist_interface::state::{
        account::ValidatorWhitelistAccount, load_mut,
    },
};

pub fn setup_context() -> SvmContext {
    let mut context = SvmContext::new();
    setup_validator_whitelist_program(&mut context);
    setup_actors(&mut context);
    setup_vote_account(&mut context, VOTE_ACCOUNT_1_NAME);
    setup_vote_account(&mut context, VOTE_ACCOUNT_2_NAME);
    setup_validator_whitelist_account(&mut context);
    let vote_account_pubkey = *context.get_account_pubkey(VOTE_ACCOUNT_1_NAME);
    setup_whitelist_entry(
        &mut context,
        vote_account_pubkey,
        WHITELIST_ENTRY_1_NAME,
    );

    context
}

pub fn setup_context_with_2_whitelist_entries() -> SvmContext {
    let mut context = setup_context();
    let vote_account_2_pubkey =
        *context.get_account_pubkey(VOTE_ACCOUNT_2_NAME);
    setup_whitelist_entry(
        &mut context,
        vote_account_2_pubkey,
        WHITELIST_ENTRY_2_NAME,
    );

    context
}

pub fn setup_context_with_authority_transfer_initiated() -> SvmContext {
    let mut context = setup_context();

    let new_authority = context.get_actor(NEW_AUTHORITY_NAME).pubkey();

    let whitelist_account_pubkey =
        context.get_account_pubkey(VALIDATOR_WHITELIST_ACCOUNT_NAME);
    let mut whitelist_account =
        context.svm.get_account(&whitelist_account_pubkey).unwrap();

    let whitelist_account_data =
        load_mut::<ValidatorWhitelistAccount>(&mut whitelist_account.data)
            .unwrap();

    whitelist_account_data.pending_authority = new_authority.to_bytes();

    whitelist_account.data = whitelist_account_data.pack().to_vec();

    context
        .svm
        .set_account(*whitelist_account_pubkey, whitelist_account)
        .unwrap();

    context
}
