-- ============================================================================
-- mining-admin-service 初始化 migration
-- 合并自: 0001_init, 0002_fix_processed_event_composite_key,
--         20250120000001_add_region_to_synced_system_contributions,
--         20250120000002_add_synced_system_contribution_records
-- 注意: 使用 Prisma relationMode = "prisma"，不在数据库层创建FK约束
-- ============================================================================

-- CreateTable
CREATE TABLE "admin_users" (
    "id" TEXT NOT NULL,
    "username" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "role" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "lastLoginAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "system_configs" (
    "id" TEXT NOT NULL,
    "category" TEXT NOT NULL,
    "key" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "description" TEXT,
    "isPublic" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "system_accounts" (
    "id" TEXT NOT NULL,
    "accountType" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "initialization_records" (
    "id" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "status" TEXT NOT NULL,
    "config" JSONB NOT NULL,
    "executedBy" TEXT NOT NULL,
    "executedAt" TIMESTAMP(3),
    "errorMessage" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "audit_logs" (
    "id" TEXT NOT NULL,
    "adminId" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "resource" TEXT NOT NULL,
    "resourceId" TEXT,
    "oldValue" JSONB,
    "newValue" JSONB,
    "ipAddress" TEXT,
    "userAgent" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "daily_reports" (
    "id" TEXT NOT NULL,
    "reportDate" DATE NOT NULL,
    "totalUsers" INTEGER NOT NULL DEFAULT 0,
    "newUsers" INTEGER NOT NULL DEFAULT 0,
    "activeUsers" INTEGER NOT NULL DEFAULT 0,
    "totalAdoptions" INTEGER NOT NULL DEFAULT 0,
    "newAdoptions" INTEGER NOT NULL DEFAULT 0,
    "totalTrees" INTEGER NOT NULL DEFAULT 0,
    "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "contributionGrowth" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalDistributed" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalBurned" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "tradingVolume" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "tradingAmount" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "tradeCount" INTEGER NOT NULL DEFAULT 0,
    "openPrice" DECIMAL(30,18) NOT NULL DEFAULT 1,
    "closePrice" DECIMAL(30,18) NOT NULL DEFAULT 1,
    "highPrice" DECIMAL(30,18) NOT NULL DEFAULT 1,
    "lowPrice" DECIMAL(30,18) NOT NULL DEFAULT 1,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "synced_users" (
    "id" TEXT NOT NULL,
    "originalUserId" TEXT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "phone" TEXT NOT NULL,
    "nickname" TEXT,
    "status" TEXT NOT NULL,
    "kycStatus" TEXT NOT NULL,
    "realName" TEXT,
    "isLegacyUser" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_referrals" (
    "id" TEXT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "referrerAccountSequence" TEXT,
    "referrerUserId" BIGINT,
    "originalUserId" BIGINT,
    "ancestorPath" TEXT,
    "depth" INTEGER NOT NULL DEFAULT 0,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_adoptions" (
    "id" TEXT NOT NULL,
    "originalAdoptionId" BIGINT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "treeCount" INTEGER NOT NULL,
    "adoptionDate" DATE NOT NULL,
    "status" TEXT,
    "contributionPerTree" DECIMAL(20,10) NOT NULL,
    "distributionSummary" TEXT,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_contribution_accounts" (
    "id" TEXT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "personalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "teamLevelContribution" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "teamBonusContribution" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "effectiveContribution" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "hasAdopted" BOOLEAN NOT NULL DEFAULT false,
    "directReferralCount" INTEGER NOT NULL DEFAULT 0,
    "unlockedLevelDepth" INTEGER NOT NULL DEFAULT 0,
    "unlockedBonusTiers" INTEGER NOT NULL DEFAULT 0,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_contribution_records" (
    "id" TEXT NOT NULL,
    "originalRecordId" BIGINT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "sourceType" TEXT NOT NULL,
    "sourceAdoptionId" BIGINT NOT NULL,
    "sourceAccountSequence" TEXT NOT NULL,
    "treeCount" INTEGER NOT NULL,
    "baseContribution" DECIMAL(20,10) NOT NULL,
    "distributionRate" DECIMAL(10,6) NOT NULL,
    "levelDepth" INTEGER,
    "bonusTier" INTEGER,
    "amount" DECIMAL(30,10) NOT NULL,
    "effectiveDate" DATE NOT NULL,
    "expireDate" DATE NOT NULL,
    "isExpired" BOOLEAN NOT NULL DEFAULT false,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_network_progress" (
    "id" TEXT NOT NULL,
    "totalTreeCount" INTEGER NOT NULL DEFAULT 0,
    "totalAdoptionOrders" INTEGER NOT NULL DEFAULT 0,
    "totalAdoptedUsers" INTEGER NOT NULL DEFAULT 0,
    "currentUnit" INTEGER NOT NULL DEFAULT 0,
    "currentMultiplier" DECIMAL(10,6) NOT NULL DEFAULT 1.0,
    "currentContributionPerTree" DECIMAL(20,10) NOT NULL DEFAULT 22617,
    "nextUnitTreeCount" INTEGER NOT NULL DEFAULT 1000,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_mining_accounts" (
    "id" TEXT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "totalMined" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "availableBalance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozenBalance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_trading_accounts" (
    "id" TEXT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "shareBalance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "cashBalance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozenShares" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozenCash" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalBought" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalSold" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_mining_configs" (
    "id" TEXT NOT NULL,
    "totalShares" DECIMAL(30,8) NOT NULL,
    "distributionPool" DECIMAL(30,8) NOT NULL,
    "remainingDistribution" DECIMAL(30,8) NOT NULL,
    "halvingPeriodYears" INTEGER NOT NULL,
    "currentEra" INTEGER NOT NULL DEFAULT 1,
    "minuteDistribution" DECIMAL(30,8) NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT false,
    "activatedAt" TIMESTAMP(3),
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_daily_mining_stats" (
    "id" TEXT NOT NULL,
    "statDate" DATE NOT NULL,
    "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalDistributed" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalBurned" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "participantCount" INTEGER NOT NULL DEFAULT 0,
    "avgContributionRate" DECIMAL(30,18) NOT NULL DEFAULT 0,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_day_klines" (
    "id" TEXT NOT NULL,
    "klineDate" DATE NOT NULL,
    "open" DECIMAL(30,18) NOT NULL,
    "high" DECIMAL(30,18) NOT NULL,
    "low" DECIMAL(30,18) NOT NULL,
    "close" DECIMAL(30,18) NOT NULL,
    "volume" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "amount" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "tradeCount" INTEGER NOT NULL DEFAULT 0,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_circulation_pools" (
    "id" TEXT NOT NULL,
    "totalShares" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalCash" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: 系统账户算力 (from contribution-service)
CREATE TABLE "synced_system_contributions" (
    "id" TEXT NOT NULL,
    "accountType" TEXT NOT NULL,
    "region_code" TEXT,
    "name" TEXT NOT NULL,
    "contributionBalance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "contributionNeverExpires" BOOLEAN NOT NULL DEFAULT false,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "cdc_sync_progress" (
    "id" TEXT NOT NULL,
    "sourceTopic" TEXT NOT NULL,
    "sourceService" TEXT NOT NULL,
    "lastOffset" TEXT,
    "lastSequenceNum" BIGINT NOT NULL DEFAULT 0,
    "lastSyncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "errorCount" INTEGER NOT NULL DEFAULT 0,
    "lastError" TEXT,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "processed_events" (
    "id" TEXT NOT NULL,
    "eventId" TEXT NOT NULL,
    "eventType" TEXT NOT NULL,
    "sourceService" TEXT NOT NULL,
    "processedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "synced_provinces" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_cities" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "province_id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_user_region_mappings" (
    "id" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "city_id" TEXT NOT NULL,
    "assigned_at" TIMESTAMP(3) NOT NULL,
    "assigned_by" TEXT,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_wallet_system_accounts" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "account_type" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "province_id" TEXT,
    "city_id" TEXT,
    "share_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "usdt_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "green_point_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozen_share" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozen_usdt" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "blockchain_address" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_wallet_pool_accounts" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "pool_type" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "target_burn" DECIMAL(30,8),
    "remaining_burn" DECIMAL(30,8),
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_user_wallets" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "wallet_type" TEXT NOT NULL,
    "balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozen_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_withdraw_requests" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "request_no" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "asset_type" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "fee" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "net_amount" DECIMAL(30,8) NOT NULL,
    "to_address" TEXT NOT NULL,
    "status" TEXT NOT NULL,
    "tx_hash" TEXT,
    "block_number" BIGINT,
    "confirmations" INTEGER NOT NULL DEFAULT 0,
    "error_message" TEXT,
    "approved_by" TEXT,
    "approved_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL,
    "completed_at" TIMESTAMP(3),
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_deposit_records" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "tx_hash" TEXT NOT NULL,
    "from_address" TEXT NOT NULL,
    "to_address" TEXT NOT NULL,
    "asset_type" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "block_number" BIGINT NOT NULL,
    "confirmations" INTEGER NOT NULL DEFAULT 0,
    "matched_account_seq" TEXT,
    "is_processed" BOOLEAN NOT NULL DEFAULT false,
    "processed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_dex_swap_records" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "swap_no" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "from_asset" TEXT NOT NULL,
    "to_asset" TEXT NOT NULL,
    "from_amount" DECIMAL(30,8) NOT NULL,
    "to_amount" DECIMAL(30,8) NOT NULL,
    "exchange_rate" DECIMAL(30,18) NOT NULL,
    "slippage" DECIMAL(10,4) NOT NULL DEFAULT 0,
    "fee" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "status" TEXT NOT NULL,
    "tx_hash" TEXT,
    "block_number" BIGINT,
    "error_message" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL,
    "completed_at" TIMESTAMP(3),
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_blockchain_address_bindings" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "kava_address" TEXT NOT NULL,
    "is_verified" BOOLEAN NOT NULL DEFAULT false,
    "verified_at" TIMESTAMP(3),
    "verification_tx_hash" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_black_hole_contracts" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "contract_address" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "total_burned" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "target_burn" DECIMAL(30,8) NOT NULL,
    "remaining_burn" DECIMAL(30,8) NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "synced_burn_to_black_hole_records" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "black_hole_id" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "source_type" TEXT NOT NULL,
    "source_account_seq" TEXT,
    "source_user_id" TEXT,
    "source_pool_type" TEXT,
    "tx_hash" TEXT,
    "block_number" BIGINT,
    "memo" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "synced_fee_configs" (
    "id" TEXT NOT NULL,
    "original_id" TEXT NOT NULL,
    "fee_type" TEXT NOT NULL,
    "fee_rate" DECIMAL(10,6) NOT NULL,
    "min_fee" DECIMAL(30,8) NOT NULL,
    "max_fee" DECIMAL(30,8),
    "headquarters_rate" DECIMAL(10,6) NOT NULL,
    "operation_rate" DECIMAL(10,6) NOT NULL,
    "province_rate" DECIMAL(10,6) NOT NULL,
    "city_rate" DECIMAL(10,6) NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "admin_users_username_key" ON "admin_users"("username");

-- CreateIndex
CREATE UNIQUE INDEX "system_configs_category_key_key" ON "system_configs"("category", "key");

-- CreateIndex
CREATE UNIQUE INDEX "system_accounts_accountType_key" ON "system_accounts"("accountType");

-- CreateIndex
CREATE INDEX "audit_logs_adminId_idx" ON "audit_logs"("adminId");

-- CreateIndex
CREATE INDEX "audit_logs_action_idx" ON "audit_logs"("action");

-- CreateIndex
CREATE INDEX "audit_logs_resource_idx" ON "audit_logs"("resource");

-- CreateIndex
CREATE INDEX "audit_logs_createdAt_idx" ON "audit_logs"("createdAt" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "daily_reports_reportDate_key" ON "daily_reports"("reportDate");

-- CreateIndex
CREATE UNIQUE INDEX "synced_users_originalUserId_key" ON "synced_users"("originalUserId");

-- CreateIndex
CREATE UNIQUE INDEX "synced_users_accountSequence_key" ON "synced_users"("accountSequence");

-- CreateIndex
CREATE INDEX "synced_users_phone_idx" ON "synced_users"("phone");

-- CreateIndex
CREATE INDEX "synced_users_status_idx" ON "synced_users"("status");

-- CreateIndex
CREATE INDEX "synced_users_kycStatus_idx" ON "synced_users"("kycStatus");

-- CreateIndex
CREATE INDEX "synced_users_createdAt_idx" ON "synced_users"("createdAt" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "synced_referrals_accountSequence_key" ON "synced_referrals"("accountSequence");

-- CreateIndex
CREATE INDEX "synced_referrals_referrerAccountSequence_idx" ON "synced_referrals"("referrerAccountSequence");

-- CreateIndex
CREATE INDEX "synced_referrals_depth_idx" ON "synced_referrals"("depth");

-- CreateIndex
CREATE INDEX "synced_referrals_accountSequence_idx" ON "synced_referrals"("accountSequence");

-- CreateIndex
CREATE UNIQUE INDEX "synced_adoptions_originalAdoptionId_key" ON "synced_adoptions"("originalAdoptionId");

-- CreateIndex
CREATE INDEX "synced_adoptions_accountSequence_idx" ON "synced_adoptions"("accountSequence");

-- CreateIndex
CREATE INDEX "synced_adoptions_adoptionDate_idx" ON "synced_adoptions"("adoptionDate");

-- CreateIndex
CREATE UNIQUE INDEX "synced_contribution_accounts_accountSequence_key" ON "synced_contribution_accounts"("accountSequence");

-- CreateIndex
CREATE INDEX "synced_contribution_accounts_accountSequence_idx" ON "synced_contribution_accounts"("accountSequence");

-- CreateIndex
CREATE UNIQUE INDEX "synced_contribution_records_originalRecordId_key" ON "synced_contribution_records"("originalRecordId");

-- CreateIndex
CREATE INDEX "synced_contribution_records_accountSequence_idx" ON "synced_contribution_records"("accountSequence");

-- CreateIndex
CREATE INDEX "synced_contribution_records_sourceAccountSequence_idx" ON "synced_contribution_records"("sourceAccountSequence");

-- CreateIndex
CREATE INDEX "synced_contribution_records_sourceAdoptionId_idx" ON "synced_contribution_records"("sourceAdoptionId");

-- CreateIndex
CREATE INDEX "synced_contribution_records_sourceType_idx" ON "synced_contribution_records"("sourceType");

-- CreateIndex
CREATE INDEX "synced_contribution_records_createdAt_idx" ON "synced_contribution_records"("createdAt" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "synced_mining_accounts_accountSequence_key" ON "synced_mining_accounts"("accountSequence");

-- CreateIndex
CREATE INDEX "synced_mining_accounts_accountSequence_idx" ON "synced_mining_accounts"("accountSequence");

-- CreateIndex
CREATE UNIQUE INDEX "synced_trading_accounts_accountSequence_key" ON "synced_trading_accounts"("accountSequence");

-- CreateIndex
CREATE INDEX "synced_trading_accounts_accountSequence_idx" ON "synced_trading_accounts"("accountSequence");

-- CreateIndex
CREATE UNIQUE INDEX "synced_daily_mining_stats_statDate_key" ON "synced_daily_mining_stats"("statDate");

-- CreateIndex
CREATE UNIQUE INDEX "synced_day_klines_klineDate_key" ON "synced_day_klines"("klineDate");

-- CreateIndex: synced_system_contributions
-- 使用 accountType + COALESCE(region_code, '__NULL__') 复合唯一键
-- 注意：PostgreSQL 中 NULL != NULL，所以直接用 region_code 做唯一索引无法阻止重复的 (OPERATION, NULL)
CREATE UNIQUE INDEX "synced_system_contributions_accountType_region_code_key" ON "synced_system_contributions"("accountType", COALESCE(region_code, '__NULL__'));
CREATE INDEX "synced_system_contributions_accountType_idx" ON "synced_system_contributions"("accountType");
CREATE INDEX "synced_system_contributions_region_code_idx" ON "synced_system_contributions"("region_code");

-- CreateIndex
CREATE UNIQUE INDEX "cdc_sync_progress_sourceTopic_key" ON "cdc_sync_progress"("sourceTopic");

-- CreateIndex
CREATE INDEX "cdc_sync_progress_sourceService_idx" ON "cdc_sync_progress"("sourceService");

-- CreateIndex (使用复合唯一键替代单独的 eventId 唯一约束)
CREATE UNIQUE INDEX "processed_events_sourceService_eventId_key" ON "processed_events"("sourceService", "eventId");

-- CreateIndex
CREATE INDEX "processed_events_processedAt_idx" ON "processed_events"("processedAt");

-- CreateIndex
CREATE UNIQUE INDEX "synced_provinces_original_id_key" ON "synced_provinces"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_provinces_code_key" ON "synced_provinces"("code");

-- CreateIndex
CREATE INDEX "synced_provinces_code_idx" ON "synced_provinces"("code");

-- CreateIndex
CREATE UNIQUE INDEX "synced_cities_original_id_key" ON "synced_cities"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_cities_code_key" ON "synced_cities"("code");

-- CreateIndex
CREATE INDEX "synced_cities_province_id_idx" ON "synced_cities"("province_id");

-- CreateIndex
CREATE INDEX "synced_cities_code_idx" ON "synced_cities"("code");

-- CreateIndex
CREATE UNIQUE INDEX "synced_user_region_mappings_account_sequence_key" ON "synced_user_region_mappings"("account_sequence");

-- CreateIndex
CREATE INDEX "synced_user_region_mappings_city_id_idx" ON "synced_user_region_mappings"("city_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_wallet_system_accounts_original_id_key" ON "synced_wallet_system_accounts"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_wallet_system_accounts_code_key" ON "synced_wallet_system_accounts"("code");

-- CreateIndex
CREATE INDEX "synced_wallet_system_accounts_account_type_idx" ON "synced_wallet_system_accounts"("account_type");

-- CreateIndex
CREATE INDEX "synced_wallet_system_accounts_province_id_idx" ON "synced_wallet_system_accounts"("province_id");

-- CreateIndex
CREATE INDEX "synced_wallet_system_accounts_city_id_idx" ON "synced_wallet_system_accounts"("city_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_wallet_pool_accounts_original_id_key" ON "synced_wallet_pool_accounts"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_wallet_pool_accounts_pool_type_key" ON "synced_wallet_pool_accounts"("pool_type");

-- CreateIndex
CREATE INDEX "synced_wallet_pool_accounts_pool_type_idx" ON "synced_wallet_pool_accounts"("pool_type");

-- CreateIndex
CREATE UNIQUE INDEX "synced_user_wallets_original_id_key" ON "synced_user_wallets"("original_id");

-- CreateIndex
CREATE INDEX "synced_user_wallets_account_sequence_idx" ON "synced_user_wallets"("account_sequence");

-- CreateIndex
CREATE INDEX "synced_user_wallets_wallet_type_idx" ON "synced_user_wallets"("wallet_type");

-- CreateIndex
CREATE UNIQUE INDEX "synced_user_wallets_account_sequence_wallet_type_key" ON "synced_user_wallets"("account_sequence", "wallet_type");

-- CreateIndex
CREATE UNIQUE INDEX "synced_withdraw_requests_original_id_key" ON "synced_withdraw_requests"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_withdraw_requests_request_no_key" ON "synced_withdraw_requests"("request_no");

-- CreateIndex
CREATE INDEX "synced_withdraw_requests_account_sequence_idx" ON "synced_withdraw_requests"("account_sequence");

-- CreateIndex
CREATE INDEX "synced_withdraw_requests_status_idx" ON "synced_withdraw_requests"("status");

-- CreateIndex
CREATE INDEX "synced_withdraw_requests_tx_hash_idx" ON "synced_withdraw_requests"("tx_hash");

-- CreateIndex
CREATE INDEX "synced_withdraw_requests_created_at_idx" ON "synced_withdraw_requests"("created_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "synced_deposit_records_original_id_key" ON "synced_deposit_records"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_deposit_records_tx_hash_key" ON "synced_deposit_records"("tx_hash");

-- CreateIndex
CREATE INDEX "synced_deposit_records_from_address_idx" ON "synced_deposit_records"("from_address");

-- CreateIndex
CREATE INDEX "synced_deposit_records_to_address_idx" ON "synced_deposit_records"("to_address");

-- CreateIndex
CREATE INDEX "synced_deposit_records_matched_account_seq_idx" ON "synced_deposit_records"("matched_account_seq");

-- CreateIndex
CREATE INDEX "synced_deposit_records_is_processed_idx" ON "synced_deposit_records"("is_processed");

-- CreateIndex
CREATE UNIQUE INDEX "synced_dex_swap_records_original_id_key" ON "synced_dex_swap_records"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_dex_swap_records_swap_no_key" ON "synced_dex_swap_records"("swap_no");

-- CreateIndex
CREATE INDEX "synced_dex_swap_records_account_sequence_idx" ON "synced_dex_swap_records"("account_sequence");

-- CreateIndex
CREATE INDEX "synced_dex_swap_records_status_idx" ON "synced_dex_swap_records"("status");

-- CreateIndex
CREATE INDEX "synced_dex_swap_records_tx_hash_idx" ON "synced_dex_swap_records"("tx_hash");

-- CreateIndex
CREATE UNIQUE INDEX "synced_blockchain_address_bindings_original_id_key" ON "synced_blockchain_address_bindings"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_blockchain_address_bindings_account_sequence_key" ON "synced_blockchain_address_bindings"("account_sequence");

-- CreateIndex
CREATE UNIQUE INDEX "synced_blockchain_address_bindings_kava_address_key" ON "synced_blockchain_address_bindings"("kava_address");

-- CreateIndex
CREATE INDEX "synced_blockchain_address_bindings_kava_address_idx" ON "synced_blockchain_address_bindings"("kava_address");

-- CreateIndex
CREATE UNIQUE INDEX "synced_black_hole_contracts_original_id_key" ON "synced_black_hole_contracts"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_black_hole_contracts_contract_address_key" ON "synced_black_hole_contracts"("contract_address");

-- CreateIndex
CREATE UNIQUE INDEX "synced_burn_to_black_hole_records_original_id_key" ON "synced_burn_to_black_hole_records"("original_id");

-- CreateIndex
CREATE INDEX "synced_burn_to_black_hole_records_black_hole_id_idx" ON "synced_burn_to_black_hole_records"("black_hole_id");

-- CreateIndex
CREATE INDEX "synced_burn_to_black_hole_records_source_account_seq_idx" ON "synced_burn_to_black_hole_records"("source_account_seq");

-- CreateIndex
CREATE INDEX "synced_burn_to_black_hole_records_tx_hash_idx" ON "synced_burn_to_black_hole_records"("tx_hash");

-- CreateIndex
CREATE INDEX "synced_burn_to_black_hole_records_created_at_idx" ON "synced_burn_to_black_hole_records"("created_at" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "synced_fee_configs_original_id_key" ON "synced_fee_configs"("original_id");

-- CreateIndex
CREATE UNIQUE INDEX "synced_fee_configs_fee_type_key" ON "synced_fee_configs"("fee_type");

-- AddForeignKey (保留 admin 相关的外键)
ALTER TABLE "audit_logs" ADD CONSTRAINT "audit_logs_adminId_fkey" FOREIGN KEY ("adminId") REFERENCES "admin_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- ============================================================================
-- 系统账户算力明细同步表
-- 用于存储从 contribution-service 同步的系统账户算力来源明细
-- ============================================================================

-- CreateTable: 系统账户算力明细 (from contribution-service)
CREATE TABLE "synced_system_contribution_records" (
    "id" TEXT NOT NULL,
    "original_record_id" BIGINT NOT NULL,
    "account_type" TEXT NOT NULL,
    "region_code" TEXT,
    "source_adoption_id" BIGINT NOT NULL,
    "source_account_sequence" TEXT NOT NULL,
    -- 来源类型: FIXED_RATE(固定比例) / LEVEL_OVERFLOW(层级溢出) / LEVEL_NO_ANCESTOR(无上线) / BONUS_TIER_1/2/3(团队奖励未解锁)
    "source_type" VARCHAR(30) NOT NULL,
    -- 层级深度（1-15），仅对 LEVEL_OVERFLOW 和 LEVEL_NO_ANCESTOR 类型有效
    "level_depth" INTEGER,
    "distribution_rate" DECIMAL(10,6) NOT NULL,
    "amount" DECIMAL(30,10) NOT NULL,
    "effective_date" DATE NOT NULL,
    "expire_date" DATE,
    "is_expired" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL,
    "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "synced_system_contribution_records_original_record_id_key" ON "synced_system_contribution_records"("original_record_id");
CREATE INDEX "synced_system_contribution_records_account_type_region_code_idx" ON "synced_system_contribution_records"("account_type", "region_code");
CREATE INDEX "synced_system_contribution_records_source_adoption_id_idx" ON "synced_system_contribution_records"("source_adoption_id");
CREATE INDEX "synced_system_contribution_records_source_account_sequence_idx" ON "synced_system_contribution_records"("source_account_sequence");
CREATE INDEX "synced_system_contribution_records_source_type_idx" ON "synced_system_contribution_records"("source_type");
CREATE INDEX "synced_system_contribution_records_created_at_idx" ON "synced_system_contribution_records"("created_at" DESC);
