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

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

use crate::app_context::AppContext;
use anyhow::{Context, Result};
use common::direct_access::root::RootRelationshipField;
use common::types::EntityId;
use direct_access::RootRelationshipDto;
use direct_access::{CreateRootDto, RootDto, UpdateRootDto, root_controller};

/// Create a new root entity (orphan, no parent)
pub fn create_orphan_root(ctx: &AppContext, dto: &CreateRootDto) -> Result<RootDto> {
    root_controller::create_orphan(&ctx.db_context, &ctx.event_hub, dto).context("creating root")
}
/// Create multiple root entities (orphan, no parent)
pub fn create_orphan_root_multi(ctx: &AppContext, dtos: &[CreateRootDto]) -> Result<Vec<RootDto>> {
    root_controller::create_orphan_multi(&ctx.db_context, &ctx.event_hub, dtos)
        .context("creating root entities")
}
/// Get a root entity by ID
pub fn get_root(ctx: &AppContext, id: &EntityId) -> Result<Option<RootDto>> {
    root_controller::get(&ctx.db_context, id).context("getting root")
}

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

/// Get all root 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_root(ctx: &AppContext) -> Result<Vec<RootDto>> {
    root_controller::get_all(&ctx.db_context).context("getting all root entities")
}

/// Update a root entity
pub fn update_root(ctx: &AppContext, dto: &UpdateRootDto) -> Result<RootDto> {
    root_controller::update(&ctx.db_context, &ctx.event_hub, dto).context("updating root")
}

/// Update multiple root entities
pub fn update_root_multi(ctx: &AppContext, dtos: &[UpdateRootDto]) -> Result<Vec<RootDto>> {
    root_controller::update_multi(&ctx.db_context, &ctx.event_hub, dtos)
        .context("updating root entities")
}

/// Update a root entity with relationships
pub fn update_root_with_relationships(ctx: &AppContext, dto: &RootDto) -> Result<RootDto> {
    root_controller::update_with_relationships(&ctx.db_context, &ctx.event_hub, dto)
        .context("updating root with relationships")
}

/// Update multiple root entities with relationships
pub fn update_root_with_relationships_multi(
    ctx: &AppContext,

    dtos: &[RootDto],
) -> Result<Vec<RootDto>> {
    root_controller::update_with_relationships_multi(&ctx.db_context, &ctx.event_hub, dtos)
        .context("updating root entities with relationships")
}

/// Remove a root entity by ID
pub fn remove_root(ctx: &AppContext, id: &EntityId) -> Result<()> {
    root_controller::remove(&ctx.db_context, &ctx.event_hub, id).context("removing root")
}

/// Remove multiple root entities by IDs
pub fn remove_root_multi(ctx: &AppContext, ids: &[EntityId]) -> Result<()> {
    root_controller::remove_multi(&ctx.db_context, &ctx.event_hub, ids)
        .context("removing root entities")
}

/// Get a root relationship
pub fn get_root_relationship(
    ctx: &AppContext,
    id: &EntityId,
    field: &RootRelationshipField,
) -> Result<Vec<EntityId>> {
    root_controller::get_relationship(&ctx.db_context, id, field)
        .context("getting root relationship")
}

/// Get relationship IDs for multiple root entities at once
pub fn get_root_relationship_many(
    ctx: &AppContext,
    ids: &[EntityId],
    field: &RootRelationshipField,
) -> Result<std::collections::HashMap<EntityId, Vec<EntityId>>> {
    root_controller::get_relationship_many(&ctx.db_context, ids, field)
        .context("getting root relationships (many)")
}

/// Get relationship count for a root entity
pub fn get_root_relationship_count(
    ctx: &AppContext,
    id: &EntityId,
    field: &RootRelationshipField,
) -> Result<usize> {
    root_controller::get_relationship_count(&ctx.db_context, id, field)
        .context("getting root relationship count")
}

/// Get relationship IDs for a root entity with pagination
pub fn get_root_relationship_in_range(
    ctx: &AppContext,
    id: &EntityId,
    field: &RootRelationshipField,
    offset: usize,
    limit: usize,
) -> Result<Vec<EntityId>> {
    root_controller::get_relationship_in_range(&ctx.db_context, id, field, offset, limit)
        .context("getting root relationship range")
}

/// Set a root relationship
pub fn set_root_relationship(ctx: &AppContext, dto: &RootRelationshipDto) -> Result<()> {
    root_controller::set_relationship(&ctx.db_context, &ctx.event_hub, dto)
        .context("setting root relationship")
}

/// Move (reorder) IDs within a root relationship
pub fn move_root_relationship(
    ctx: &AppContext,

    id: &EntityId,
    field: &RootRelationshipField,
    ids_to_move: &[EntityId],
    new_index: i32,
) -> Result<Vec<EntityId>> {
    root_controller::move_relationship(
        &ctx.db_context,
        &ctx.event_hub,
        id,
        field,
        ids_to_move,
        new_index,
    )
    .context("moving root relationship")
}
