using ErsatzTV.Application.ProgramSchedules;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Scheduling;

namespace ErsatzTV.Controllers.Api.Requests;

/// <param name="Id">
/// Server-assigned identity of an existing schedule item, as returned by GET /api/v1/schedules/{id}/items.
/// Omit (or send null / 0) for a new item. On a replace, the item carrying this id keeps its persisted
/// fill-group/shuffle progression even when moved to a different position. Unknown or duplicated ids are
/// rejected with 422 — never fabricate an id.
/// </param>
public record ScheduleItemRequest(
    int? Id,
    StartType StartType,
    TimeSpan? StartTime,
    FixedStartTimeBehavior? FixedStartTimeBehavior,
    PlayoutMode PlayoutMode,
    CollectionType CollectionType,
    int? CollectionId,
    int? MultiCollectionId,
    int? SmartCollectionId,
    int? RerunCollectionId,
    int? MediaItemId,
    int? PlaylistId,
    string SearchTitle,
    string SearchQuery,
    PlaybackOrder PlaybackOrder,
    MarathonGroupBy MarathonGroupBy,
    bool MarathonShuffleGroups,
    bool MarathonShuffleItems,
    int? MarathonBatchSize,
    FillWithGroupMode FillWithGroupMode,
    MultipleMode MultipleMode,
    string MultipleCount,
    TimeSpan? PlayoutDuration,
    TailMode TailMode,
    int? DiscardToFillAttempts,
    string CustomTitle,
    GuideMode GuideMode,
    int? PreRollFillerId,
    int? MidRollFillerId,
    int? PostRollFillerId,
    int? TailFillerId,
    int? FallbackFillerId,
    List<int> WatermarkIds,
    List<int> GraphicsElementIds,
    string PreferredAudioLanguageCode,
    string PreferredAudioTitle,
    string PreferredSubtitleLanguageCode,
    ChannelSubtitleMode? SubtitleMode)
{
    public AddProgramScheduleItem ToAddCommand(int scheduleId) =>
        new(
            scheduleId,
            StartType,
            StartTime,
            FixedStartTimeBehavior,
            PlayoutMode,
            CollectionType,
            CollectionId,
            MultiCollectionId,
            SmartCollectionId,
            RerunCollectionId,
            MediaItemId,
            PlaylistId,
            SearchTitle,
            SearchQuery,
            PlaybackOrder,
            MarathonGroupBy,
            MarathonShuffleGroups,
            MarathonShuffleItems,
            MarathonBatchSize,
            FillWithGroupMode,
            MultipleMode,
            MultipleCount,
            PlayoutDuration,
            TailMode,
            DiscardToFillAttempts,
            CustomTitle,
            GuideMode,
            PreRollFillerId,
            MidRollFillerId,
            PostRollFillerId,
            TailFillerId,
            FallbackFillerId,
            WatermarkIds ?? [],
            GraphicsElementIds ?? [],
            PreferredAudioLanguageCode,
            PreferredAudioTitle,
            PreferredSubtitleLanguageCode,
            SubtitleMode);

    public ReplaceProgramScheduleItem ToReplaceCommand(int index) =>
        new(
            // normalize the two-state client value (null / 0 / positive) to the handler's null-or-real
            // contract so 0-defaulting clients read as "new item" rather than "existing item 0"
            Id is > 0 ? Id : null,
            index,
            StartType,
            StartTime,
            FixedStartTimeBehavior,
            PlayoutMode,
            CollectionType,
            CollectionId,
            MultiCollectionId,
            SmartCollectionId,
            RerunCollectionId,
            MediaItemId,
            PlaylistId,
            SearchTitle,
            SearchQuery,
            PlaybackOrder,
            MarathonGroupBy,
            MarathonShuffleGroups,
            MarathonShuffleItems,
            MarathonBatchSize,
            FillWithGroupMode,
            MultipleMode,
            MultipleCount,
            PlayoutDuration,
            TailMode,
            DiscardToFillAttempts,
            CustomTitle,
            GuideMode,
            PreRollFillerId,
            MidRollFillerId,
            PostRollFillerId,
            TailFillerId,
            FallbackFillerId,
            WatermarkIds ?? [],
            GraphicsElementIds ?? [],
            PreferredAudioLanguageCode,
            PreferredAudioTitle,
            PreferredSubtitleLanguageCode,
            SubtitleMode);
}
