import { ACCESS_LEVEL_HUB, ACCESS_LEVEL_PRIVATE } from '@/modules/feeds/Editor/constants';

import { MetadataFeedPlatformModelType, PlatformModel } from '@/modules/feeds/Editor/types';
import type { FeedType } from '@/modules/feeds/types';

import { pick } from '../../helpers';
import { getDefaultPlatformModelsWhenCreate } from '@/modules/feeds/Editor/components/FormTabs/ModelTab/helpers';
import { AllFieldsType } from '@/modules/feeds/Editor/helpers/requestPayload/getAllFields';

export type MrssFeedPayloadType = {
  id?: number;
  type: string;
  accountId?: number;
  name?: string;
  description: string;
  lang: string;
  url: string;
  speechToTextProvider: string;
  auth_method: string;
  priorityVerification: boolean;
  feedPriority: number;
  schedule_type: string;
  immediateAvailability: boolean;
  skipTaggingForLongClips: boolean;
  maxDurationForTagging: number;
  accessLevel: string;
  accessAllPublishers: boolean;
  parseKeywordsToLabels: boolean;
  iabCategories: string;
  accessVideoOwnerId?: number | null;
  accessPublishers?: { publisherId: number }[];
  user: string;
  password: string;
  use_for_download: boolean;
  schedule_status: boolean;
  schedule_value: string;
  schedule_frequency: number;
  defaultTimezoneEnabled: boolean;
  defaultTimezone: string;
  isEvergreenFeed: boolean;
  isRestricted: boolean;
  keywords: string;
  maxDuration: number;
  importPlot: boolean;
  videoFileType: string;
  fileSelection: string;
  resolution: string;
  resolutionValue: number;
  minResolutionValue: number;
  bitrate: number;
  minBitrate: number;
  importCCFromMrssFile: boolean;
  createClip: boolean;
  processVideoVersions: boolean;
  versionAttributeName: string;
  automationScript: string;
  platformModels: PlatformModel[];

  keywords_parser_type: string;
  contentXmlStructure: string;
  thumbnailsXmlStructure: string;
};

const getMrssFields = (
  fields: AllFieldsType,
  isSelfServeUser: boolean,
  metadataPlatformModels: MetadataFeedPlatformModelType[] | null,
): Partial<MrssFeedPayloadType> => {
  const payload: MrssFeedPayloadType = {
    type: fields.type,
    name: fields.name,
    description: fields.description,
    lang: fields.lang,
    url: fields.url,
    speechToTextProvider: fields.speechToTextProvider,
    auth_method: fields.authMethod,
    priorityVerification: fields.priorityVerification,
    feedPriority: fields.feedPriority,
    schedule_type: fields.scheduleType,
    immediateAvailability: fields.immediateAvailability,
    skipTaggingForLongClips: fields.skipTaggingForLongClips,
    maxDurationForTagging: fields.maxDurationForTagging,
    accessLevel: fields.accessLevel,
    accessAllPublishers: fields.accessAllPublishers,
    parseKeywordsToLabels: fields.parseKeywordsToLabels,
    iabCategories: fields.iabCategories || '[]',
    user: fields.user,
    password: fields.password,
    use_for_download: fields.isUseForDownload,
    schedule_status: fields.scheduleStatus,
    schedule_value: fields.scheduleValue,
    schedule_frequency: fields.scheduleFrequency,
    defaultTimezoneEnabled: fields.defaultTimezoneEnabled,
    defaultTimezone: fields.defaultTimezone,
    isEvergreenFeed: fields.isEvergreenFeed,
    isRestricted: fields.isRestricted,
    keywords: fields.keywords,
    maxDuration: fields.maxDuration,
    importPlot: fields.importPlot,
    videoFileType: fields.videoFileType,
    fileSelection: fields.fileSelection,
    resolution: fields.resolution,
    resolutionValue: fields.resolutionValue,
    minResolutionValue: fields.minResolutionValue,
    bitrate: fields.bitrate,
    minBitrate: fields.minBitrate,
    importCCFromMrssFile: fields.importCCFromMrssFile,
    createClip: fields.createClip,
    processVideoVersions: fields.processVideoVersions,
    versionAttributeName: fields.versionAttributeName,
    automationScript: fields.automationScript || '{}',
    platformModels: fields.platformModels,
    accessPublishers: [],

    // default values
    contentXmlStructure: 'DEFAULT',
    thumbnailsXmlStructure: 'DEFAULT',
    keywords_parser_type: 'DEFAULT',
  };

  if (fields.id) {
    payload.id = fields.id;
  }

  if (fields.account?.id && !fields.id) {
    payload.accountId = fields.account.id;
  }

  if (fields.accessLevel === ACCESS_LEVEL_PRIVATE && fields.accessVideoOwner?.id) {
    payload.accessVideoOwnerId = fields.accessVideoOwner.id;
  }

  if (fields.accessLevel === ACCESS_LEVEL_HUB && fields.accessPublishers?.length && !fields.accessAllPublishers) {
    payload.accessPublishers = fields.accessPublishers.map((hub) => ({ publisherId: hub.id }));
  }

  if (fields.feedPriority) {
    payload.feedPriority = parseFloat(fields.feedPriority as unknown as string);
  }

  if (fields.platformModels.length === 0 && metadataPlatformModels && !fields.id) {
    payload.platformModels = getDefaultPlatformModelsWhenCreate(metadataPlatformModels, fields.type as FeedType);
  }

  // if selfServe
  if (isSelfServeUser) {
    return pick(payload, [
      'id',
      'type',
      'description',
      'lang',
      'iabCategories',
      'accessLevel',
      'accessAllPublishers',
      'accessVideoOwnerId',
      'accessPublishers',
      'url',

      'isEvergreenFeed',
      'keywords',
      'importPlot',
      'importCCFromMrssFile',

      'schedule_status',
      'schedule_type',
      'schedule_value',
      'schedule_frequency',

      'defaultTimezoneEnabled',
      'defaultTimezone',

      'user',
      'password',
      'auth_method',
      'use_for_download',
    ]);
  }

  return payload;
};

export default getMrssFields;
