-- UniFi Database Schema
-- Generated from API data analysis
-- 3 tables

CREATE TABLE hosts (
    id TEXT NOT NULL PRIMARY KEY,
    hardwareId TEXT NOT NULL,
    type TEXT NOT NULL,
    ipAddress TEXT NOT NULL,
    owner BOOLEAN NOT NULL,
    isBlocked BOOLEAN NOT NULL,
    registrationTime TEXT NOT NULL,
    lastConnectionStateChange TEXT NOT NULL,
    latestBackupTime TEXT NOT NULL,
    userData.status TEXT NOT NULL,
    reportedState.controller_uuid TEXT NOT NULL,
    reportedState.firmware_version REAL,
    reportedState.hardware_id TEXT,
    reportedState.host_type INTEGER NOT NULL,
    reportedState.hostname TEXT NOT NULL,
    reportedState.inform_port REAL,
    reportedState.mgmt_port INTEGER NOT NULL,
    reportedState.name TEXT NOT NULL,
    reportedState.override_inform_host TEXT,
    reportedState.release_channel TEXT,
    reportedState.state TEXT NOT NULL,
    reportedState.version TEXT NOT NULL,
    userData.email TEXT,
    userData.features.deviceGroups TEXT,
    userData.features.floorplan.canEdit TEXT,
    userData.features.floorplan.canView TEXT,
    userData.features.manageApplications TEXT,
    userData.features.notifications TEXT,
    userData.features.webrtc.iceRestart TEXT,
    userData.features.webrtc.mediaStreams TEXT,
    userData.features.webrtc.mediaStreamsAV1 TEXT,
    userData.features.webrtc.mediaStreamsH265 TEXT,
    userData.features.webrtc.twoWayAudio TEXT,
    userData.fullName TEXT,
    userData.localId TEXT,
    userData.role TEXT,
    userData.roleId TEXT,
    reportedState.anonid TEXT,
    reportedState.autoUpdate.includeApplications TEXT,
    reportedState.autoUpdate.preferencesPrompt.unifiOS.applications TEXT,
    reportedState.autoUpdate.preferencesPrompt.unifiOS.defaultSchedule.day REAL,
    reportedState.autoUpdate.preferencesPrompt.unifiOS.defaultSchedule.frequency TEXT,
    reportedState.autoUpdate.preferencesPrompt.unifiOS.defaultSchedule.hour REAL,
    reportedState.autoUpdate.preferencesPrompt.unifiOS.firmware TEXT,
    reportedState.autoUpdate.schedule.day REAL,
    reportedState.autoUpdate.schedule.frequency TEXT,
    reportedState.autoUpdate.schedule.hour REAL,
    reportedState.deviceErrorCode REAL,
    reportedState.deviceState TEXT,
    reportedState.deviceStateLastChanged REAL,
    reportedState.directConnectDomain TEXT,
    reportedState.features.alarmManager TEXT,
    reportedState.features.apiIntegration TEXT,
    reportedState.features.applications.access.mspPlayback TEXT,
    reportedState.features.captiveProxy TEXT,
    reportedState.features.cloud.applicationEvents TEXT,
    reportedState.features.cloud.applicationEventsHttp TEXT,
    reportedState.features.cloud.ucp4GuestConnection TEXT,
    reportedState.features.cloudBackup TEXT,
    reportedState.features.customSmtpServer TEXT,
    reportedState.features.deviceList.autolinkDevices TEXT,
    reportedState.features.deviceList.partialUpdates TEXT,
    reportedState.features.deviceList.ucp4Events TEXT,
    reportedState.features.directRemoteConnection TEXT,
    reportedState.features.hasBezel TEXT,
    reportedState.features.hasGateway TEXT,
    reportedState.features.hasLCM TEXT,
    reportedState.features.hasLED TEXT,
    reportedState.features.identity.hostingIdentityHubScore REAL,
    reportedState.features.identity.standard TEXT,
    reportedState.features.identity.ucs TEXT,
    reportedState.features.identity.ucsAgent TEXT,
    reportedState.features.identity.ucsRemoteAccessViaUOS TEXT,
    reportedState.features.identity.unifiedAdminsUsersPage TEXT,
    reportedState.features.infoApis.firmwareUpdate TEXT,
    reportedState.features.isAutomaticFailoverAvailable TEXT,
    reportedState.features.led.canSetColor TEXT,
    reportedState.features.mfa TEXT,
    reportedState.features.mspBridgeModesSupported TEXT,
    reportedState.features.multiplePoolsSupport TEXT,
    reportedState.features.netInAppBackupSupport TEXT,
    reportedState.features.notifications TEXT,
    reportedState.features.sharedTokens TEXT,
    reportedState.features.snmpConfig TEXT,
    reportedState.features.supportForm TEXT,
    reportedState.features.syslog TEXT,
    reportedState.features.teleport TEXT,
    reportedState.features.teleportState TEXT,
    reportedState.features.uidService TEXT,
    reportedState.features.updates.applicationReleaseChannels TEXT,
    reportedState.features.updates.applicationSchedules TEXT,
    reportedState.firmwareUpdate.latestAvailableVersion REAL,
    reportedState.hardware.bom TEXT,
    reportedState.hardware.cpu.id TEXT,
    reportedState.hardware.debianCodename TEXT,
    reportedState.hardware.firmwareVersion TEXT,
    reportedState.hardware.hwrev REAL,
    reportedState.hardware.isUbios TEXT,
    reportedState.hardware.mac TEXT,
    reportedState.hardware.name TEXT,
    reportedState.hardware.qrid TEXT,
    reportedState.hardware.reboot TEXT,
    reportedState.hardware.serialno TEXT,
    reportedState.hardware.shortname TEXT,
    reportedState.hardware.subtype TEXT,
    reportedState.hardware.sysid REAL,
    reportedState.hardware.upgrade TEXT,
    reportedState.hardware.uuid TEXT,
    reportedState.ip TEXT,
    reportedState.isStacked TEXT,
    reportedState.isUbiosMigration TEXT,
    reportedState.location.lat REAL,
    reportedState.location.long REAL,
    reportedState.location.radius REAL,
    reportedState.location.text TEXT,
    reportedState.mac TEXT,
    reportedState.releaseChannel TEXT,
    reportedState.timezone TEXT,
    reportedState.ucareState REAL,
    reportedState.uidb.guid TEXT,
    reportedState.uidb.id TEXT,
    reportedState.uidb.images.default TEXT,
    reportedState.uidb.images.mobile-connection TEXT,
    reportedState.uidb.images.mobile-internet-connected TEXT,
    reportedState.uidb.images.mobile-no-internet TEXT,
    reportedState.uidb.images.nopadding TEXT,
    reportedState.uidb.images.topology TEXT,
    registrationTime_parsed TIMESTAMP,
    lastConnectionStateChange_parsed TIMESTAMP NOT NULL
);

CREATE TABLE sites (
    siteId TEXT NOT NULL PRIMARY KEY,
    hostId TEXT NOT NULL,
    permission TEXT NOT NULL,
    isOwner BOOLEAN NOT NULL,
    meta.desc TEXT NOT NULL,
    meta.name TEXT NOT NULL,
    meta.timezone TEXT NOT NULL,
    statistics.counts.criticalNotification INTEGER NOT NULL,
    statistics.counts.gatewayDevice INTEGER NOT NULL,
    statistics.counts.guestClient INTEGER NOT NULL,
    statistics.counts.lanConfiguration INTEGER NOT NULL,
    statistics.counts.offlineDevice INTEGER NOT NULL,
    statistics.counts.offlineGatewayDevice INTEGER NOT NULL,
    statistics.counts.offlineWifiDevice INTEGER NOT NULL,
    statistics.counts.offlineWiredDevice INTEGER NOT NULL,
    statistics.counts.pendingUpdateDevice INTEGER NOT NULL,
    statistics.counts.totalDevice INTEGER NOT NULL,
    statistics.counts.wanConfiguration INTEGER NOT NULL,
    statistics.counts.wifiClient INTEGER NOT NULL,
    statistics.counts.wifiConfiguration INTEGER NOT NULL,
    statistics.counts.wifiDevice INTEGER NOT NULL,
    statistics.counts.wiredClient INTEGER NOT NULL,
    statistics.counts.wiredDevice INTEGER NOT NULL,
    meta.gatewayMac TEXT,
    statistics.gateway.hardwareId TEXT,
    statistics.gateway.inspectionState TEXT,
    statistics.gateway.ipsMode TEXT,
    statistics.gateway.ipsSignature.rulesCount REAL,
    statistics.gateway.ipsSignature.type TEXT,
    statistics.gateway.shortname TEXT,
    statistics.ispInfo.name TEXT,
    statistics.ispInfo.organization TEXT,
    statistics.percentages.txRetry REAL,
    statistics.percentages.wanUptime REAL,
    statistics.wanMagic.available TEXT,
    statistics.wanMagic.enabled TEXT,
    statistics.wanMagic.subscribed TEXT,
    statistics.wans.WAN.externalIp TEXT,
    statistics.wans.WAN.ispInfo.name TEXT,
    statistics.wans.WAN.ispInfo.organization TEXT,
    statistics.wans.WAN.wanUptime REAL,
    meta.timezone_parsed TIMESTAMP,
    statistics.counts.pendingUpdateDevice_parsed TIMESTAMP NOT NULL
);

CREATE TABLE devices (
    hostId TEXT NOT NULL PRIMARY KEY,
    updatedAt TEXT NOT NULL,
    updatedAt_parsed TIMESTAMP NOT NULL,
    FOREIGN KEY (hostId) REFERENCES sites(hostId)
);