-- 设备档案表: 每台设备一行, upsert 更新
-- 大厂标准: 事件流水(event_log) + 设备快照(device_profile) 分离
-- 设备分布查询直接走此表，O(devices) 而非 O(events)
CREATE TABLE "analytics_device_profile" (
  "install_id"   VARCHAR(64)   NOT NULL,
  "user_id"      VARCHAR(20),
  "device_brand" VARCHAR(64),
  "device_model" VARCHAR(64),
  "device_os"    VARCHAR(32),
  "app_version"  VARCHAR(32),
  "locale"       VARCHAR(16),
  "first_seen_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "last_seen_at"  TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "event_count"   INTEGER     NOT NULL DEFAULT 1,

  CONSTRAINT "analytics_device_profile_pkey" PRIMARY KEY ("install_id")
);

CREATE INDEX "idx_device_profile_brand"       ON "analytics_device_profile"("device_brand");
CREATE INDEX "idx_device_profile_app_version" ON "analytics_device_profile"("app_version");
CREATE INDEX "idx_device_profile_user_id"     ON "analytics_device_profile"("user_id");
CREATE INDEX "idx_device_profile_last_seen"   ON "analytics_device_profile"("last_seen_at" DESC);
