import i18next from 'i18next';
import { isEqual } from 'lodash-es';

import { type RulesOptions } from 'src/hooks/useRules';

import { PathSteps } from '../constants';
import {
  type NewPathFormValues,
  type PathBasicInformation,
  type PathConfiguration,
  type PathDetail,
  type PathStepsType,
} from '../types';
import { fromNotificationChannel } from '../utils';

const { BASIC_INFORMATION, CONTENT, CONFIGURATION, SEGMENTATION, REVISION } =
  PathSteps;

export const newPathFields = {
  [BASIC_INFORMATION]: {
    all: () => BASIC_INFORMATION,
    title: () => `${BASIC_INFORMATION}.title` as const,
    description: () => `${BASIC_INFORMATION}.description` as const,
    coverPicture: () => `${BASIC_INFORMATION}.coverPicture` as const,
  },
  [CONTENT]: {
    all: () => CONTENT,
    courses: () => `${CONTENT}.courses` as const,
  },
  [CONFIGURATION]: {
    all: () => CONFIGURATION,
    isObligatory: () => `${CONFIGURATION}.isObligatory` as const,
    isSequential: () => `${CONFIGURATION}.isSequential` as const,
    sendPushNotification: () =>
      `${CONFIGURATION}.sendPushNotification` as const,
    sendEmailNotification: () =>
      `${CONFIGURATION}.sendEmailNotification` as const,
    dueDate: () => `${CONFIGURATION}.dueDate` as const,
    issueCertificate: () => `${CONFIGURATION}.issueCertificate` as const,
  },
  [SEGMENTATION]: {
    all: () => SEGMENTATION,
    userSegmentation: () => `${SEGMENTATION}.userSegmentation` as const,
    groupSegmentation: () => `${SEGMENTATION}.groupSegmentation` as const,
  },
  [REVISION]: {},
};

export const newPathRules: Record<
  PathStepsType,
  Record<string, () => RulesOptions>
> = {
  [BASIC_INFORMATION]: {
    title: () => ({ requiredString: true, maxString: 140, minString: 3 }),
    description: () => ({ maxString: 1000 }),
  },
  [CONTENT]: {},
  [CONFIGURATION]: {
    dueDate: () => ({
      requiredStringWithMessage: i18next.t('GENERAL:INCOMPLETE'),
    }),
  },
  [SEGMENTATION]: {},
  [REVISION]: {},
};

export const hasStepChanges = (
  prevValues: NewPathFormValues | null,
  currentValues: NewPathFormValues | undefined,
  step: PathStepsType,
  isAudienceDefined: boolean,
) => {
  if (!prevValues || !currentValues) return false;

  // Edge case: if audience is not defined, we need to force to set the audience call
  if (!isAudienceDefined && step === PathSteps.SEGMENTATION) return true;

  const prevFields = prevValues[step as keyof NewPathFormValues];
  const nextFields = currentValues[step as keyof NewPathFormValues];

  return !isEqual(prevFields, nextFields);
};

export const updatePath = (
  newPath: PathDetail,
  currentValues: NewPathFormValues,
): NewPathFormValues => {
  const newBasicInformation: PathBasicInformation = {
    ...currentValues.basic_information,
    id: newPath.id,
    status: newPath.status,
    coverPicture: {
      original: currentValues.basic_information.coverPicture?.original || null,
      cropped: currentValues.basic_information.coverPicture?.cropped || null,
      url: newPath.pictureUrl,
    },
  };

  const newConfiguration: PathConfiguration = {
    ...currentValues.configuration,
    isObligatory: newPath.isObligatory,
    ...fromNotificationChannel(newPath.sendNotification),
  };

  return {
    ...currentValues,
    basic_information: newBasicInformation,
    configuration: newConfiguration,
  };
};
