-- CreateTable
CREATE TABLE "analytics_event_log" (
    "id" BIGSERIAL NOT NULL,
    "user_id" VARCHAR(20),
    "install_id" VARCHAR(64) NOT NULL,
    "event_name" VARCHAR(64) NOT NULL,
    "event_time" TIMESTAMPTZ NOT NULL,
    "properties" JSONB,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "analytics_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 "analytics_daily_active_users_pkey" PRIMARY KEY ("day")
);

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

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

-- CreateIndex
CREATE INDEX "idx_event_log_event_time" ON "analytics_event_log"("event_time");

-- CreateIndex
CREATE INDEX "idx_event_log_event_name" ON "analytics_event_log"("event_name");

-- CreateIndex
CREATE INDEX "idx_event_log_event_name_time" ON "analytics_event_log"("event_name", "event_time");

-- CreateIndex
CREATE INDEX "idx_event_log_user_id" ON "analytics_event_log"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "analytics_online_snapshots_ts_key" ON "analytics_online_snapshots"("ts");

-- CreateIndex
CREATE INDEX "idx_online_snapshots_ts" ON "analytics_online_snapshots"("ts" DESC);
