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

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

use crate::app_context::AppContext;
use anyhow::{Context, Result};
use common::direct_access::frame::FrameRelationshipField;
use common::types::EntityId;
use direct_access::FrameRelationshipDto;
use direct_access::{CreateFrameDto, FrameDto, UpdateFrameDto, frame_controller};

/// Create a new frame entity (orphan, no parent)
pub fn create_orphan_frame(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &CreateFrameDto,
) -> Result<FrameDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    frame_controller::create_orphan(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("creating frame")
}
/// Create a new frame entity as child of owner
pub fn create_frame(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &CreateFrameDto,
    owner_id: EntityId,
    index: i32,
) -> Result<FrameDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    frame_controller::create(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
        owner_id,
        index,
    )
    .context("creating frame")
}
/// Create multiple frame entities (orphan, no parent)
pub fn create_orphan_frame_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[CreateFrameDto],
) -> Result<Vec<FrameDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    frame_controller::create_orphan_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
    )
    .context("creating frame entities")
}
/// Create multiple frame entities as children of owner
pub fn create_frame_multi(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dtos: &[CreateFrameDto],
    owner_id: EntityId,
    index: i32,
) -> Result<Vec<FrameDto>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    frame_controller::create_multi(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dtos,
        owner_id,
        index,
    )
    .context("creating frame entities")
}
/// Get a frame entity by ID
pub fn get_frame(ctx: &AppContext, id: &EntityId) -> Result<Option<FrameDto>> {
    frame_controller::get(&ctx.db_context, id).context("getting frame")
}

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

/// Get all frame 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_frame(ctx: &AppContext) -> Result<Vec<FrameDto>> {
    frame_controller::get_all(&ctx.db_context).context("getting all frame entities")
}

/// Update a frame entity
pub fn update_frame(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &UpdateFrameDto,
) -> Result<FrameDto> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    frame_controller::update(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("updating frame")
}

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

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

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

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

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

/// Get a frame relationship
pub fn get_frame_relationship(
    ctx: &AppContext,
    id: &EntityId,
    field: &FrameRelationshipField,
) -> Result<Vec<EntityId>> {
    frame_controller::get_relationship(&ctx.db_context, id, field)
        .context("getting frame relationship")
}

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

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

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

/// Set a frame relationship
pub fn set_frame_relationship(
    ctx: &AppContext,
    stack_id: Option<u64>,
    dto: &FrameRelationshipDto,
) -> Result<()> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    frame_controller::set_relationship(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        dto,
    )
    .context("setting frame relationship")
}

/// Move (reorder) IDs within a frame relationship
pub fn move_frame_relationship(
    ctx: &AppContext,
    stack_id: Option<u64>,
    id: &EntityId,
    field: &FrameRelationshipField,
    ids_to_move: &[EntityId],
    new_index: i32,
) -> Result<Vec<EntityId>> {
    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
    frame_controller::move_relationship(
        &ctx.db_context,
        &ctx.event_hub,
        &mut undo_redo_manager,
        stack_id,
        id,
        field,
        ids_to_move,
        new_index,
    )
    .context("moving frame relationship")
}
