-- CreateTable
CREATE TABLE "presence_event_log" (
    "id" BIGSERIAL NOT NULL,
    "user_id" VARCHAR(36),
    "install_id" VARCHAR(64) NOT NULL,
    "event_name" VARCHAR(64) NOT NULL,
    "event_time" TIMESTAMPTZ NOT NULL,
    "device_brand" VARCHAR(64),
    "device_model" VARCHAR(64),
    "device_os" VARCHAR(32),
    "app_version" VARCHAR(32),
    "locale" VARCHAR(16),
    "properties" JSONB,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "presence_event_log_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "presence_device_profile" (
    "install_id" VARCHAR(64) NOT NULL,
    "user_id" VARCHAR(36),
    "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,
    "event_count" INTEGER NOT NULL DEFAULT 1,

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

-- CreateTable
CREATE TABLE "presence_daily_active_users" (
    "day" DATE NOT NULL,
    "dau_count" INTEGER NOT NULL,
    "dau_by_province" JSONB,
    "dau_by_city" JSONB,
    "calculated_at" TIMESTAMPTZ NOT NULL,
    "version" INTEGER NOT NULL DEFAULT 1,

    CONSTRAINT "presence_daily_active_users_pkey" PRIMARY KEY ("day")
);

-- CreateTable
CREATE TABLE "presence_online_snapshots" (
    "id" BIGSERIAL NOT NULL,
    "ts" TIMESTAMPTZ NOT NULL,
    "online_count" INTEGER NOT NULL,
    "window_seconds" INTEGER NOT NULL DEFAULT 300,

    CONSTRAINT "presence_online_snapshots_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "idx_presence_event_log_event_time" ON "presence_event_log"("event_time");

-- CreateIndex
CREATE INDEX "idx_presence_event_log_event_name" ON "presence_event_log"("event_name");

-- CreateIndex
CREATE INDEX "idx_presence_event_log_event_name_time" ON "presence_event_log"("event_name", "event_time");

-- CreateIndex
CREATE INDEX "idx_presence_event_log_user_id" ON "presence_event_log"("user_id");

-- CreateIndex
CREATE INDEX "idx_presence_event_log_device_brand" ON "presence_event_log"("device_brand");

-- CreateIndex
CREATE INDEX "idx_presence_event_log_app_version" ON "presence_event_log"("app_version");

-- CreateIndex
CREATE INDEX "idx_presence_device_profile_brand" ON "presence_device_profile"("device_brand");

-- CreateIndex
CREATE INDEX "idx_presence_device_profile_app_version" ON "presence_device_profile"("app_version");

-- CreateIndex
CREATE INDEX "idx_presence_device_profile_user_id" ON "presence_device_profile"("user_id");

-- CreateIndex
CREATE INDEX "idx_presence_device_profile_last_seen" ON "presence_device_profile"("last_seen_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "presence_online_snapshots_ts_key" ON "presence_online_snapshots"("ts");

-- CreateIndex
CREATE INDEX "idx_presence_online_snapshots_ts" ON "presence_online_snapshots"("ts" DESC);
