import { z } from "zod";

const resourceGroupIdInput = z.union([z.string(), z.number()]);

export const sevenDayCheckInConfigFormSchema = z
    .object({
        enabled: z.boolean(),
        rewards: z
            .array(
                z.object({
                    dayIndex: z.number().int().min(1).max(7),
                    resourceGroupId: resourceGroupIdInput,
                }),
            )
            .length(7, "必须配置 7 天奖励"),
    })
    .superRefine((value, context) => {
        const seen = new Set<number>();
        for (const reward of value.rewards) {
            if (seen.has(reward.dayIndex)) {
                context.addIssue({ code: "custom", message: "签到天数不能重复", path: ["rewards"] });
            }
            seen.add(reward.dayIndex);
            const resourceGroupId = Number(reward.resourceGroupId || 0);
            if (!Number.isInteger(resourceGroupId) || resourceGroupId <= 0) {
                context.addIssue({
                    code: "custom",
                    message: `第 ${reward.dayIndex} 天必须选择资源组`,
                    path: ["rewards", reward.dayIndex - 1, "resourceGroupId"],
                });
            }
        }
    });

export type SevenDayCheckInConfigForm = z.infer<typeof sevenDayCheckInConfigFormSchema>;
