-- CreateTable: 预种 CDC 同步追踪 — 订单
CREATE TABLE "pre_planting_synced_orders" (
    "id" BIGSERIAL NOT NULL,
    "original_order_id" BIGINT NOT NULL,
    "order_no" VARCHAR(50) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "portion_count" INTEGER NOT NULL,
    "price_per_portion" DECIMAL(20,8) NOT NULL,
    "total_amount" DECIMAL(20,8) NOT NULL,
    "province_code" VARCHAR(10) NOT NULL,
    "city_code" VARCHAR(10) NOT NULL,
    "status" VARCHAR(20) NOT NULL,
    "merged_to_merge_id" BIGINT,
    "paid_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL,
    "contribution_per_portion" DECIMAL(20,10) NOT NULL,
    "contribution_distributed" BOOLEAN NOT NULL DEFAULT false,
    "contribution_distributed_at" TIMESTAMP(3),
    "source_topic" VARCHAR(200) NOT NULL,
    "source_offset" BIGINT NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable: 预种 CDC 同步追踪 — 持仓
CREATE TABLE "pre_planting_synced_positions" (
    "id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "total_portions" INTEGER NOT NULL DEFAULT 0,
    "merged_portions" INTEGER NOT NULL DEFAULT 0,
    "total_trees_merged" INTEGER NOT NULL DEFAULT 0,
    "first_purchase_at" TIMESTAMP(3),
    "source_topic" VARCHAR(200) NOT NULL,
    "source_offset" BIGINT NOT NULL,
    "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable: 预种算力冻结状态
CREATE TABLE "pre_planting_freeze_states" (
    "id" BIGSERIAL NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "total_portions" INTEGER NOT NULL DEFAULT 0,
    "total_trees_merged" INTEGER NOT NULL DEFAULT 0,
    "first_purchase_at" TIMESTAMP(3),
    "is_frozen" BOOLEAN NOT NULL DEFAULT false,
    "frozen_at" TIMESTAMP(3),
    "unfrozen_at" TIMESTAMP(3),
    "post_unfreeze_expire_date" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: 预种 CDC 幂等性追踪
CREATE TABLE "pre_planting_processed_cdc_events" (
    "id" BIGSERIAL NOT NULL,
    "source_topic" VARCHAR(200) NOT NULL,
    "offset" BIGINT NOT NULL,
    "table_name" VARCHAR(100) NOT NULL,
    "operation" VARCHAR(10) NOT NULL,
    "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "pre_planting_synced_orders_original_order_id_key" ON "pre_planting_synced_orders"("original_order_id");
CREATE INDEX "pre_planting_synced_orders_account_sequence_idx" ON "pre_planting_synced_orders"("account_sequence");
CREATE INDEX "pre_planting_synced_orders_status_idx" ON "pre_planting_synced_orders"("status");
CREATE INDEX "pre_planting_synced_orders_contribution_distributed_idx" ON "pre_planting_synced_orders"("contribution_distributed");

-- CreateIndex
CREATE UNIQUE INDEX "pre_planting_synced_positions_user_id_key" ON "pre_planting_synced_positions"("user_id");
CREATE UNIQUE INDEX "pre_planting_synced_positions_account_sequence_key" ON "pre_planting_synced_positions"("account_sequence");

-- CreateIndex
CREATE UNIQUE INDEX "pre_planting_freeze_states_account_sequence_key" ON "pre_planting_freeze_states"("account_sequence");
CREATE INDEX "pre_planting_freeze_states_is_frozen_idx" ON "pre_planting_freeze_states"("is_frozen");
CREATE INDEX "pre_planting_freeze_states_first_purchase_at_idx" ON "pre_planting_freeze_states"("first_purchase_at");

-- CreateIndex
CREATE UNIQUE INDEX "pre_planting_processed_cdc_events_source_topic_offset_key" ON "pre_planting_processed_cdc_events"("source_topic", "offset");
CREATE INDEX "pre_planting_processed_cdc_events_processed_at_idx" ON "pre_planting_processed_cdc_events"("processed_at");
