create table agent_deployments
(
    id                  bigint generated by default as identity primary key,
    asset_id            bigint      not null references hosts (id),
    target_ip           inet        not null,
    os_family           text        not null check (os_family in ('windows', 'linux')),
    transport           text        not null check (transport in ('wmi', 'ssh')),
    status              text        not null check (status in ('pending', 'uploading', 'installing', 'awaiting_heartbeat', 'enrolled', 'failed')),
    error_message       text,
    expected_hostname   text,
    expected_current_ip inet,
    enrollment_token    text        not null unique,
    agent_id            uuid,
    package_version     text,
    last_log            text,
    started_at          timestamptz,
    finished_at         timestamptz,
    created_by          bigint,
    created_at          timestamptz not null default now(),
    updated_at          timestamptz not null default now()
);

create index agent_deployments_asset_id_idx on agent_deployments (asset_id);
create index agent_deployments_target_ip_idx on agent_deployments (target_ip);
create index agent_deployments_status_idx on agent_deployments (status);
create index agent_deployments_enrollment_token_idx on agent_deployments (enrollment_token);
create index agent_deployments_awaiting_match_idx on agent_deployments (asset_id, target_ip, expected_hostname, status);
