import type { components } from '../../api/generated/v1';
import type { AddItemsToCollectionRequest, LibraryBrowseItem, LibraryBrowseMediaType, ScheduleItemRequest } from '../../api';

type CollectionType = components['schemas']['CollectionType'];

// The union the Add-to dialogs accept for the items they operate on: either a set of browse
// items (bucketed by kind via toAddItemsRequest) or a pre-built request body (the query-based
// "Add all results" flow supplies this so callers never page through every result).
export type AddToItems = LibraryBrowseItem[] | { requestOverride: AddItemsToCollectionRequest };

// Only shows, seasons and artists are schedulable as a single media item. This matches both
// Blazor's AddProgramScheduleItem.ForMediaItem call sites (only the show / season / artist list
// pages offer "Add to schedule") and the server validator
// ProgramScheduleItemCommandBase.CollectionTypeMustBeValid, which 422s every other
// per-media-item CollectionType. Every other kind maps to null (not schedulable).
const MEDIA_TYPE_TO_COLLECTION_TYPE: Partial<Record<LibraryBrowseMediaType, CollectionType>> = {
  Artist: 'Artist',
  TelevisionSeason: 'TelevisionSeason',
  TelevisionShow: 'TelevisionShow'
};

export function collectionTypeForMediaType(mediaType: LibraryBrowseMediaType): CollectionType | null {
  return MEDIA_TYPE_TO_COLLECTION_TYPE[mediaType] ?? null;
}

// Builds the ScheduleItemRequest body for adding a single media item to a schedule, replicating
// the defaults of AddProgramScheduleItem.ForMediaItem
// (ErsatzTV.Application/ProgramSchedules/Commands/AddProgramScheduleItem.cs:48-87): StartType.Dynamic,
// PlayoutMode.One, PlaybackOrder.Shuffle, MarathonGroupBy.None, FillWithGroupMode.None,
// MultipleMode.Count, TailMode.None, GuideMode.Normal, all collection/filler ids null, empty
// watermark/graphics lists — with CollectionType + MediaItemId set for this item.
export function scheduleItemRequestForMediaItem(
  collectionType: CollectionType,
  mediaItemId: number
): ScheduleItemRequest {
  return {
    id: null,
    startType: 'Dynamic',
    startTime: null,
    fixedStartTimeBehavior: null,
    playoutMode: 'One',
    collectionType,
    collectionId: null,
    multiCollectionId: null,
    smartCollectionId: null,
    rerunCollectionId: null,
    mediaItemId,
    playlistId: null,
    searchTitle: null,
    searchQuery: null,
    playbackOrder: 'Shuffle',
    marathonGroupBy: 'None',
    marathonShuffleGroups: false,
    marathonShuffleItems: false,
    marathonBatchSize: null,
    fillWithGroupMode: 'None',
    multipleMode: 'Count',
    multipleCount: null,
    playoutDuration: null,
    tailMode: 'None',
    discardToFillAttempts: null,
    customTitle: null,
    guideMode: 'Normal',
    preRollFillerId: null,
    midRollFillerId: null,
    postRollFillerId: null,
    tailFillerId: null,
    fallbackFillerId: null,
    watermarkIds: [],
    graphicsElementIds: [],
    preferredAudioLanguageCode: null,
    preferredAudioTitle: null,
    preferredSubtitleLanguageCode: null,
    subtitleMode: null
  };
}
