//! This code was AUTOGENERATED using the Codama library.
pub mod close_position_row;
pub mod collect_fund_fee_row;
pub mod collect_protocol_fee_row;
pub mod collect_remaining_rewards_row;
pub mod cpi_event_row;
pub mod create_amm_config_row;
pub mod create_operation_account_row;
pub mod create_permissionless_farm_switch_row;
pub mod create_pool_row;
pub mod create_support_mint_associated_row;
pub mod decrease_liquidity_row;
pub mod decrease_liquidity_v2_row;
pub mod increase_liquidity_row;
pub mod increase_liquidity_v2_row;
pub mod initialize_reward_row;
pub mod open_position_row;
pub mod open_position_v2_row;
pub mod open_position_with_token22_nft_row;
pub mod set_reward_params_row;
pub mod swap_router_base_in_row;
pub mod swap_row;
pub mod swap_v2_row;
pub mod toggle_permissionless_farm_switch_row;
pub mod transfer_reward_owner_row;
pub mod update_amm_config_row;
pub mod update_operation_account_row;
pub mod update_pool_status_row;
pub mod update_reward_infos_row;

pub use self::{
    close_position_row::*, collect_fund_fee_row::*, collect_protocol_fee_row::*,
    collect_remaining_rewards_row::*, cpi_event_row::*, create_amm_config_row::*,
    create_operation_account_row::*, create_permissionless_farm_switch_row::*, create_pool_row::*,
    create_support_mint_associated_row::*, decrease_liquidity_row::*, decrease_liquidity_v2_row::*,
    increase_liquidity_row::*, increase_liquidity_v2_row::*, initialize_reward_row::*,
    open_position_row::*, open_position_v2_row::*, open_position_with_token22_nft_row::*,
    set_reward_params_row::*, swap_router_base_in_row::*, swap_row::*, swap_v2_row::*,
    toggle_permissionless_farm_switch_row::*, transfer_reward_owner_row::*,
    update_amm_config_row::*, update_operation_account_row::*, update_pool_status_row::*,
    update_reward_infos_row::*,
};
use super::PancakeSwapInstruction;

pub struct PancakeSwapInstructionsMigration;

impl sqlx_migrator::Migration<sqlx::Postgres> for PancakeSwapInstructionsMigration {
    fn app(&self) -> &str {
        "pancake-swap"
    }

    fn name(&self) -> &str {
        "pancake_swap_instructions"
    }

    fn operations(&self) -> Vec<Box<dyn sqlx_migrator::Operation<sqlx::Postgres>>> {
        vec![
            Box::new(ClosePositionMigrationOperation),
            Box::new(CollectFundFeeMigrationOperation),
            Box::new(CollectProtocolFeeMigrationOperation),
            Box::new(CollectRemainingRewardsMigrationOperation),
            Box::new(CreateAmmConfigMigrationOperation),
            Box::new(CreateOperationAccountMigrationOperation),
            Box::new(CreatePermissionlessFarmSwitchMigrationOperation),
            Box::new(CreatePoolMigrationOperation),
            Box::new(CreateSupportMintAssociatedMigrationOperation),
            Box::new(DecreaseLiquidityMigrationOperation),
            Box::new(DecreaseLiquidityV2MigrationOperation),
            Box::new(IncreaseLiquidityMigrationOperation),
            Box::new(IncreaseLiquidityV2MigrationOperation),
            Box::new(InitializeRewardMigrationOperation),
            Box::new(OpenPositionMigrationOperation),
            Box::new(OpenPositionV2MigrationOperation),
            Box::new(OpenPositionWithToken22NftMigrationOperation),
            Box::new(SetRewardParamsMigrationOperation),
            Box::new(SwapMigrationOperation),
            Box::new(SwapRouterBaseInMigrationOperation),
            Box::new(SwapV2MigrationOperation),
            Box::new(TogglePermissionlessFarmSwitchMigrationOperation),
            Box::new(TransferRewardOwnerMigrationOperation),
            Box::new(UpdateAmmConfigMigrationOperation),
            Box::new(UpdateOperationAccountMigrationOperation),
            Box::new(UpdatePoolStatusMigrationOperation),
            Box::new(UpdateRewardInfosMigrationOperation),
            Box::new(CpiEventMigrationOperation),
        ]
    }

    fn parents(&self) -> Vec<Box<dyn sqlx_migrator::Migration<sqlx::Postgres>>> {
        vec![]
    }
}

pub struct PancakeSwapInstructionWithMetadata(
    pub PancakeSwapInstruction,
    pub carbon_core::instruction::InstructionMetadata,
    pub Vec<solana_instruction::AccountMeta>,
);

impl
    From<(
        PancakeSwapInstruction,
        carbon_core::instruction::InstructionMetadata,
        Vec<solana_instruction::AccountMeta>,
    )> for PancakeSwapInstructionWithMetadata
{
    fn from(
        value: (
            PancakeSwapInstruction,
            carbon_core::instruction::InstructionMetadata,
            Vec<solana_instruction::AccountMeta>,
        ),
    ) -> Self {
        PancakeSwapInstructionWithMetadata(value.0, value.1, value.2)
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PancakeSwapInstructionWithMetadata {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        let PancakeSwapInstructionWithMetadata(decoded_instruction, metadata, raw_accounts) = self;
        match decoded_instruction {
            PancakeSwapInstruction::ClosePosition { data, .. } => {
                let row = close_position_row::ClosePositionRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CollectFundFee { data, .. } => {
                let row = collect_fund_fee_row::CollectFundFeeRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CollectProtocolFee { data, .. } => {
                let row = collect_protocol_fee_row::CollectProtocolFeeRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CollectRemainingRewards { data, .. } => {
                let row = collect_remaining_rewards_row::CollectRemainingRewardsRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreateAmmConfig { data, .. } => {
                let row = create_amm_config_row::CreateAmmConfigRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreateOperationAccount { data, .. } => {
                let row = create_operation_account_row::CreateOperationAccountRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreatePermissionlessFarmSwitch { data, .. } => {
                let row = create_permissionless_farm_switch_row::CreatePermissionlessFarmSwitchRow::from_parts(data.clone(), metadata.clone(), raw_accounts.clone());
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreatePool { data, .. } => {
                let row = create_pool_row::CreatePoolRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreateSupportMintAssociated { data, .. } => {
                let row =
                    create_support_mint_associated_row::CreateSupportMintAssociatedRow::from_parts(
                        data.clone(),
                        metadata.clone(),
                        raw_accounts.clone(),
                    );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::DecreaseLiquidity { data, .. } => {
                let row = decrease_liquidity_row::DecreaseLiquidityRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::DecreaseLiquidityV2 { data, .. } => {
                let row = decrease_liquidity_v2_row::DecreaseLiquidityV2Row::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::IncreaseLiquidity { data, .. } => {
                let row = increase_liquidity_row::IncreaseLiquidityRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::IncreaseLiquidityV2 { data, .. } => {
                let row = increase_liquidity_v2_row::IncreaseLiquidityV2Row::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::InitializeReward { data, .. } => {
                let row = initialize_reward_row::InitializeRewardRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::OpenPosition { data, .. } => {
                let row = open_position_row::OpenPositionRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::OpenPositionV2 { data, .. } => {
                let row = open_position_v2_row::OpenPositionV2Row::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::OpenPositionWithToken22Nft { data, .. } => {
                let row =
                    open_position_with_token22_nft_row::OpenPositionWithToken22NftRow::from_parts(
                        data.clone(),
                        metadata.clone(),
                        raw_accounts.clone(),
                    );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::SetRewardParams { data, .. } => {
                let row = set_reward_params_row::SetRewardParamsRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::Swap { data, .. } => {
                let row = swap_row::SwapRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::SwapRouterBaseIn { data, .. } => {
                let row = swap_router_base_in_row::SwapRouterBaseInRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::SwapV2 { data, .. } => {
                let row = swap_v2_row::SwapV2Row::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::TogglePermissionlessFarmSwitch { data, .. } => {
                let row = toggle_permissionless_farm_switch_row::TogglePermissionlessFarmSwitchRow::from_parts(data.clone(), metadata.clone(), raw_accounts.clone());
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::TransferRewardOwner { data, .. } => {
                let row = transfer_reward_owner_row::TransferRewardOwnerRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::UpdateAmmConfig { data, .. } => {
                let row = update_amm_config_row::UpdateAmmConfigRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::UpdateOperationAccount { data, .. } => {
                let row = update_operation_account_row::UpdateOperationAccountRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::UpdatePoolStatus { data, .. } => {
                let row = update_pool_status_row::UpdatePoolStatusRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::UpdateRewardInfos { data, .. } => {
                let row = update_reward_infos_row::UpdateRewardInfosRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CpiEvent { data, .. } => {
                let row = cpi_event_row::CpiEventRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
        }
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for PancakeSwapInstructionWithMetadata {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        let PancakeSwapInstructionWithMetadata(decoded_instruction, metadata, raw_accounts) = self;
        match decoded_instruction {
            PancakeSwapInstruction::ClosePosition { data, .. } => {
                let row = close_position_row::ClosePositionRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CollectFundFee { data, .. } => {
                let row = collect_fund_fee_row::CollectFundFeeRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CollectProtocolFee { data, .. } => {
                let row = collect_protocol_fee_row::CollectProtocolFeeRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CollectRemainingRewards { data, .. } => {
                let row = collect_remaining_rewards_row::CollectRemainingRewardsRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreateAmmConfig { data, .. } => {
                let row = create_amm_config_row::CreateAmmConfigRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreateOperationAccount { data, .. } => {
                let row = create_operation_account_row::CreateOperationAccountRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreatePermissionlessFarmSwitch { data, .. } => {
                let row = create_permissionless_farm_switch_row::CreatePermissionlessFarmSwitchRow::from_parts(data.clone(), metadata.clone(), raw_accounts.clone());
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreatePool { data, .. } => {
                let row = create_pool_row::CreatePoolRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CreateSupportMintAssociated { data, .. } => {
                let row =
                    create_support_mint_associated_row::CreateSupportMintAssociatedRow::from_parts(
                        data.clone(),
                        metadata.clone(),
                        raw_accounts.clone(),
                    );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::DecreaseLiquidity { data, .. } => {
                let row = decrease_liquidity_row::DecreaseLiquidityRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::DecreaseLiquidityV2 { data, .. } => {
                let row = decrease_liquidity_v2_row::DecreaseLiquidityV2Row::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::IncreaseLiquidity { data, .. } => {
                let row = increase_liquidity_row::IncreaseLiquidityRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::IncreaseLiquidityV2 { data, .. } => {
                let row = increase_liquidity_v2_row::IncreaseLiquidityV2Row::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::InitializeReward { data, .. } => {
                let row = initialize_reward_row::InitializeRewardRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::OpenPosition { data, .. } => {
                let row = open_position_row::OpenPositionRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::OpenPositionV2 { data, .. } => {
                let row = open_position_v2_row::OpenPositionV2Row::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::OpenPositionWithToken22Nft { data, .. } => {
                let row =
                    open_position_with_token22_nft_row::OpenPositionWithToken22NftRow::from_parts(
                        data.clone(),
                        metadata.clone(),
                        raw_accounts.clone(),
                    );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::SetRewardParams { data, .. } => {
                let row = set_reward_params_row::SetRewardParamsRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::Swap { data, .. } => {
                let row = swap_row::SwapRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::SwapRouterBaseIn { data, .. } => {
                let row = swap_router_base_in_row::SwapRouterBaseInRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::SwapV2 { data, .. } => {
                let row = swap_v2_row::SwapV2Row::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::TogglePermissionlessFarmSwitch { data, .. } => {
                let row = toggle_permissionless_farm_switch_row::TogglePermissionlessFarmSwitchRow::from_parts(data.clone(), metadata.clone(), raw_accounts.clone());
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::TransferRewardOwner { data, .. } => {
                let row = transfer_reward_owner_row::TransferRewardOwnerRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::UpdateAmmConfig { data, .. } => {
                let row = update_amm_config_row::UpdateAmmConfigRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::UpdateOperationAccount { data, .. } => {
                let row = update_operation_account_row::UpdateOperationAccountRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::UpdatePoolStatus { data, .. } => {
                let row = update_pool_status_row::UpdatePoolStatusRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::UpdateRewardInfos { data, .. } => {
                let row = update_reward_infos_row::UpdateRewardInfosRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapInstruction::CpiEvent { data, .. } => {
                let row = cpi_event_row::CpiEventRow::from_parts(
                    data.clone(),
                    metadata.clone(),
                    raw_accounts.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
        }
    }
}
