mod program_test;
use {
    mpl_token_metadata::accounts::{MasterEdition, Metadata},
    program_test::StablebondProgramTest,
    solana_program_test::{tokio, ProgramTestContext},
    solana_sdk::{
        signature::{Keypair, Signer},
        transaction::Transaction,
    },
    spl_associated_token_account::get_associated_token_address,
    stablebond_sdk::{
        accounts::{Delegate, Kyc},
        find_config_pda, find_delegate_pda, find_kyc_pda, find_payment_feed_pda,
        instructions::{
            AddKycUser, InitializeConfig, InitializeConfigInstructionArgs, InitializeDelegate,
        },
        types::{Discriminator, OracleSetup, PaymentFeedType},
    },
};

struct ExecuteTestContext {
    context: ProgramTestContext,
    admin: Keypair,
}

async fn setup() -> ExecuteTestContext {
    let pt = StablebondProgramTest::start_new().await;
    let admin = pt.admin;
    let nft_collection_mint = pt.nft_collection_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 ixs = &[initialize_config_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 }
}

#[tokio::test]
async fn add_kyc_user() {
    let mut et = setup().await;

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

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

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

    //validate the delegate account
    let account = et
        .context
        .banks_client
        .get_account(find_delegate_pda(et.admin.pubkey()).0)
        .await
        .unwrap();

    assert!(account.is_some());
    let delegate = Delegate::from_bytes(&account.unwrap().data).unwrap();
    assert_eq!(delegate.discriminator, Discriminator::Delegate);
    assert_eq!(delegate.delegate, et.admin.pubkey());

    let add_kyc_user_ix = AddKycUser {
        delegate_wallet: et.admin.pubkey(),
        delegate_account: find_delegate_pda(et.admin.pubkey()).0,
        wallet_account: et.admin.pubkey(),
        kyc_account: find_kyc_pda(et.admin.pubkey()).0,
        system_program: solana_program::system_program::id(),
    }
    .instruction();

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

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

    //validate the kyc account
    let account = et
        .context
        .banks_client
        .get_account(find_kyc_pda(et.admin.pubkey()).0)
        .await
        .unwrap();

    assert!(account.is_some());
    let kyc = Kyc::from_bytes(&account.unwrap().data).unwrap();
    assert_eq!(kyc.discriminator, Discriminator::Kyc);
}
