use solana_program::instruction::Instruction;
use solana_pubkey::Pubkey;

use crate::{
    operations::{
        spend_kamino, KaminoReserveRefresh, SpendKaminoAccountsParams, SpendKaminoAmountsParams,
        SpendKaminoParams,
    },
    settle_spend, SettleSpendParams,
};

pub struct SpendKaminoThenSettleParams {
    pub spend_amounts: SpendKaminoAmountsParams,
    pub spend_accounts: SpendKaminoAccountsParams,
    pub deposit_reserves: Vec<KaminoReserveRefresh>,
    pub borrow_reserves: Vec<KaminoReserveRefresh>,
    pub settlement_account: Pubkey,
    pub settlement_amount_base_units: u64,
}

pub fn spend_kamino_then_settle(params: &SpendKaminoThenSettleParams) -> Vec<Instruction> {
    let mut instructions = Vec::new();

    let spend_kamino_params = SpendKaminoParams {
        amounts: params.spend_amounts.clone(),
        accounts: params.spend_accounts.clone(),
        deposit_reserves: params.deposit_reserves.clone(),
        borrow_reserves: params.borrow_reserves.clone(),
    };
    let spend_ixs = spend_kamino(&spend_kamino_params);
    instructions.extend(spend_ixs);

    let settle_params = SettleSpendParams {
        admin: params.spend_accounts.admin,
        spend_settlement_account: params.settlement_account,
        mint: params.spend_accounts.liquidity_mint,
        token_program: params.spend_accounts.liquidity_token_program,
        amount_base_units: params.settlement_amount_base_units,
    };
    let settle_ix = settle_spend(&settle_params);
    instructions.push(settle_ix);

    instructions
}
