-- CreateTable: 预种订单表
CREATE TABLE "pre_planting_orders" (
    "order_id" BIGSERIAL NOT NULL,
    "order_no" VARCHAR(50) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "portion_count" INTEGER NOT NULL DEFAULT 1,
    "price_per_portion" DECIMAL(20,8) NOT NULL DEFAULT 3171,
    "total_amount" DECIMAL(20,8) NOT NULL,
    "province_code" VARCHAR(10) NOT NULL,
    "city_code" VARCHAR(10) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'CREATED',
    "merged_to_merge_id" BIGINT,
    "merged_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "paid_at" TIMESTAMP(3),
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "pre_planting_orders_pkey" PRIMARY KEY ("order_id")
);

-- CreateTable: 预种持仓表（每用户一条）
CREATE TABLE "pre_planting_positions" (
    "position_id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "total_portions" INTEGER NOT NULL DEFAULT 0,
    "available_portions" INTEGER NOT NULL DEFAULT 0,
    "merged_portions" INTEGER NOT NULL DEFAULT 0,
    "total_trees_merged" INTEGER NOT NULL DEFAULT 0,
    "province_code" VARCHAR(10),
    "city_code" VARCHAR(10),
    "first_purchase_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "pre_planting_positions_pkey" PRIMARY KEY ("position_id")
);

-- CreateTable: 预种合并记录表
CREATE TABLE "pre_planting_merges" (
    "merge_id" BIGSERIAL NOT NULL,
    "merge_no" VARCHAR(50) NOT NULL,
    "user_id" BIGINT NOT NULL,
    "account_sequence" VARCHAR(20) NOT NULL,
    "source_order_nos" JSONB NOT NULL,
    "tree_count" INTEGER NOT NULL DEFAULT 1,
    "province_code" VARCHAR(10),
    "city_code" VARCHAR(10),
    "contract_status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "contract_signed_at" TIMESTAMP(3),
    "mining_enabled_at" TIMESTAMP(3),
    "merged_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "pre_planting_merges_pkey" PRIMARY KEY ("merge_id")
);

-- CreateTable: 预种分配记录表
CREATE TABLE "pre_planting_reward_entries" (
    "entry_id" BIGSERIAL NOT NULL,
    "source_order_no" VARCHAR(50) NOT NULL,
    "source_account_sequence" VARCHAR(20) NOT NULL,
    "recipient_account_sequence" VARCHAR(20) NOT NULL,
    "right_type" VARCHAR(50) NOT NULL,
    "usdt_amount" DECIMAL(20,8) NOT NULL,
    "reward_status" VARCHAR(20) NOT NULL DEFAULT 'SETTLED',
    "memo" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "pre_planting_orders_order_no_key" ON "pre_planting_orders"("order_no");
CREATE INDEX "pre_planting_orders_user_id_idx" ON "pre_planting_orders"("user_id");
CREATE INDEX "pre_planting_orders_account_sequence_idx" ON "pre_planting_orders"("account_sequence");
CREATE INDEX "pre_planting_orders_order_no_idx" ON "pre_planting_orders"("order_no");
CREATE INDEX "pre_planting_orders_status_idx" ON "pre_planting_orders"("status");
CREATE INDEX "pre_planting_orders_merged_to_merge_id_idx" ON "pre_planting_orders"("merged_to_merge_id");
CREATE INDEX "pre_planting_orders_created_at_idx" ON "pre_planting_orders"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "pre_planting_positions_user_id_key" ON "pre_planting_positions"("user_id");
CREATE UNIQUE INDEX "pre_planting_positions_account_sequence_key" ON "pre_planting_positions"("account_sequence");
CREATE INDEX "pre_planting_positions_user_id_idx" ON "pre_planting_positions"("user_id");
CREATE INDEX "pre_planting_positions_account_sequence_idx" ON "pre_planting_positions"("account_sequence");
CREATE INDEX "pre_planting_positions_total_trees_merged_idx" ON "pre_planting_positions"("total_trees_merged");

-- CreateIndex
CREATE UNIQUE INDEX "pre_planting_merges_merge_no_key" ON "pre_planting_merges"("merge_no");
CREATE INDEX "pre_planting_merges_user_id_idx" ON "pre_planting_merges"("user_id");
CREATE INDEX "pre_planting_merges_account_sequence_idx" ON "pre_planting_merges"("account_sequence");
CREATE INDEX "pre_planting_merges_merge_no_idx" ON "pre_planting_merges"("merge_no");
CREATE INDEX "pre_planting_merges_contract_status_idx" ON "pre_planting_merges"("contract_status");

-- CreateIndex
CREATE INDEX "pre_planting_reward_entries_source_order_no_idx" ON "pre_planting_reward_entries"("source_order_no");
CREATE INDEX "pre_planting_reward_entries_source_account_sequence_idx" ON "pre_planting_reward_entries"("source_account_sequence");
CREATE INDEX "pre_planting_reward_entries_recipient_account_sequence_idx" ON "pre_planting_reward_entries"("recipient_account_sequence");
CREATE INDEX "pre_planting_reward_entries_right_type_idx" ON "pre_planting_reward_entries"("right_type");
CREATE INDEX "pre_planting_reward_entries_reward_status_idx" ON "pre_planting_reward_entries"("reward_status");
CREATE INDEX "pre_planting_reward_entries_created_at_idx" ON "pre_planting_reward_entries"("created_at");
