//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::graphql::ObservationGraphQL,
    carbon_core::graphql::primitives::{Pubkey, U64},
    juniper::GraphQLObject,
};

#[derive(Debug, Clone, GraphQLObject)]
#[graphql(name = "ObservationState")]
pub struct ObservationStateGraphQL {
    pub account_metadata: crate::accounts::graphql::AccountMetadataGraphQL,
    pub initialized: bool,
    pub recent_epoch: U64,
    pub observation_index: i32,
    pub pool_id: Pubkey,
    pub observations: Vec<ObservationGraphQL>,
    pub padding: Vec<U64>,
}

impl TryFrom<crate::accounts::postgres::ObservationStateRow> for ObservationStateGraphQL {
    type Error = carbon_core::error::Error;
    fn try_from(row: crate::accounts::postgres::ObservationStateRow) -> Result<Self, Self::Error> {
        Ok(Self {
            account_metadata: row.account_metadata.into(),
            initialized: row.initialized,
            recent_epoch: carbon_core::graphql::primitives::U64(*row.recent_epoch),
            observation_index: *row.observation_index,
            pool_id: carbon_core::graphql::primitives::Pubkey(row.pool_id.0),
            observations: row
                .observations
                .0
                .into_iter()
                .map(|item| item.into())
                .collect(),
            padding: row
                .padding
                .into_iter()
                .map(|item| carbon_core::graphql::primitives::U64(*item))
                .collect(),
        })
    }
}
