import {
  ACCESS_LEVEL_HUB,
  ACCESS_LEVEL_PRIVATE,
  CONTENT_OWNER_LIVE_EVENT_TYPE,
  CONTENT_OWNER_STATUS_DISABLED,
  FILE_SELECTION_BITRATE,
  FILE_SELECTION_NONE,
  FILE_SELECTION_RESOLUTION,
  SCHEDULE_TYPE_CUSTOM,
  TAB_ADVANCED,
  TAB_GENERAL,
} from '../constants';
import {
  TYPE_CSV,
  TYPE_MANUAL,
  TYPE_MRSS,
  TYPE_MS_STREAM,
  TYPE_RSS,
  TYPE_SITEMAP,
  TYPE_STORY_API,
  TYPE_TIKTOK,
  TYPE_VIDEO_API,
  TYPE_VIMEO,
  TYPE_YOUTUBE,
} from '@/modules/feeds/constants';

import { isNumber } from '@/modules/@common/helpers/number';
import { StateType } from '@/modules/feeds/Editor/redux/slices';

const urlRegex = /^(https?):\/\/[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&/=]*)/;
const versionAttributeNameRegex = /^[a-zA-Z]+$/;

const requiredFieldsForAdminFeedsByType = {
  name: [
    TYPE_MRSS,
    TYPE_TIKTOK,
    TYPE_RSS,
    TYPE_SITEMAP,
    TYPE_STORY_API,
    TYPE_MANUAL,
    TYPE_YOUTUBE,
    TYPE_CSV,
    TYPE_VIDEO_API,
    TYPE_VIMEO,
  ],
  account: [
    TYPE_MRSS,
    TYPE_TIKTOK,
    TYPE_RSS,
    TYPE_SITEMAP,
    TYPE_STORY_API,
    TYPE_MANUAL,
    TYPE_YOUTUBE,
    TYPE_CSV,
    TYPE_VIDEO_API,
    TYPE_VIMEO,
    TYPE_MS_STREAM,
  ],
  description: [
    TYPE_MRSS,
    TYPE_TIKTOK,
    TYPE_RSS,
    TYPE_SITEMAP,
    TYPE_STORY_API,
    TYPE_MANUAL,
    TYPE_YOUTUBE,
    TYPE_CSV,
    TYPE_VIDEO_API,
    TYPE_VIMEO,
    TYPE_MS_STREAM,
  ],
  accessPublishers: [
    TYPE_MRSS,
    TYPE_TIKTOK,
    TYPE_RSS,
    TYPE_SITEMAP,
    TYPE_STORY_API,
    TYPE_MANUAL,
    TYPE_YOUTUBE,
    TYPE_CSV,
    TYPE_VIDEO_API,
    TYPE_VIMEO,
    TYPE_MS_STREAM,
  ],
  accessVideoOwner: [
    TYPE_MRSS,
    TYPE_TIKTOK,
    TYPE_RSS,
    TYPE_SITEMAP,
    TYPE_STORY_API,
    TYPE_MANUAL,
    TYPE_YOUTUBE,
    TYPE_CSV,
    TYPE_VIDEO_API,
    TYPE_VIMEO,
    TYPE_MS_STREAM,
  ],
  url: [TYPE_MRSS, TYPE_RSS, TYPE_SITEMAP, TYPE_STORY_API, TYPE_CSV, TYPE_VIDEO_API],
  feedPriority: [TYPE_MRSS, TYPE_MANUAL, TYPE_YOUTUBE, TYPE_CSV, TYPE_VIDEO_API, TYPE_VIMEO],
  maxDuration: [TYPE_MRSS, TYPE_YOUTUBE, TYPE_TIKTOK, TYPE_CSV, TYPE_VIDEO_API, TYPE_VIMEO, TYPE_MS_STREAM],
  maxDurationForTagging: [
    TYPE_MRSS,
    TYPE_MANUAL,
    TYPE_YOUTUBE,
    TYPE_TIKTOK,
    TYPE_CSV,
    TYPE_VIDEO_API,
    TYPE_VIMEO,
    TYPE_MS_STREAM,
  ],
  versionAttributeName: [TYPE_MRSS, TYPE_MANUAL],
  loadFromLastDays: [TYPE_TIKTOK, TYPE_VIMEO],
  scheduleFrequency: [
    TYPE_MRSS,
    TYPE_TIKTOK,
    TYPE_RSS,
    TYPE_SITEMAP,
    TYPE_STORY_API,
    TYPE_YOUTUBE,
    TYPE_CSV,
    TYPE_VIDEO_API,
    TYPE_VIMEO,
    TYPE_MS_STREAM,
  ],
  resolutionValue: [
    TYPE_MRSS,
    TYPE_RSS,
    TYPE_SITEMAP,
    TYPE_STORY_API,
    TYPE_YOUTUBE,
    TYPE_CSV,
    TYPE_VIDEO_API,
    TYPE_VIMEO,
  ],
  minResolutionValue: [
    TYPE_MRSS,
    TYPE_RSS,
    TYPE_SITEMAP,
    TYPE_STORY_API,
    TYPE_YOUTUBE,
    TYPE_CSV,
    TYPE_VIDEO_API,
    TYPE_VIMEO,
  ],
  bitrate: [TYPE_MRSS, TYPE_RSS, TYPE_SITEMAP, TYPE_STORY_API, TYPE_YOUTUBE, TYPE_CSV, TYPE_VIDEO_API, TYPE_VIMEO],
  minBitrate: [TYPE_MRSS, TYPE_RSS, TYPE_SITEMAP, TYPE_STORY_API, TYPE_YOUTUBE, TYPE_CSV, TYPE_VIDEO_API, TYPE_VIMEO],
  maxStories: [TYPE_RSS, TYPE_SITEMAP, TYPE_STORY_API],
  videoDuration: [TYPE_RSS, TYPE_SITEMAP, TYPE_STORY_API],
  videoMaxZoom: [TYPE_RSS, TYPE_SITEMAP, TYPE_STORY_API],
  youTubeChannelId: [TYPE_YOUTUBE, TYPE_MS_STREAM],
  user: [TYPE_VIMEO, TYPE_MS_STREAM],
  password: [TYPE_VIMEO, TYPE_MS_STREAM],
} as const;

const requiredFieldsForSelfServeFeedsByType = {
  description: [TYPE_TIKTOK, TYPE_MRSS, TYPE_MS_STREAM],
  accessPublishers: [TYPE_TIKTOK, TYPE_MRSS, TYPE_MS_STREAM],
  accessVideoOwner: [TYPE_TIKTOK, TYPE_MRSS, TYPE_MS_STREAM],
  url: [TYPE_MRSS],
  scheduleFrequency: [TYPE_MRSS],
  loadFromLastDays: [TYPE_TIKTOK],
  user: [TYPE_MS_STREAM],
  password: [TYPE_MS_STREAM],
  youTubeChannelId: [TYPE_MS_STREAM],
} as const;

const shouldValidate = (fieldName: string, type: string, isSelfServeUser: boolean) => {
  const fields = (isSelfServeUser
    ? requiredFieldsForSelfServeFeedsByType
    : requiredFieldsForAdminFeedsByType) as unknown as Record<string, string[]>;

  return !!fields[fieldName]?.includes(type);
};

export const validationScheme = [
  {
    fieldName: 'name',
    tabId: TAB_GENERAL,
    validation: (value: string, formFields: StateType) => {
      if (!shouldValidate('name', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (value.length < 2) {
        return 'Minimum 2 letters';
      }

      return '';
    },
  },
  {
    fieldName: 'account',
    tabId: TAB_GENERAL,
    validation: (value: StateType['account'], formFields: StateType) => {
      if (!shouldValidate('account', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (value?.contentOwners) {
        const isAllowedContentOwners = value.contentOwners.some(
          (co) => co.type !== CONTENT_OWNER_LIVE_EVENT_TYPE && co.status !== CONTENT_OWNER_STATUS_DISABLED,
        );

        if (!isAllowedContentOwners) {
          return 'Only allowed content owners are: Not Live Event, Not Disabled.';
        }
      }

      return '';
    },
  },
  {
    fieldName: 'description',
    tabId: TAB_GENERAL,
    validation: (value: string, formFields: StateType) => {
      if (!shouldValidate('description', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (value.length < 2) {
        return 'Minimum 2 letters';
      }

      return '';
    },
  },
  {
    fieldName: 'accessPublishers',
    tabId: TAB_GENERAL,
    validation: (value: unknown[], formFields: StateType) => {
      if (!shouldValidate('accessPublishers', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (formFields.accessLevel !== ACCESS_LEVEL_HUB || formFields.accessAllPublishers) {
        return '';
      }

      if (!value.length) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'accessVideoOwner',
    tabId: TAB_GENERAL,
    validation: (value: unknown, formFields: StateType) => {
      if (!shouldValidate('accessVideoOwner', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (formFields.accessLevel !== ACCESS_LEVEL_PRIVATE) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'url',
    tabId: TAB_GENERAL,
    validation: (value: string, formFields: StateType) => {
      if (!shouldValidate('url', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (!urlRegex.test(value)) {
        return 'Please provide correct url';
      }

      return '';
    },
  },
  {
    fieldName: 'scheduleFrequency',
    tabId: TAB_GENERAL,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('scheduleFrequency', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (formFields.scheduleType !== SCHEDULE_TYPE_CUSTOM) {
        return '';
      }

      if (!isNumber(value)) {
        return 'Field cannot be empty';
      }

      if (value < 1 || value > 10000) {
        return 'Please enter a value between 1 and 10,000';
      }

      return '';
    },
  },
  {
    fieldName: 'feedPriority',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('feedPriority', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (value < 0.01 || value > 1) {
        return 'Please enter a value between 0.01 and 1';
      }

      return '';
    },
  },
  {
    fieldName: 'maxDuration',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('maxDuration', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (value < 10 || value > 7200) {
        return 'Please enter a value between 10 and 7200';
      }

      return '';
    },
  },
  {
    fieldName: 'maxDurationForTagging',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('maxDurationForTagging', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (value < 1 || value > 10000) {
        return 'Please enter a value between 1 and 10.000';
      }

      return '';
    },
  },
  {
    fieldName: 'versionAttributeName',
    tabId: TAB_ADVANCED,
    validation: (value: string, formFields: StateType) => {
      if (!shouldValidate('versionAttributeName', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!formFields.processVideoVersions) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (!versionAttributeNameRegex.test(value)) {
        return 'Letters only, no spaces';
      }

      return '';
    },
  },
  {
    fieldName: 'loadFromLastDays',
    tabId: TAB_GENERAL,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('loadFromLastDays', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (value < 1 || value > 9999) {
        return 'Field should be 1 to 9999';
      }

      return '';
    },
  },
  {
    fieldName: 'youTubeChannelId',
    tabId: TAB_GENERAL,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('youTubeChannelId', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'user',
    tabId: TAB_GENERAL,
    validation: (value: string, formFields: StateType) => {
      if (!shouldValidate('user', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      if (value.length < 2) {
        return 'Minimum 2 letters';
      }

      return '';
    },
  },
  {
    fieldName: 'password',
    tabId: TAB_GENERAL,
    validation: (value: string, formFields: StateType) => {
      if (!shouldValidate('password', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'resolutionValue',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('resolutionValue', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (formFields.fileSelection === FILE_SELECTION_NONE || formFields.fileSelection !== FILE_SELECTION_RESOLUTION) {
        return '';
      }

      if (!isNumber(value)) {
        return 'Field cannot be empty';
      }

      if (value < 0 || value > 10000) {
        return 'Please enter a value between 0 and 10,000';
      }

      return '';
    },
  },
  {
    fieldName: 'minResolutionValue',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('minResolutionValue', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (formFields.fileSelection === FILE_SELECTION_NONE || formFields.fileSelection !== FILE_SELECTION_RESOLUTION) {
        return '';
      }

      if (!isNumber(value)) {
        return 'Field cannot be empty';
      }

      if (value < 190 || value > 10000) {
        return 'Please enter a value between 190 and 10,000';
      }

      return '';
    },
  },
  {
    fieldName: 'bitrate',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('bitrate', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (formFields.fileSelection === FILE_SELECTION_NONE || formFields.fileSelection !== FILE_SELECTION_BITRATE) {
        return '';
      }

      if (!isNumber(value)) {
        return 'Field cannot be empty';
      }

      if (value < 0 || value > 10000) {
        return 'Please enter a value between 0 and 10,000';
      }

      return '';
    },
  },
  {
    fieldName: 'minBitrate',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('minBitrate', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (formFields.fileSelection === FILE_SELECTION_NONE || formFields.fileSelection !== FILE_SELECTION_BITRATE) {
        return '';
      }

      if (!isNumber(value)) {
        return 'Field cannot be empty';
      }

      if (value < 0 || value > 10000) {
        return 'Please enter a value between 0 and 10,000';
      }

      return '';
    },
  },
  {
    fieldName: 'maxStories',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('maxStories', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!isNumber(value)) {
        return 'Field cannot be empty';
      }

      if (value < 1 || value > 10000) {
        return 'Please enter a value between 1 and 10,000';
      }

      return '';
    },
  },
  {
    fieldName: 'videoDuration',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('videoDuration', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!isNumber(value)) {
        return 'Field cannot be empty';
      }

      if (value < 1 || value > 7200) {
        return 'Please enter a value between 1 and 7,200';
      }

      return '';
    },
  },
  {
    fieldName: 'videoMaxZoom',
    tabId: TAB_ADVANCED,
    validation: (value: number, formFields: StateType) => {
      if (!shouldValidate('videoMaxZoom', formFields.type, formFields.isSelfServeUser)) {
        return '';
      }

      if (!isNumber(value)) {
        return 'Field cannot be empty';
      }

      if (value < 1 || value > 100) {
        return 'Please enter a value between 1 and 100';
      }

      return '';
    },
  },
];
