mod program_test;
use {
    mpl_token_metadata::accounts::{MasterEdition, Metadata},
    program_test::StablebondProgramTest,
    solana_program_test::{tokio, ProgramTestContext},
    solana_sdk::{
        pubkey::Pubkey,
        signature::{Keypair, Signer},
        transaction::Transaction,
    },
    spl_associated_token_account::get_associated_token_address,
    stablebond_sdk::{
        find_config_pda, find_delegate_pda, find_payment_feed_pda,
        instructions::{
            InitializeConfig, InitializeConfigInstructionArgs, InitializeDelegate,
            InitializePaymentFeed, InitializePaymentFeedInstructionArgs,
        },
        types::{OracleSetup, PaymentFeedInfo, PaymentFeedType},
    },
};
struct ExecuteTestContext {
    context: ProgramTestContext,
    admin: Keypair,
    usdc_mint: Pubkey,
}

async fn setup() -> ExecuteTestContext {
    let pt = StablebondProgramTest::start_new().await;
    let admin = pt.admin;
    let nft_collection_mint = pt.nft_collection_mint;
    let usdc_mint = pt.mints[0].payment_mint;

    let initialize_config_ix_args = InitializeConfigInstructionArgs {
        oracle: OracleSetup::Stub,
        usdc_mxn_payment_feed: pt.usdc_mxn_payment_feed.clone(),
        usdc_usd_payment_feed: pt.usdc_usd_payment_feed,
    };

    let initialize_config_ix = InitializeConfig {
        admin_wallet: admin.pubkey(),
        config_account: find_config_pda().0,
        config_token_account: get_associated_token_address(
            &find_config_pda().0,
            &nft_collection_mint.pubkey(),
        ),
        mint_account: nft_collection_mint.pubkey(),
        payment_mint_account: pt.usdc_mxn_payment_feed.payment_mint,
        metadata_account: Metadata::find_pda(&nft_collection_mint.pubkey()).0,
        master_edition_account: MasterEdition::find_pda(&nft_collection_mint.pubkey()).0,
        usdc_mxn_feed_account: find_payment_feed_pda(PaymentFeedType::UsdcMxn).0,
        usdc_usd_feed_account: find_payment_feed_pda(PaymentFeedType::UsdcUsd).0,
        token_program: spl_token::id(),
        associated_token_program: spl_associated_token_account::id(),
        metadata_program: mpl_token_metadata::ID,
        system_program: solana_program::system_program::id(),
    }
    .instruction(initialize_config_ix_args);

    let initialize_delegate_ix = InitializeDelegate {
        config_account: find_config_pda().0,
        admin_wallet: admin.pubkey(),
        delegate_wallet: admin.pubkey(),
        delegate_account: find_delegate_pda(admin.pubkey()).0,
        system_program: solana_program::system_program::id(),
    }
    .instruction();

    let ixs = &[initialize_config_ix, initialize_delegate_ix];

    let mut context = pt.context;
    let transaction = Transaction::new_signed_with_payer(
        ixs,
        Some(&admin.pubkey()),
        &[&admin, &nft_collection_mint],
        context.last_blockhash,
    );

    context
        .banks_client
        .process_transaction(transaction)
        .await
        .unwrap();
    ExecuteTestContext {
        context,
        admin,
        usdc_mint,
    }
}
#[tokio::test]
async fn initialize_payment_feed_as_delegate() {
    let mut et = setup().await;

    let payment_feed_info = PaymentFeedInfo {
        payment_feed_type: PaymentFeedType::SwitchboardUsdcMxn,
        payment_mint: et.usdc_mint,
        base_price_feed: Keypair::new().pubkey(),
        quote_price_feed: Keypair::new().pubkey(),
    };

    let initialize_payment_feed_args = InitializePaymentFeedInstructionArgs { payment_feed_info };

    let initialize_payment_feed_ix = InitializePaymentFeed {
        delegate_wallet: et.admin.pubkey(),
        delegate_account: find_delegate_pda(et.admin.pubkey()).0,
        payment_feed_account: find_payment_feed_pda(PaymentFeedType::SwitchboardUsdcMxn).0,
        payment_mint_account: et.usdc_mint,
        system_program: solana_program::system_program::id(),
    }
    .instruction(initialize_payment_feed_args);

    let transaction: Transaction = Transaction::new_signed_with_payer(
        &[initialize_payment_feed_ix],
        Some(&et.admin.pubkey()),
        &[&et.admin],
        et.context.last_blockhash,
    );

    assert!(et
        .context
        .banks_client
        .process_transaction(transaction)
        .await
        .is_ok());
}
