import { type TFunction } from 'i18next';
import { z } from 'zod';
import { type SortableAttachment } from '@material-hu/types/attachments';
import { type IconInterface } from '@material-hu/types/icons';

import { ARTICLE_TITLE_MAX_LENGTH } from './constants';
import { ArticleNotificationType } from './types';

export const articleFormSchema = (t: TFunction) =>
  z.object({
    icon: z.custom<IconInterface>().optional(),
    title: z
      .string()
      .trim()
      .min(1, t('general:required_field'))
      .max(
        ARTICLE_TITLE_MAX_LENGTH,
        t('validations:max_length', { count: ARTICLE_TITLE_MAX_LENGTH }),
      ),
    body: z.string().optional().default(''),
    coverPicture: z
      .object({
        url: z.string().nullable(),
        file: z.custom<File>().nullable(),
      })
      .nullable()
      .optional(),
    showCoverPicture: z.boolean().default(false),
    attachments: z.array(z.custom<SortableAttachment>()).default([]),
    sendNotification: z.boolean().default(false),
    notificationType: z
      .nativeEnum(ArticleNotificationType)
      .nullable()
      .default(null),
  });

export type ArticleFormValues = z.infer<ReturnType<typeof articleFormSchema>>;

export const duplicateArticleSchema = (t: TFunction) =>
  z.object({
    title: z
      .string()
      .trim()
      .min(1, t('general:required_field'))
      .max(
        ARTICLE_TITLE_MAX_LENGTH,
        t('validations:max_length', { count: ARTICLE_TITLE_MAX_LENGTH }),
      ),
    parentId: z.number().nullable(),
  });

export type DuplicateArticleFormValues = z.infer<
  ReturnType<typeof duplicateArticleSchema>
>;
