-- CreateTable
CREATE TABLE "leaderboard_rankings" (
    "ranking_id" BIGSERIAL NOT NULL,
    "leaderboard_type" VARCHAR(30) NOT NULL,
    "period_key" VARCHAR(20) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "is_virtual" BOOLEAN NOT NULL DEFAULT false,
    "rank_position" INTEGER NOT NULL,
    "display_position" INTEGER NOT NULL,
    "previous_rank" INTEGER,
    "total_team_planting" INTEGER NOT NULL DEFAULT 0,
    "max_direct_team_planting" INTEGER NOT NULL DEFAULT 0,
    "effective_score" INTEGER NOT NULL DEFAULT 0,
    "user_snapshot" JSONB NOT NULL,
    "period_start_at" TIMESTAMP(3) NOT NULL,
    "period_end_at" TIMESTAMP(3) NOT NULL,
    "calculated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "leaderboard_rankings_pkey" PRIMARY KEY ("ranking_id")
);

-- CreateTable
CREATE TABLE "leaderboard_configs" (
    "config_id" BIGSERIAL NOT NULL,
    "config_key" VARCHAR(50) NOT NULL,
    "daily_enabled" BOOLEAN NOT NULL DEFAULT true,
    "weekly_enabled" BOOLEAN NOT NULL DEFAULT true,
    "monthly_enabled" BOOLEAN NOT NULL DEFAULT true,
    "virtual_ranking_enabled" BOOLEAN NOT NULL DEFAULT false,
    "virtual_account_count" INTEGER NOT NULL DEFAULT 0,
    "display_limit" INTEGER NOT NULL DEFAULT 30,
    "refresh_interval_minutes" INTEGER NOT NULL DEFAULT 5,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "leaderboard_configs_pkey" PRIMARY KEY ("config_id")
);

-- CreateTable
CREATE TABLE "virtual_accounts" (
    "virtual_account_id" BIGSERIAL NOT NULL,
    "account_type" VARCHAR(30) NOT NULL,
    "display_name" VARCHAR(100) NOT NULL,
    "avatar" VARCHAR(255),
    "province_code" VARCHAR(10),
    "city_code" VARCHAR(10),
    "min_score" INTEGER,
    "max_score" INTEGER,
    "current_score" INTEGER NOT NULL DEFAULT 0,
    "usdt_balance" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "hashpower_balance" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "virtual_accounts_pkey" PRIMARY KEY ("virtual_account_id")
);

-- CreateTable
CREATE TABLE "virtual_ranking_entries" (
    "entry_id" BIGSERIAL NOT NULL,
    "virtual_account_id" BIGINT NOT NULL,
    "leaderboard_type" VARCHAR(30) NOT NULL,
    "period_key" VARCHAR(20) NOT NULL,
    "display_position" INTEGER NOT NULL,
    "generated_score" INTEGER NOT NULL,
    "display_name" VARCHAR(100) NOT NULL,
    "avatar" VARCHAR(255),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "virtual_ranking_entries_pkey" PRIMARY KEY ("entry_id")
);

-- CreateTable
CREATE TABLE "leaderboard_snapshots" (
    "snapshot_id" BIGSERIAL NOT NULL,
    "leaderboard_type" VARCHAR(30) NOT NULL,
    "period_key" VARCHAR(20) NOT NULL,
    "rankings_data" JSONB NOT NULL,
    "total_participants" INTEGER NOT NULL,
    "top_score" INTEGER NOT NULL,
    "average_score" INTEGER NOT NULL,
    "period_start_at" TIMESTAMP(3) NOT NULL,
    "period_end_at" TIMESTAMP(3) NOT NULL,
    "snapshot_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "leaderboard_snapshots_pkey" PRIMARY KEY ("snapshot_id")
);

-- CreateTable
CREATE TABLE "virtual_account_transactions" (
    "transaction_id" BIGSERIAL NOT NULL,
    "virtual_account_id" BIGINT NOT NULL,
    "transaction_type" VARCHAR(30) NOT NULL,
    "amount" DECIMAL(20,8) NOT NULL,
    "currency" VARCHAR(10) NOT NULL,
    "source_type" VARCHAR(50),
    "source_id" VARCHAR(100),
    "source_user_id" BIGINT,
    "memo" VARCHAR(500),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "virtual_account_transactions_pkey" PRIMARY KEY ("transaction_id")
);

-- CreateTable
CREATE TABLE "leaderboard_events" (
    "event_id" BIGSERIAL NOT NULL,
    "event_type" VARCHAR(50) NOT NULL,
    "aggregate_id" VARCHAR(100) NOT NULL,
    "aggregate_type" VARCHAR(50) NOT NULL,
    "event_data" JSONB NOT NULL,
    "user_id" BIGINT,
    "occurred_at" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "version" INTEGER NOT NULL DEFAULT 1,

    CONSTRAINT "leaderboard_events_pkey" PRIMARY KEY ("event_id")
);

-- CreateIndex
CREATE UNIQUE INDEX "uk_type_period_user" ON "leaderboard_rankings"("leaderboard_type", "period_key", "user_id");

-- CreateIndex
CREATE INDEX "idx_display_rank" ON "leaderboard_rankings"("leaderboard_type", "period_key", "display_position");

-- CreateIndex
CREATE INDEX "idx_score" ON "leaderboard_rankings"("leaderboard_type", "period_key", "effective_score" DESC);

-- CreateIndex
CREATE INDEX "idx_ranking_user" ON "leaderboard_rankings"("user_id");

-- CreateIndex
CREATE INDEX "idx_period" ON "leaderboard_rankings"("period_key");

-- CreateIndex
CREATE INDEX "idx_virtual" ON "leaderboard_rankings"("is_virtual");

-- CreateIndex
CREATE UNIQUE INDEX "leaderboard_configs_config_key_key" ON "leaderboard_configs"("config_key");

-- CreateIndex
CREATE INDEX "idx_va_type" ON "virtual_accounts"("account_type");

-- CreateIndex
CREATE INDEX "idx_va_province" ON "virtual_accounts"("province_code");

-- CreateIndex
CREATE INDEX "idx_va_city" ON "virtual_accounts"("city_code");

-- CreateIndex
CREATE INDEX "idx_va_active" ON "virtual_accounts"("is_active");

-- CreateIndex
CREATE UNIQUE INDEX "uk_vr_type_period_pos" ON "virtual_ranking_entries"("leaderboard_type", "period_key", "display_position");

-- CreateIndex
CREATE INDEX "idx_vr_va" ON "virtual_ranking_entries"("virtual_account_id");

-- CreateIndex
CREATE INDEX "idx_vr_type_period" ON "virtual_ranking_entries"("leaderboard_type", "period_key");

-- CreateIndex
CREATE UNIQUE INDEX "uk_snapshot_type_period" ON "leaderboard_snapshots"("leaderboard_type", "period_key");

-- CreateIndex
CREATE INDEX "idx_snapshot_type" ON "leaderboard_snapshots"("leaderboard_type");

-- CreateIndex
CREATE INDEX "idx_snapshot_period" ON "leaderboard_snapshots"("period_key");

-- CreateIndex
CREATE INDEX "idx_vat_account" ON "virtual_account_transactions"("virtual_account_id");

-- CreateIndex
CREATE INDEX "idx_vat_type" ON "virtual_account_transactions"("transaction_type");

-- CreateIndex
CREATE INDEX "idx_vat_created" ON "virtual_account_transactions"("created_at" DESC);

-- CreateIndex
CREATE INDEX "idx_lb_event_aggregate" ON "leaderboard_events"("aggregate_type", "aggregate_id");

-- CreateIndex
CREATE INDEX "idx_lb_event_type" ON "leaderboard_events"("event_type");

-- CreateIndex
CREATE INDEX "idx_lb_event_occurred" ON "leaderboard_events"("occurred_at");

-- Insert default config
INSERT INTO "leaderboard_configs" ("config_key", "daily_enabled", "weekly_enabled", "monthly_enabled", "virtual_ranking_enabled", "virtual_account_count", "display_limit", "refresh_interval_minutes", "updated_at")
VALUES ('GLOBAL', true, true, true, false, 0, 30, 5, CURRENT_TIMESTAMP);
