CREATE TABLE fleet_hosts (
    installation_id TEXT PRIMARY KEY,
    credential_hash BLOB NOT NULL,
    registration_source_hash BLOB NOT NULL,
    verified_at DATETIME,
    first_seen_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    last_seen_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    last_success_at DATETIME,
    last_event TEXT NOT NULL DEFAULT 'installed',
    last_result TEXT NOT NULL DEFAULT 'success',
    last_error_code TEXT NOT NULL DEFAULT '',
    proxmenu_version TEXT NOT NULL DEFAULT '',
    git_commit TEXT NOT NULL DEFAULT '',
    pve_version TEXT NOT NULL DEFAULT '',
    os_version TEXT NOT NULL DEFAULT '',
    kernel_version TEXT NOT NULL DEFAULT '',
    architecture TEXT NOT NULL DEFAULT '',
    clustered INTEGER NOT NULL DEFAULT 0,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_fleet_hosts_last_seen ON fleet_hosts(last_seen_at);
CREATE INDEX idx_fleet_hosts_version ON fleet_hosts(proxmenu_version);
CREATE INDEX idx_fleet_hosts_registration_source
    ON fleet_hosts(registration_source_hash, first_seen_at);

CREATE TABLE fleet_events (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    installation_id TEXT NOT NULL,
    event_type TEXT NOT NULL,
    result TEXT NOT NULL,
    proxmenu_version TEXT NOT NULL DEFAULT '',
    error_code TEXT NOT NULL DEFAULT '',
    duration_seconds INTEGER NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (installation_id) REFERENCES fleet_hosts(installation_id)
        ON DELETE CASCADE
);
CREATE INDEX idx_fleet_events_host_created
    ON fleet_events(installation_id, created_at);
CREATE INDEX idx_fleet_events_created ON fleet_events(created_at);
