-- CreateTable
CREATE TABLE "realtime_stats" (
    "stats_id" BIGSERIAL NOT NULL,
    "stats_date" DATE NOT NULL,
    "daily_planting_count" INTEGER NOT NULL DEFAULT 0,
    "daily_order_count" INTEGER NOT NULL DEFAULT 0,
    "daily_planting_amount" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "daily_new_user_count" INTEGER NOT NULL DEFAULT 0,
    "daily_province_auth_count" INTEGER NOT NULL DEFAULT 0,
    "daily_city_auth_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "realtime_stats_pkey" PRIMARY KEY ("stats_id")
);

-- CreateTable
CREATE TABLE "global_stats" (
    "stats_id" BIGSERIAL NOT NULL,
    "stats_key" VARCHAR(20) NOT NULL DEFAULT 'global',
    "total_planting_count" INTEGER NOT NULL DEFAULT 0,
    "total_order_count" INTEGER NOT NULL DEFAULT 0,
    "total_planting_amount" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "total_user_count" INTEGER NOT NULL DEFAULT 0,
    "total_province_company_count" INTEGER NOT NULL DEFAULT 0,
    "total_city_company_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "global_stats_pkey" PRIMARY KEY ("stats_id")
);

-- CreateIndex
CREATE UNIQUE INDEX "uk_realtime_stats_date" ON "realtime_stats"("stats_date");

-- CreateIndex
CREATE INDEX "idx_rs_date" ON "realtime_stats"("stats_date" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "global_stats_stats_key_key" ON "global_stats"("stats_key");

-- Insert initial global stats record
INSERT INTO "global_stats" ("stats_key", "total_planting_count", "total_order_count", "total_planting_amount", "total_user_count", "total_province_company_count", "total_city_company_count", "updated_at")
VALUES ('global', 0, 0, 0, 0, 0, 0, CURRENT_TIMESTAMP);
