//! This code was AUTOGENERATED using the Codama library.

use {
    super::super::cpi_event::CpiEvent,
    carbon_core::{instruction::InstructionMetadata, postgres::metadata::InstructionRowMetadata},
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct CpiEventRow {
    #[sqlx(flatten)]
    pub instruction_metadata: InstructionRowMetadata,
    pub name: String,
    pub data: sqlx::types::Json<CpiEvent>,
    #[sqlx(rename = "__accounts")]
    pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}

impl CpiEventRow {
    pub fn from_parts(
        source: CpiEvent,
        metadata: InstructionMetadata,
        accounts: Vec<solana_instruction::AccountMeta>,
    ) -> Self {
        Self {
            instruction_metadata: metadata.into(),
            name: match &source {
                CpiEvent::SwapEvent(_) => "swap_event".to_string(),
                CpiEvent::SwapWithFeesCpiEvent(_) => "swap_with_fees_cpi_event".to_string(),
            },
            data: sqlx::types::Json(source),
            accounts: sqlx::types::Json(accounts),
        }
    }
}

impl TryFrom<CpiEventRow> for CpiEvent {
    type Error = carbon_core::error::Error;
    fn try_from(source: CpiEventRow) -> Result<Self, Self::Error> {
        Ok(source.data.0)
    }
}

impl carbon_core::postgres::operations::Table for CpiEvent {
    fn table() -> &'static str {
        "cpi_events"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__signature",
            "__instruction_index",
            "__stack_height",
            "__slot",
            "name",
            "data",
            "__accounts",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for CpiEventRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO cpi_events (
            __signature, __instruction_index, __stack_height, __slot, "name", "data", __accounts
            ) VALUES (
            $1, $2, $3, $4, $5, $6, $7
        )"#,
        )
        .bind(&self.instruction_metadata.signature)
        .bind(self.instruction_metadata.instruction_index)
        .bind(self.instruction_metadata.stack_height)
        .bind(&self.instruction_metadata.slot)
        .bind(&self.name)
        .bind(&self.data)
        .bind(&self.accounts)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for CpiEventRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO cpi_events (
            __signature, __instruction_index, __stack_height, __slot, "name", "data", __accounts
        ) VALUES (
            $1, $2, $3, $4, $5, $6, $7
        ) ON CONFLICT (__signature, __instruction_index, __stack_height) DO UPDATE SET
            __slot = EXCLUDED.__slot,
            "name" = EXCLUDED."name",
            "data" = EXCLUDED."data",
            __accounts = EXCLUDED.__accounts
        "#,
        )
        .bind(&self.instruction_metadata.signature)
        .bind(self.instruction_metadata.instruction_index)
        .bind(self.instruction_metadata.stack_height)
        .bind(&self.instruction_metadata.slot)
        .bind(&self.name)
        .bind(&self.data)
        .bind(&self.accounts)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Delete for CpiEventRow {
    type Key = (
        String,
        carbon_core::postgres::primitives::U32,
        carbon_core::postgres::primitives::U32,
    );

    async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"DELETE FROM cpi_events WHERE
            __signature = $1 AND __instruction_index = $2 AND __stack_height = $3
        "#,
        )
        .bind(key.0)
        .bind(key.1)
        .bind(key.2)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;

        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Lookup for CpiEventRow {
    type Key = (
        String,
        carbon_core::postgres::primitives::U32,
        carbon_core::postgres::primitives::U32,
    );

    async fn lookup(
        key: Self::Key,
        pool: &sqlx::PgPool,
    ) -> carbon_core::error::CarbonResult<Option<Self>> {
        let row = sqlx::query_as(
            r#"SELECT * FROM cpi_events WHERE
            __signature = $1 AND __instruction_index = $2 AND __stack_height = $3
        "#,
        )
        .bind(key.0)
        .bind(key.1)
        .bind(key.2)
        .fetch_optional(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;

        Ok(row)
    }
}

pub struct CpiEventMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for CpiEventMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS cpi_events (
            -- Instruction data
            "name" TEXT NOT NULL,
            "data" JSONB NOT NULL,

            -- Instruction metadata
            __signature TEXT NOT NULL,
            __instruction_index BIGINT NOT NULL,
            __stack_height BIGINT NOT NULL,
            __slot NUMERIC(20),
            __accounts JSONB NOT NULL,

            PRIMARY KEY (__signature, __instruction_index, __stack_height)
        )"#,
        )
        .execute(connection)
        .await?;
        Ok(())
    }

    async fn down(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(r#"DROP TABLE IF EXISTS cpi_events"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}
