import { TFunction } from 'i18next';
import { z } from 'zod';

import { TaskReportType } from 'src/pages/dashboard/serviceManagement/types';

import { serviceItemsSchema, statesSchema, userSchema } from '../types';

export const createCoordinatorFiltersSchema = (t: TFunction) => {
  return z
    .object({
      creationDateFrom: z
        .union([z.date(), z.string(), z.null()])
        .optional()
        .refine(
          date => {
            if (!date) return true;
            const dateObj = date instanceof Date ? date : new Date(date);
            return !isNaN(dateObj.getTime());
          },
          {
            message: t('INVALID_DATE'),
          },
        ),
      creationDateTo: z
        .union([z.date(), z.string(), z.null()])
        .optional()
        .refine(
          date => {
            if (!date) return true;
            const dateObj = date instanceof Date ? date : new Date(date);
            return !isNaN(dateObj.getTime());
          },
          {
            message: t('INVALID_DATE'),
          },
        ),
      dateOptions: z
        .object({
          lastSevenDays: z.boolean().default(false),
          lastFourteenDays: z.boolean().default(false),
          lastThirtyDays: z.boolean().default(false),
          dateRange: z.boolean().default(false),
        })
        .optional(),
    })
    .refine(
      data => {
        // If both dates are provided, to date should be after from date
        if (data.creationDateFrom && data.creationDateTo) {
          const fromDate =
            data.creationDateFrom instanceof Date
              ? data.creationDateFrom
              : new Date(data.creationDateFrom);
          const toDate =
            data.creationDateTo instanceof Date
              ? data.creationDateTo
              : new Date(data.creationDateTo);

          return fromDate <= toDate;
        }
        return true;
      },
      {
        message: t('START_DATE_MUST_BE_BEFORE_END_DATE'),
        path: ['creationDateTo'],
      },
    );
};

const SegmentationSchema = z.object({
  id: z.number(),
  isEditable: z.boolean(),
  items: z.array(z.any()),
  name: z.string(),
  position: z.number().optional(),
  visibility: z.string(),
  amountOfUsers: z.number().nullable().optional(),
  value: z.number().nullable().optional(),
  label: z.string().nullable().optional(),
});

const ProfileFieldSchema = z.object({
  id: z.string(),
  name: z.string(),
  value: z.string().nullable().optional(),
  label: z.string().nullable().optional(),
});

export const downloadTicketsReportSchema = () => {
  return z.object({
    email: z.string().email(),
    includeProfileFields: z.boolean().default(false),
    includeSegmentations: z.boolean().default(false),
    profileFields: z.array(ProfileFieldSchema),
    segmentations: z.array(SegmentationSchema),
    reportType: z
      .nativeEnum(TaskReportType)
      .default(TaskReportType.CONSOLIDATED),
  });
};

export const subtasksFormSchema = (t: TFunction) => {
  return z
    .object({
      statesIn: z.array(statesSchema).optional(),
      parentTaskCatalogItemsIn: z.array(serviceItemsSchema).optional(),
      parentTaskInitiatorsIn: z.array(userSchema).optional(),
      createdAtFrom: z.date().nullable().default(null),
      createdAtTo: z.date().nullable().default(null),
      dateOptions: z
        .object({
          lastSevenDays: z.boolean().default(false),
          lastFourteenDays: z.boolean().default(false),
          lastThirtyDays: z.boolean().default(false),
          dateRange: z.boolean().default(false),
        })
        .optional(),
    })
    .refine(
      data => {
        // If both dates are provided, to date should be after from date
        if (data.createdAtFrom && data.createdAtTo) {
          const fromDate =
            data.createdAtFrom instanceof Date
              ? data.createdAtFrom
              : new Date(data.createdAtFrom);
          const toDate =
            data.createdAtTo instanceof Date
              ? data.createdAtTo
              : new Date(data.createdAtTo);

          return fromDate <= toDate;
        }
        return true;
      },
      {
        message: t('start_date_must_be_before_end_date'),
        path: ['createdAtTo'],
      },
    );
};
