create table agent_tasks
(
    id                 bigint generated by default as identity primary key,
    parent_task_id     bigint      not null references tasks (id) on update cascade on delete cascade,
    executor_agent_id  uuid        not null,
    target_asset_id    bigint      references hosts (id) on update cascade on delete set null,
    target_observed_ip inet,
    type               smallint    not null check (type > 0),
    status             smallint    not null check (status > 0),
    result             jsonb       not null default '{}'::jsonb,
    host_info          jsonb       not null default '{}'::jsonb,
    created_at         timestamptz not null default now(),
    picked_at          timestamptz,
    finished_at        timestamptz,
    deadline_picked    timestamptz not null default (now() + interval '2 minutes'),
    deadline_finished  timestamptz
);

create index agent_tasks_parent_task_id_idx on agent_tasks (parent_task_id);
create index agent_tasks_executor_agent_id_idx on agent_tasks (executor_agent_id);
create index agent_tasks_target_asset_id_idx on agent_tasks (target_asset_id);
create index agent_tasks_status_idx on agent_tasks (status);
create index agent_tasks_type_idx on agent_tasks (type);
