namespace ytLive.Services.Encoder;

/// <summary>
/// Builds the FFmpeg command line for a live RTMP push (TASK 4 ship step 3):
/// raw BGRA frames via stdin (paced <c>-re</c>), real audio via the named pipe
/// (TASK 9 — the mixer streams f32le at 48 kHz stereo into <c>\\.\pipe\&lt;name&gt;</c>;
/// WASAPI loopback + the mic ride the pipe instead of the old anullsrc silence),
/// H.264 + AAC encoding, FLV muxing to the ingestion URL. Pure — the encoder
/// just starts <c>ffmpeg.exe [Build(...)]</c>.
/// </summary>
public static class FfmpegArgs
{
    public static IReadOnlyList<string> Build(EncoderOptions options, string videoEncoder)
    {
        var gop = options.GopSize;
        return
        [
            "-hide_banner",
            "-loglevel", "info",
            "-stats",
            "-stats_period", "0.5",
            "-re",
            "-f", "rawvideo",
            "-pix_fmt", "bgra",
            "-video_size", $"{options.Width}x{options.Height}",
            "-framerate", options.Fps.ToString(),
            "-i", "pipe:0",
            "-f", "f32le",
            "-ar", options.AudioSampleRate.ToString(),
            "-ac", options.AudioChannels.ToString(),
            "-i", $@"\\.\pipe\{options.AudioPipeName}",
            "-map", "0:v",
            "-map", "1:a",
            "-c:v", videoEncoder,
            "-b:v", $"{options.BitrateKbps}k",
            "-maxrate", $"{options.BitrateKbps}k",
            "-bufsize", $"{options.BitrateKbps * 2}k",
            "-g", gop.ToString(),
            "-keyint_min", gop.ToString(),
            "-sc_threshold", "0",
            "-bf", "0",
            "-pix_fmt", "yuv420p",
            "-c:a", "aac",
            "-b:a", "128k",
            "-ar", options.AudioSampleRate.ToString(),
            "-ac", options.AudioChannels.ToString(),
            "-f", "flv",
            options.RtmpUrl,
        ];
    }
}
