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 TikTokFeedPayloadType = {
  token?: string;
  id?: number;
  type: string;
  accountId?: number;
  name?: string;
  description: string;
  lang: string;
  accessLevel: string;
  accessAllPublishers: boolean;
  accessVideoOwnerId?: number | null;
  accessPublishers?: { publisherId: number }[];
  schedule_value: string;
  schedule_frequency: number;
  loadFromLastDays: number;

  videoFileType: string;
  isEvergreenFeed: boolean;
  discardLongClips: boolean;
  maxDuration: number;
  skipTaggingForLongClips: boolean;
  maxDurationForTagging: number;
  immediateAvailability: boolean;
  isRestricted: boolean;
  createClip: boolean;
  fillLandingPage: boolean;
  priorityVerification: boolean;

  speechToTextProvider: string;
  platformModels: PlatformModel[];
  automationScript: string;

  schedule_type: string;
  schedule_status: boolean;
  importPlot: boolean;

  // default
  status: number;
  source: {
    deleteAfterImport: boolean;
    importParticipantsNames: boolean;
  };
};

const getTikTokFields = (
  fields: AllFieldsType,
  isSelfServeUser: boolean,
  metadataPlatformModels: MetadataFeedPlatformModelType[] | null,
): Partial<TikTokFeedPayloadType> => {
  const payload: TikTokFeedPayloadType = {
    type: fields.type,
    name: fields.name,
    description: fields.description,
    lang: fields.lang,
    accessLevel: fields.accessLevel,
    accessAllPublishers: fields.accessAllPublishers,
    schedule_value: fields.scheduleValue,
    schedule_frequency: fields.scheduleFrequency,
    loadFromLastDays: fields.loadFromLastDays,

    videoFileType: fields.videoFileType,
    isEvergreenFeed: fields.isEvergreenFeed,
    discardLongClips: fields.discardLongClips,
    maxDuration: fields.maxDuration,
    skipTaggingForLongClips: fields.skipTaggingForLongClips,
    maxDurationForTagging: fields.maxDurationForTagging,
    immediateAvailability: fields.immediateAvailability,
    isRestricted: fields.isRestricted,
    createClip: fields.createClip,
    fillLandingPage: fields.fillLandingPage,
    priorityVerification: fields.priorityVerification,

    speechToTextProvider: fields.speechToTextProvider,
    platformModels: fields.platformModels,
    automationScript: fields.automationScript,
    accessPublishers: [],

    // default values
    status: 1,
    source: {
      deleteAfterImport: false,
      importParticipantsNames: false,
    },

    schedule_type: fields.scheduleType,
    schedule_status: fields.scheduleStatus,
    importPlot: fields.importPlot,
  };

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

  if (fields.oAuthToken) {
    payload.token = fields.oAuthToken;
  }

  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',
      'token',
      'type',
      'description',
      'lang',
      'accessLevel',
      'accessAllPublishers',
      'accountId',
      'accessVideoOwnerId',
      'accessPublishers',

      'schedule_value',
      'schedule_frequency',
      'loadFromLastDays',

      'status',
      'source',
      'schedule_status',
      'schedule_type',
    ]);
  }

  return payload;
};

export default getTikTokFields;
