use crate::ecs::world::World;
use crate::mcp::McpResponse;

pub(crate) fn mcp_set_position(
    world: &mut World,
    request: crate::mcp::SetPositionRequest,
) -> Result<McpResponse, McpResponse> {
    let entity = super::resolve_entity(world, &request.name)?;

    let Some(transform) = world.get_local_transform_mut(entity) else {
        return Err(McpResponse::Error(format!(
            "Entity '{}' has no transform",
            request.name
        )));
    };

    transform.translation = nalgebra_glm::Vec3::new(
        request.position[0],
        request.position[1],
        request.position[2],
    );
    crate::ecs::transform::systems::mark_local_transform_dirty(world, entity);
    Ok(McpResponse::Success(format!(
        "Set position of '{}' to {:?}",
        request.name, request.position
    )))
}

pub(crate) fn mcp_set_rotation(
    world: &mut World,
    request: crate::mcp::SetRotationRequest,
) -> Result<McpResponse, McpResponse> {
    let entity = super::resolve_entity(world, &request.name)?;

    let Some(transform) = world.get_local_transform_mut(entity) else {
        return Err(McpResponse::Error(format!(
            "Entity '{}' has no transform",
            request.name
        )));
    };

    let qx = nalgebra_glm::quat_angle_axis(request.rotation[0], &nalgebra_glm::Vec3::x());
    let qy = nalgebra_glm::quat_angle_axis(request.rotation[1], &nalgebra_glm::Vec3::y());
    let qz = nalgebra_glm::quat_angle_axis(request.rotation[2], &nalgebra_glm::Vec3::z());
    transform.rotation = nalgebra_glm::quat_normalize(&(qz * qy * qx));
    crate::ecs::transform::systems::mark_local_transform_dirty(world, entity);
    Ok(McpResponse::Success(format!(
        "Set rotation of '{}' to {:?}",
        request.name, request.rotation
    )))
}

pub(crate) fn mcp_set_scale(
    world: &mut World,
    request: crate::mcp::SetScaleRequest,
) -> Result<McpResponse, McpResponse> {
    let entity = super::resolve_entity(world, &request.name)?;

    let Some(transform) = world.get_local_transform_mut(entity) else {
        return Err(McpResponse::Error(format!(
            "Entity '{}' has no transform",
            request.name
        )));
    };

    transform.scale = nalgebra_glm::Vec3::new(request.scale[0], request.scale[1], request.scale[2]);
    crate::ecs::transform::systems::mark_local_transform_dirty(world, entity);
    Ok(McpResponse::Success(format!(
        "Set scale of '{}' to {:?}",
        request.name, request.scale
    )))
}

pub(crate) fn mcp_set_parent(
    world: &mut World,
    request: crate::mcp::SetParentRequest,
) -> Result<McpResponse, McpResponse> {
    use crate::ecs::transform::components::Parent;

    let entity = super::resolve_entity(world, &request.name)?;

    let parent_entity = if let Some(ref pname) = request.parent {
        if pname.is_empty() {
            None
        } else {
            let Some(&parent) = world.resources.entity_names.get(pname) else {
                return Err(McpResponse::Error(format!(
                    "Parent entity '{}' not found",
                    pname
                )));
            };
            Some(parent)
        }
    } else {
        None
    };

    world.set_parent(entity, Parent(parent_entity));
    crate::ecs::transform::commands::mark_local_transform_dirty(world, entity);

    match request.parent {
        Some(ref pname) if !pname.is_empty() => Ok(McpResponse::Success(format!(
            "Set parent of '{}' to '{}'",
            request.name, pname
        ))),
        _ => Ok(McpResponse::Success(format!(
            "Unparented '{}'",
            request.name
        ))),
    }
}
