import { z } from "zod";
import { IdSchema, nameWithSeparators, TagSchema } from "./util";
import {
  ConfigGroupName,
  ConfigGroupNameSchema,
  PartialConfigSchema,
} from "./configs";

export const PresetNameSchema = nameWithSeparators().max(16);
export type PresetName = z.infer<typeof PresetNameSchema>;

export const PresetTypeSchema = z.enum(["full", "partial"]);
export type PresetType = z.infer<typeof PresetTypeSchema>;

const PresetSettingsGroupsSchema = z
  .array(ConfigGroupNameSchema)
  .min(1)
  .superRefine((settingList, ctx) => {
    ConfigGroupNameSchema.options.forEach(
      (presetSettingGroup: ConfigGroupName) => {
        const duplicateElemExits: boolean =
          settingList.filter(
            (settingGroup: ConfigGroupName) =>
              settingGroup === presetSettingGroup,
          ).length > 1;
        if (duplicateElemExits) {
          ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: `No duplicates allowed.`,
          });
        }
      },
    );
  });

export const PresetSchema = z.object({
  _id: IdSchema,
  name: PresetNameSchema,
  settingGroups: PresetSettingsGroupsSchema.nullable().optional(),
  config: PartialConfigSchema.extend({
    tags: z.array(TagSchema).optional(),
  }),
});
export type Preset = z.infer<typeof PresetSchema>;

export const EditPresetRequestSchema = PresetSchema.partial({
  config: true,
  settingGroups: true,
});

export type EditPresetRequest = z.infer<typeof EditPresetRequestSchema>;
