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 MsStreamFeedPayloadType = {
  id?: number;
  type: string;
  accountId?: number;
  name: string;
  description: string;
  lang: string;
  discardLongClips: boolean;
  speechToTextProvider: string;
  priorityVerification: boolean;
  schedule_type: string;
  immediateAvailability: boolean;
  skipTaggingForLongClips: boolean;
  maxDurationForTagging: number;
  accessLevel: string;
  accessAllPublishers: boolean;
  accessVideoOwnerId?: number | null;
  accessPublishers?: { publisherId: number }[];
  schedule_status: boolean;
  schedule_value: string;
  schedule_frequency: number;
  isEvergreenFeed: boolean;
  isRestricted: boolean;
  keywords: string;
  maxDuration: number;
  importPlot: boolean;
  videoFileType: string;
  createClip: boolean;
  automationScript: string;
  platformModels: PlatformModel[];

  user: string;
  password: string;
  youTubeChannelId: string;

  status: number;
};

const getMsStreamFields = (
  fields: AllFieldsType,
  isSelfServeUser: boolean,
  metadataPlatformModels: MetadataFeedPlatformModelType[] | null,
): Partial<MsStreamFeedPayloadType> => {
  const payload: MsStreamFeedPayloadType = {
    type: fields.type,
    name: fields.description, // in old UI name === description
    description: fields.description,
    lang: fields.lang,
    discardLongClips: fields.discardLongClips,
    speechToTextProvider: fields.speechToTextProvider,
    priorityVerification: fields.priorityVerification,
    schedule_type: fields.scheduleType,
    immediateAvailability: fields.immediateAvailability,
    skipTaggingForLongClips: fields.skipTaggingForLongClips,
    maxDurationForTagging: fields.maxDurationForTagging,
    accessLevel: fields.accessLevel,
    accessAllPublishers: fields.accessAllPublishers,
    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,
    createClip: fields.createClip,
    automationScript: fields.automationScript,
    platformModels: fields.platformModels,

    user: fields.user,
    password: fields.password,
    youTubeChannelId: fields.youTubeChannelId,

    status: 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.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',
      'accessLevel',
      'accessAllPublishers',
      'accessVideoOwnerId',
      'accessPublishers',
      'user',
      'password',
      'youTubeChannelId',
      'status',
      'schedule_status',
      'platformModels',
    ]);
  }

  return payload;
};

export default getMsStreamFields;
