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 { getDefaultPlatformModelsWhenCreate } from '@/modules/feeds/Editor/components/FormTabs/ModelTab/helpers';
import { AllFieldsType } from '@/modules/feeds/Editor/helpers/requestPayload/getAllFields';

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

const getVimeoApiFields = (
  fields: AllFieldsType,
  metadataPlatformModels: MetadataFeedPlatformModelType[] | null,
): Partial<VimeoFeedPayloadType> => {
  const payload: VimeoFeedPayloadType = {
    type: fields.type,
    name: fields.name,
    description: fields.description,
    lang: fields.lang,
    speechToTextProvider: fields.speechToTextProvider,
    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,
    iabCategories: fields.iabCategories || '[]',
    user: fields.user,
    password: fields.password,
    schedule_status: fields.scheduleStatus,
    schedule_value: fields.scheduleValue,
    schedule_frequency: fields.scheduleFrequency,
    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,
    createClip: fields.createClip,
    automationScript: fields.automationScript || '{}',
    platformModels: fields.platformModels,
    accessPublishers: [],
    loadFromLastDays: fields.loadFromLastDays || 1,
  };

  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);
  }

  return payload;
};

export default getVimeoApiFields;
