-- Existing robot rows are intentionally left with robot_id = NULL.
alter table inspection_robot
    add column robot_id varchar(128) null comment 'Permanent robot identity reported by QUIC' after id,
    add column connect_status char(1) not null default '0' comment '0 offline, 1 online' after robot_id,
    add column quic_node_id varchar(128) null comment 'Current QUIC node instance' after connect_status,
    add column quic_session_id varchar(128) null comment 'Current QUIC session' after quic_node_id,
    add column quic_boot_id varchar(128) null comment 'Current edge boot id' after quic_session_id,
    add column observed_ip varchar(64) null comment 'Observed QUIC source IP' after quic_boot_id,
    add column software_version varchar(64) null comment 'Edge software version' after observed_ip,
    add column last_heartbeat_time datetime(3) null comment 'Last accepted heartbeat time' after software_version,
    add column archived_status char(1) not null default '0' comment '0 active, 1 archived' after last_heartbeat_time,
    add index idx_inspection_robot_robot_id (robot_id),
    add index idx_inspection_robot_connect_status (connect_status);

create table inspection_robot_device (
    id varchar(64) not null,
    robot_id varchar(128) not null,
    device_id varchar(128) not null,
    device_name varchar(128) null,
    device_kind int null,
    type_name varchar(128) null,
    enabled char(1) not null default '1',
    manager_state int null,
    health_status int null,
    online_status char(1) not null default '0',
    has_error char(1) not null default '0',
    error_message varchar(1000) null,
    status_updated_time datetime(3) null,
    last_seen_time datetime(3) null,
    offline_time datetime(3) null,
    create_by varchar(64) null,
    create_time datetime null,
    update_by varchar(64) null,
    update_time datetime null,
    remark varchar(500) null,
    primary key (id),
    index idx_robot_device_robot_id (robot_id),
    index idx_robot_device_identity (robot_id, device_id),
    index idx_robot_device_online (robot_id, online_status)
) comment='Devices discovered from robot QUIC heartbeat';

-- New task instances record the permanent identity. Legacy instances remain NULL.
alter table te_task_inst
    add column robot_id varchar(128) null comment 'Permanent robot identity' after task_id,
    add index idx_te_task_inst_robot_id (robot_id);
