//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0.0-rc.9

use sea_orm::entity::prelude::*;

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "cake")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(unique)]
    pub name: String,
    #[sea_orm(column_type = "Decimal(Some((16, 4)))")]
    pub price: Decimal,
    pub bakery_id: Option<i32>,
    pub gluten_free: bool,
    pub serial: Uuid,
    #[sea_orm(belongs_to, from = "BakeryId", to = "Id")]
    pub bakery: BelongsTo<Option<super::bakery::Entity>>,
    #[sea_orm(has_many)]
    pub lineitems: HasMany<super::lineitem::Entity>,
    #[sea_orm(has_many, via = "cakes_bakers")]
    pub bakers: HasMany<super::baker::Entity>,
}

impl ActiveModelBehavior for ActiveModel {
    fn new() -> Self {
        use sea_orm::Set;
        Self {
            serial: Set(Uuid::new_v4()),
            ..ActiveModelTrait::default()
        }
    }
}

/* // Following will be generated

pub struct EntityLoader {
    select: sea_orm::Select<Entity>,
    with: EntityLoaderWith,
}

#[derive(Debug, Default)]
struct EntityLoaderWith {
    bakery: bool,
    baker: bool,
}

impl sea_orm::QueryFilter for EntityLoader {
    type QueryStatement = <sea_orm::Select<Entity> as sea_orm::QueryFilter>::QueryStatement;

    fn query(&mut self) -> &mut Self::QueryStatement {
        sea_orm::QueryFilter::query(&mut self.select)
    }
}

impl sea_orm::QueryOrder for EntityLoader {
    type QueryStatement = <sea_orm::Select<Entity> as sea_orm::QueryOrder>::QueryStatement;

    fn query(&mut self) -> &mut Self::QueryStatement {
        sea_orm::QueryOrder::query(&mut self.select)
    }
}

impl sea_orm::compound::EntityLoaderTrait<Entity> for EntityLoader {}

impl Entity {
    pub fn load() -> EntityLoader {
        EntityLoader {
            select: Entity::find(),
            with: Default::default(),
        }
    }
}

impl EntityLoader {
    pub async fn one<C: sea_orm::ConnectionTrait>(
        mut self,
        db: &C,
    ) -> Result<Option<Model>, DbErr> {
        use sea_orm::QuerySelect;

        self.select = self.select.limit(1);
        Ok(self.all(db).await?.into_iter().next())
    }

    pub fn with<R>(mut self, entity: R) -> Self
    where
        R: EntityTrait,
        Entity: Related<R>,
    {
        if entity.table_ref() == super::bakery::Entity.table_ref() {
            self.with.bakery = true;
        }
        if entity.table_ref() == super::baker::Entity.table_ref() {
            self.with.baker = true;
        }
        self
    }

    pub async fn load<C: sea_orm::ConnectionTrait>(Vec<Model>, with: &EntityLoaderWith, nest: &EntityLoaderNest, db: &C) -> Result<Vec<Model>, DbErr> {
        if self.with.baker {
            let bakers = cakes.load_many(super::baker::Entity, db).await?;
            super::baker::EntityLoader::load_nest_nest(bakers, &nest.baker).await?;

            for (cake, bakers) in cakes.iter_mut().zip(bakers) {
                cake.bakers.set(bakers);
            }
        }
    }

    pub async fn all<C: sea_orm::ConnectionTrait>(mut self, db: &C) -> Result<Vec<Model>, DbErr> {
        let select = if self.with.bakery {
            self.with.bakery = false;
            self.select.find_also(Entity, super::bakery::Entity)
        } else {
            // select also but without join
            self.select.select_also_fake(super::bakery::Entity)
        };

        let models = select.all(db).await?;

        let mut cakes = Vec::new();

        for (mut cake, bakery) in models {
            cake.bakery.set(bakery);
            cakes.push(cake);
        }

        let cakes = Self::load(cakes, &self.with, db).await?;

        Ok(cakes)
    }
}

*/
