-- CreateTable: 认种树定价配置
-- 基础价 15831 USDT 不变，supplement 作为加价全额归总部 (S0000000001)
-- 涨价原因：总部运营成本压力
CREATE TABLE "tree_pricing_configs" (
    "id" TEXT NOT NULL,
    "current_supplement" INTEGER NOT NULL DEFAULT 0,
    "auto_increase_enabled" BOOLEAN NOT NULL DEFAULT false,
    "auto_increase_amount" INTEGER NOT NULL DEFAULT 0,
    "auto_increase_interval_days" INTEGER NOT NULL DEFAULT 0,
    "last_auto_increase_at" TIMESTAMP(3),
    "next_auto_increase_at" TIMESTAMP(3),
    "updated_at" TIMESTAMP(3) NOT NULL,
    "updated_by" VARCHAR(50),

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

-- 插入默认配置（加价为0，自动涨价关闭）
INSERT INTO "tree_pricing_configs" ("id", "current_supplement", "auto_increase_enabled", "updated_at")
VALUES (gen_random_uuid(), 0, false, NOW());

-- CreateTable: 认种树价格变更审计日志
-- 每次价格变更（手动或自动）都会记录一条不可修改的日志，用于审计追踪
CREATE TYPE "PriceChangeType" AS ENUM ('MANUAL', 'AUTO');

CREATE TABLE "tree_price_change_logs" (
    "id" TEXT NOT NULL,
    "change_type" "PriceChangeType" NOT NULL,
    "previous_supplement" INTEGER NOT NULL,
    "new_supplement" INTEGER NOT NULL,
    "change_amount" INTEGER NOT NULL,
    "reason" VARCHAR(500),
    "operator_id" VARCHAR(50),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "tree_price_change_logs_created_at_idx" ON "tree_price_change_logs"("created_at");
CREATE INDEX "tree_price_change_logs_change_type_idx" ON "tree_price_change_logs"("change_type");
