using ErsatzTV.Application.Artworks;
using ErsatzTV.Core.Domain;
using MediatR;

namespace ErsatzTV.Application.Channels;

public record CreateAutoTunedChannels(
    int TemplateId,
    string Group,
    List<AutoTuneChannelSelection> Channels) : IRequest<AutoTuneResult>;

// The batch-level TemplateId is the default; any per-channel field set here overrides it for that one
// channel. Advanced/Logo/TemplateId are all optional so the older positional {axis, value, name, number}
// form (and every existing caller/test) keeps compiling and behaving identically.
public record AutoTuneChannelSelection(
    AutoTuneAxis Axis,
    string Value,
    string Name,
    string Number,
    int? TemplateId = null,
    ArtworkContentTypeModel Logo = null,
    CreateChannelFromLineupAdvancedOptions Advanced = null,
    List<AutoTuneSourceWeight> Sources = null);

// Per-content-source rotation weight + query correction for a weighted auto-tune channel (#425).
// SourceId is the show id (TV axes) or movie media-item id (movie axis) from the members list (#384).
// Weight is the relative share of airtime (weighted round-robin; 1 = fair-share). Excluded drops the
// source entirely. A SourceId that is not in the axis's base set is an "add-untagged" source — materialized
// like any other. When every entry is Weight 1 and not excluded (and adds nothing), the channel keeps the
// single-SmartCollection fair-share shape; otherwise it is built as a MultiCollection of per-source
// SmartCollections carrying the weights.
public record AutoTuneSourceWeight(int SourceId, int Weight = 1, bool Excluded = false);

public record AutoTuneResult(List<AutoTuneChannelOutcome> Results)
{
    public int CreatedCount => Results.Count(r => r.Status == AutoTuneOutcomeStatus.Created);
    public int SkippedCount => Results.Count(r => r.Status == AutoTuneOutcomeStatus.Skipped);
    public int FailedCount => Results.Count(r => r.Status == AutoTuneOutcomeStatus.Failed);
}

public record AutoTuneChannelOutcome(
    string Name,
    AutoTuneOutcomeStatus Status,
    int? ChannelId,
    string Reason);

public enum AutoTuneOutcomeStatus
{
    Created,
    Skipped,
    Failed
}
