// Generated by Qleany v1.5.0 from frontend_entity_commands.tera

//! InlineElement entity commands
#![allow(unused_imports, dead_code)]

use crate::app_context::AppContext;
use anyhow::{Context, Result};
use common::types::EntityId;
use direct_access::{
    CreateInlineElementDto, InlineElementDto, UpdateInlineElementDto, inline_element_controller,
};

/// Create a new inline_element entity (orphan, no parent)
pub fn create_orphan_inline_element(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &CreateInlineElementDto,
) -> Result<InlineElementDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::create_orphan(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("creating inline_element")
}
/// Create a new inline_element entity as child of owner
pub fn create_inline_element(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &CreateInlineElementDto,
    owner_id: EntityId,
    index: i32,
) -> Result<InlineElementDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::create(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
        owner_id,
        index,
    )
    .context("creating inline_element")
}
/// Create multiple inline_element entities (orphan, no parent)
pub fn create_orphan_inline_element_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[CreateInlineElementDto],
) -> Result<Vec<InlineElementDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::create_orphan_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
    )
    .context("creating inline_element entities")
}
/// Create multiple inline_element entities as children of owner
pub fn create_inline_element_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[CreateInlineElementDto],
    owner_id: EntityId,
    index: i32,
) -> Result<Vec<InlineElementDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::create_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
        owner_id,
        index,
    )
    .context("creating inline_element entities")
}
/// Get a inline_element entity by ID
pub fn get_inline_element(ctx: &AppContext, id: &EntityId) -> Result<Option<InlineElementDto>> {
    inline_element_controller::get(&ctx.db_context, id).context("getting inline_element")
}

/// Get multiple inline_element entities by IDs
pub fn get_inline_element_multi(
    ctx: &AppContext,
    ids: &[EntityId],
) -> Result<Vec<Option<InlineElementDto>>> {
    inline_element_controller::get_multi(&ctx.db_context, ids)
        .context("getting inline_element entities")
}

/// Get all inline_element entities.
/// Note: returns entities in database key order (by EntityId), not insertion order
/// or any user-defined sort. For ordered collections, use relationship-based
/// retrieval (e.g. get_*_relationship for ordered_one_to_many fields).
pub fn get_all_inline_element(ctx: &AppContext) -> Result<Vec<InlineElementDto>> {
    inline_element_controller::get_all(&ctx.db_context)
        .context("getting all inline_element entities")
}

/// Update a inline_element entity
pub fn update_inline_element(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &UpdateInlineElementDto,
) -> Result<InlineElementDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::update(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("updating inline_element")
}

/// Update multiple inline_element entities
pub fn update_inline_element_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[UpdateInlineElementDto],
) -> Result<Vec<InlineElementDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::update_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
    )
    .context("updating inline_element entities")
}

/// Update a inline_element entity with relationships
pub fn update_inline_element_with_relationships(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &InlineElementDto,
) -> Result<InlineElementDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::update_with_relationships(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("updating inline_element with relationships")
}

/// Update multiple inline_element entities with relationships
pub fn update_inline_element_with_relationships_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[InlineElementDto],
) -> Result<Vec<InlineElementDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::update_with_relationships_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
    )
    .context("updating inline_element entities with relationships")
}

/// Remove a inline_element entity by ID
pub fn remove_inline_element(ctx: &AppContext, stack_id: Option<u64>, id: &EntityId) -> Result<()> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::remove(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        id,
    )
    .context("removing inline_element")
}

/// Remove multiple inline_element entities by IDs
pub fn remove_inline_element_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    ids: &[EntityId],
) -> Result<()> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    inline_element_controller::remove_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        ids,
    )
    .context("removing inline_element entities")
}
