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

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

use crate::app_context::AppContext;
use anyhow::{Context, Result};
use common::types::EntityId;
use direct_access::{CreateListDto, ListDto, UpdateListDto, list_controller};

/// Create a new list entity (orphan, no parent)
pub fn create_orphan_list(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &CreateListDto,
) -> Result<ListDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    list_controller::create_orphan(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("creating list")
}
/// Create a new list entity as child of owner
pub fn create_list(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &CreateListDto,
    owner_id: EntityId,
    index: i32,
) -> Result<ListDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    list_controller::create(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
        owner_id,
        index,
    )
    .context("creating list")
}
/// Create multiple list entities (orphan, no parent)
pub fn create_orphan_list_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[CreateListDto],
) -> Result<Vec<ListDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    list_controller::create_orphan_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
    )
    .context("creating list entities")
}
/// Create multiple list entities as children of owner
pub fn create_list_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[CreateListDto],
    owner_id: EntityId,
    index: i32,
) -> Result<Vec<ListDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    list_controller::create_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
        owner_id,
        index,
    )
    .context("creating list entities")
}
/// Get a list entity by ID
pub fn get_list(ctx: &AppContext, id: &EntityId) -> Result<Option<ListDto>> {
    list_controller::get(&ctx.db_context, id).context("getting list")
}

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

/// Get all list 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_list(ctx: &AppContext) -> Result<Vec<ListDto>> {
    list_controller::get_all(&ctx.db_context).context("getting all list entities")
}

/// Update a list entity
pub fn update_list(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &UpdateListDto,
) -> Result<ListDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    list_controller::update(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("updating list")
}

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

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

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

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

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