import { describe, expect, test } from "vitest";
import { emptyLuckyGiftForm } from "@/features/lucky-gift/configModel.js";
import { luckyGiftConfigFormSchema } from "@/features/lucky-gift/schema";

describe("lucky gift config schema", () => {
    test("validates enabled stage probability with backend ppm semantics", () => {
        const form = emptyLuckyGiftForm();
        const normal = form.stages.find((stage: { stage: string }) => stage.stage === "normal");
        if (!normal) {
            throw new Error("normal stage missing");
        }
        normal.tiers[0].enabled = false;

        const result = luckyGiftConfigFormSchema.safeParse(form);

        expect(result.success).toBe(false);
        if (!result.success) {
            expect(result.error.issues.map((issue) => issue.message)).toContain("启用奖档概率合计必须等于 100%");
        }
    });

    test("does not require manually maintained tier ids before submit", () => {
        const form = emptyLuckyGiftForm();
        const advanced = form.stages.find((stage: { stage: string }) => stage.stage === "advanced");
        if (!advanced) {
            throw new Error("advanced stage missing");
        }
        advanced.tiers[0].tierId = "";
        advanced.tiers = [
            ...advanced.tiers,
            {
                ...advanced.tiers[advanced.tiers.length - 1],
                enabled: false,
                multiplier: 10,
                probabilityPercent: 0,
                tierId: "advanced_5x",
            },
        ];

        const result = luckyGiftConfigFormSchema.safeParse(form);

        expect(result.success).toBe(true);
    });
});
