<?xml version="1.0" encoding="utf-8"?>
<robot name="agv_pro" xmlns:xacro="http://www.ros.org/wiki/xacro"
    xmlns:gz="http://gazebosim.org/schema">

    <!-- Optional namespace prefix for all link/joint names -->
    <xacro:arg name="prefix" default="" />
    <xacro:property name="prefix" value="$(arg prefix)" />

    <!-- Hardware selection: false -> real serial base, true -> Gazebo (gz-sim) -->
    <xacro:arg name="sim" default="false" />
    <xacro:property name="sim" value="$(arg sim)" />

    <!-- Real-hardware serial port (ignored when sim=true) -->
    <xacro:arg name="port_name" default="/dev/agvpro_controller" />
    <xacro:property name="port_name" value="$(arg port_name)" />

    <!-- Controller params file consumed by gz_ros2_control (sim only) -->
    <xacro:arg name="controllers_file" default="" />
    <xacro:property name="controllers_file" value="$(arg controllers_file)" />

    <link name="${prefix}base_footprint" />

    <joint name="${prefix}base_joint" type="fixed">
        <parent link="${prefix}base_footprint" />
        <child link="${prefix}base_link" />
        <origin xyz="0 0 0.020" rpy="0 0 0" />
    </joint>

    <link name="${prefix}base_link">
        <inertial>
            <origin xyz="-0.0076254 0.0 0.06693" rpy="0 0 0" />
            <mass value="19.236" />
            <inertia ixx="0.14436" ixy="0.0012037" ixz="0.0019458"
                iyy="0.24191" iyz="0.0044629"
                izz="0.33755" />
        </inertial>

        <visual>
            <origin xyz="0 0 0"
                rpy="0 0 0" />
            <geometry>
                <mesh filename="package://agv_pro_description/meshes/base_link.stl" />
            </geometry>
            <material name="">
                <color rgba="1 1 1 1" />
            </material>
        </visual>

        <collision>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <geometry>
                <mesh filename="package://agv_pro_description/meshes/base_link.stl" />
            </geometry>
        </collision>
    </link>

    <!-- Wheels (built from the shared wheel macro) -->
    <xacro:include filename="$(find agv_pro_description)/urdf/parts/wheel_macro.xacro" />

    <xacro:agv_pro_wheel
        prefix="${prefix}front_right_"
        parent="${prefix}base_link"
        reflect="false"
        mesh="wheel_lb_link.stl"
        origin_xyz="0.17128 -0.1799 0.052"
        gazebo_ignition="${sim}" />

    <xacro:agv_pro_wheel
        prefix="${prefix}front_left_"
        parent="${prefix}base_link"
        reflect="true"
        mesh="wheel_lf_link.stl"
        origin_xyz="0.17128 0.1799 0.052"
        gazebo_ignition="${sim}" />

    <xacro:agv_pro_wheel
        prefix="${prefix}rear_left_"
        parent="${prefix}base_link"
        reflect="false"
        mesh="wheel_rf_link.stl"
        origin_xyz="-0.17181 0.1799 0.052"
        gazebo_ignition="${sim}" />

    <xacro:agv_pro_wheel
        prefix="${prefix}rear_right_"
        parent="${prefix}base_link"
        reflect="true"
        mesh="wheel_rb_link.stl"
        origin_xyz="-0.171806101587598 -0.179900399999999 0.052"
        gazebo_ignition="${sim}" />


    <link name="${prefix}laser_link">
        <inertial>
            <origin xyz="-0.0035142 -2.8248E-05 0.0010013" rpy="0 0 0" />
            <mass value="0.049095" />
            <inertia ixx="2.0572E-05" ixy="8.5013E-08" ixz="2.0871E-07" iyy="2.0483E-05"
                iyz="-4.2154E-09"
                izz="3.4612E-05" />
        </inertial>

        <visual>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <geometry>
                <mesh filename="package://agv_pro_description/meshes/laser_link.stl" />
            </geometry>
            <material name="">
                <color rgba="1 1 1 1" />
            </material>
        </visual>

        <collision>
            <origin xyz="0 0 0" rpy="0 0 0" />
            <geometry>
                <mesh filename="package://agv_pro_description/meshes/laser_link.stl" />
            </geometry>
        </collision>
    </link>

    <joint name="${prefix}lidar_joint" type="fixed">
        <origin xyz="0.17891 0 0.20928" rpy="0 0 0" />
        <parent link="${prefix}base_link" />
        <child link="${prefix}laser_link" />
    </joint>

    <link name="${prefix}camera_link" />

    <joint name="${prefix}camera_joint" type="fixed">
        <origin xyz="0.23191 0 0.14928" rpy="0 0 0" />
        <parent link="${prefix}base_link" />
        <child link="${prefix}camera_link" />
    </joint>

    <link name="${prefix}imu_link" />

    <joint name="${prefix}imu_joint" type="fixed">
        <origin xyz="-0.17181 -0.0270532 0.14928" rpy="0 0 1.5707" />
        <parent link="${prefix}base_link" />
        <child link="${prefix}imu_link" />
    </joint>

    <!-- ros2_control: real hardware or gz_ros2_control depending on 'sim' -->
    <xacro:include filename="$(find agv_pro_description)/urdf/control/agv_pro.ros2_control.xacro" />
    <xacro:agv_pro_ros2_control sim="${sim}" prefix="${prefix}" port_name="${port_name}" />

    <!-- Gazebo system plugin (hosts controller_manager); wheel friction is in the wheel macro -->
    <xacro:if value="${sim}">
        <xacro:include filename="$(find agv_pro_description)/urdf/agv_pro.gazebo.xacro" />
        <xacro:agv_pro_gazebo prefix="${prefix}" controllers_file="${controllers_file}" />
    </xacro:if>

</robot>