import dayjs from 'dayjs';

import { TAB_DELIVERY, TAB_GENERAL, TAB_TARGETING } from '../constants';

import { stringToTimestamp } from '../helpers/timestamp';

export const validationScheme = [
  {
    fieldName: 'name',
    tabId: TAB_GENERAL,
    validation: (value) => {
      if (!value) {
        return 'Field cannot be empty';
      }

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

      return '';
    },
  },
  {
    fieldName: 'hub',
    tabId: TAB_GENERAL,
    validation: (value) => {
      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'xrayCampaign',
    tabId: TAB_GENERAL,
    validation: (value) => {
      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'xrayCreative1',
    tabId: TAB_GENERAL,
    validation: (value) => {
      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'startTime',
    tabId: TAB_DELIVERY,
    validation: (value, fields) => {
      if (value) {
        return !dayjs(value).isBefore(fields.endTime) ? 'Must be before then End' : '';
      }

      return '';
    },
  },
  {
    fieldName: 'rate',
    tabId: TAB_DELIVERY,
    validation: (value) => {
      if (!value) {
        return 'Field cannot be empty';
      }

      return '';
    },
  },
  {
    fieldName: 'impressionsBudget',
    tabId: TAB_DELIVERY,
    validation: (value) => {
      if (value && value <= 0) {
        return 'Minimum amount is 1';
      }

      return '';
    },
  },
  {
    fieldName: 'spendBudget',
    tabId: TAB_DELIVERY,
    validation: (value) => {
      if (value && value <= 0) {
        return 'Minimum amount is 1';
      }

      return '';
    },
  },
  {
    fieldName: 'videoFromTimestamp',
    tabId: TAB_TARGETING,
    validation: (value, fields) => {
      if (value && value !== '00:00:00') {
        const videoFromTimestamp = stringToTimestamp(value);
        const videoToTimestamp = stringToTimestamp(fields.videoToTimestamp);

        return videoFromTimestamp >= videoToTimestamp ? 'Must be lower then To timestamp' : '';
      }

      return '';
    },
  },
];
