
-- Current state of agents
CREATE TABLE IF NOT EXISTS agents (
    agentid TEXT PRIMARY KEY,
    clientversion TEXT,
    domain TEXT,
    freespace INTEGER,
    groupid TEXT,
    hostname TEXT,
    ip TEXT,
    localip TEXT,
    lastcheckin TEXT,
    os TEXT,
    policyversion TEXT,
    status INTEGER,
    username TEXT,
    groupname TEXT,
    status_text TEXT,
    firstseen TEXT
);

-- History of clientversion changes
CREATE TABLE IF NOT EXISTS agent_clientversion_history (
    agentid TEXT,
    clientversion TEXT,
    timestamp TEXT,
    PRIMARY KEY (agentid, clientversion, timestamp)
);

-- History of IP changes
CREATE TABLE IF NOT EXISTS agent_ip_history (
    agentid TEXT,
    ip TEXT,
    timestamp TEXT,
    PRIMARY KEY (agentid, ip, timestamp)
);

-- History of local IP changes
CREATE TABLE IF NOT EXISTS agent_localip_history (
    agentid TEXT,
    localip TEXT,
    timestamp TEXT,
    PRIMARY KEY (agentid, localip, timestamp)
);

-- History of policyversion changes
CREATE TABLE IF NOT EXISTS agent_policyversion_history (
    agentid TEXT,
    policyversion TEXT,
    timestamp TEXT,
    PRIMARY KEY (agentid, policyversion, timestamp)
);


-- Current state of policies
CREATE TABLE IF NOT EXISTS policies (
    groupid TEXT PRIMARY KEY,
    hidden BOOLEAN,
    name TEXT,
    parent TEXT,
    firstseen TEXT
);

-- History of policy name changes
CREATE TABLE IF NOT EXISTS policy_name_history (
    groupid TEXT,
    name TEXT,
    timestamp TEXT,
    PRIMARY KEY (groupid, name, timestamp)
);


-- Current state of allowlists
CREATE TABLE IF NOT EXISTS allowlists (
    applicationid TEXT PRIMARY KEY,
    name TEXT,
    version TEXT,
    firstseen TEXT
);

-- History of allowlist name changes
CREATE TABLE IF NOT EXISTS allowlist_name_history (
    applicationid TEXT,
    name TEXT,
    timestamp TEXT,
    PRIMARY KEY (applicationid, name, timestamp)
);

-- History of allowlist version changes
CREATE TABLE IF NOT EXISTS allowlist_version_history (
    applicationid TEXT,
    version TEXT,
    timestamp TEXT,
    PRIMARY KEY (applicationid, version, timestamp)
);
