using ErsatzTV.Application.Playouts;

namespace ErsatzTV.Controllers.Api.Requests;

public record ReplacePlayoutAlternateSchedulesRequest(List<PlayoutAlternateScheduleItemRequest> Items)
{
    // Index is assigned from array order: the first item is the highest priority and the last item
    // is the lowest priority (the catch-all default whose schedule becomes the playout's default
    // schedule). This mirrors the Blazor editor, which lists items top-to-bottom in priority order
    // and writes the highest-Index item's schedule as the playout default.
    public ReplacePlayoutAlternateScheduleItems ToCommand(int playoutId, Option<Seq<int>> expectedVersions = default) =>
        new(
            playoutId,
            (Items ?? [])
                .Select((item, index) => item.ToReplaceItem(index))
                .ToList(),
            expectedVersions);
}
