//! This code was AUTOGENERATED using the Codama library.
pub mod amm_config_row;
pub mod observation_state_row;
pub mod operation_state_row;
pub mod permissionless_farm_switch_row;
pub mod personal_position_state_row;
pub mod pool_state_row;
pub mod protocol_position_state_row;
pub mod support_mint_associated_row;
pub mod tick_array_bitmap_extension_row;
pub mod tick_array_state_row;

pub use self::{
    amm_config_row::*, observation_state_row::*, operation_state_row::*,
    permissionless_farm_switch_row::*, personal_position_state_row::*, pool_state_row::*,
    protocol_position_state_row::*, support_mint_associated_row::*,
    tick_array_bitmap_extension_row::*, tick_array_state_row::*,
};
use super::PancakeSwapAccount;

pub struct PancakeSwapAccountsMigration;

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

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

    fn operations(&self) -> Vec<Box<dyn sqlx_migrator::Operation<sqlx::Postgres>>> {
        vec![
            Box::new(AmmConfigMigrationOperation),
            Box::new(ObservationStateMigrationOperation),
            Box::new(OperationStateMigrationOperation),
            Box::new(PermissionlessFarmSwitchMigrationOperation),
            Box::new(PersonalPositionStateMigrationOperation),
            Box::new(PoolStateMigrationOperation),
            Box::new(ProtocolPositionStateMigrationOperation),
            Box::new(SupportMintAssociatedMigrationOperation),
            Box::new(TickArrayBitmapExtensionMigrationOperation),
            Box::new(TickArrayStateMigrationOperation),
        ]
    }

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

pub struct PancakeSwapAccountWithMetadata(
    pub PancakeSwapAccount,
    pub carbon_core::account::AccountMetadata,
);

impl From<(PancakeSwapAccount, carbon_core::account::AccountMetadata)>
    for PancakeSwapAccountWithMetadata
{
    fn from(value: (PancakeSwapAccount, carbon_core::account::AccountMetadata)) -> Self {
        PancakeSwapAccountWithMetadata(value.0, value.1)
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PancakeSwapAccountWithMetadata {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        let PancakeSwapAccountWithMetadata(account, metadata) = self;

        match account {
            PancakeSwapAccount::AmmConfig(account) => {
                let row =
                    amm_config_row::AmmConfigRow::from_parts(*account.clone(), metadata.clone());
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::ObservationState(account) => {
                let row = observation_state_row::ObservationStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::OperationState(account) => {
                let row = operation_state_row::OperationStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::PermissionlessFarmSwitch(account) => {
                let row = permissionless_farm_switch_row::PermissionlessFarmSwitchRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::PersonalPositionState(account) => {
                let row = personal_position_state_row::PersonalPositionStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::PoolState(account) => {
                let row =
                    pool_state_row::PoolStateRow::from_parts(*account.clone(), metadata.clone());
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::ProtocolPositionState(account) => {
                let row = protocol_position_state_row::ProtocolPositionStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::SupportMintAssociated(account) => {
                let row = support_mint_associated_row::SupportMintAssociatedRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::TickArrayBitmapExtension(account) => {
                let row = tick_array_bitmap_extension_row::TickArrayBitmapExtensionRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::TickArrayState(account) => {
                let row = tick_array_state_row::TickArrayStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
        }
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for PancakeSwapAccountWithMetadata {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        let PancakeSwapAccountWithMetadata(account, metadata) = self;
        match account {
            PancakeSwapAccount::AmmConfig(account) => {
                let row =
                    amm_config_row::AmmConfigRow::from_parts(*account.clone(), metadata.clone());
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::ObservationState(account) => {
                let row = observation_state_row::ObservationStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::OperationState(account) => {
                let row = operation_state_row::OperationStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::PermissionlessFarmSwitch(account) => {
                let row = permissionless_farm_switch_row::PermissionlessFarmSwitchRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::PersonalPositionState(account) => {
                let row = personal_position_state_row::PersonalPositionStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::PoolState(account) => {
                let row =
                    pool_state_row::PoolStateRow::from_parts(*account.clone(), metadata.clone());
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::ProtocolPositionState(account) => {
                let row = protocol_position_state_row::ProtocolPositionStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::SupportMintAssociated(account) => {
                let row = support_mint_associated_row::SupportMintAssociatedRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::TickArrayBitmapExtension(account) => {
                let row = tick_array_bitmap_extension_row::TickArrayBitmapExtensionRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            PancakeSwapAccount::TickArrayState(account) => {
                let row = tick_array_state_row::TickArrayStateRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
        }
    }
}
