using ErsatzTV.Application.Channels;
using ErsatzTV.Controllers;
using ErsatzTV.Core.Interfaces.FFmpeg;
using ErsatzTV.Core.Interfaces.Streaming;
using ErsatzTV.Core.Iptv;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NUnit.Framework;
using Shouldly;

namespace ErsatzTV.Tests.Controllers;

// #559: dynamic /iptv manifests embed the caller's access_token in their body/URLs, so the response must
// be marked no-store to keep a browser or intermediary from caching (and later replaying) the token.
[TestFixture]
public class IptvControllerCacheHeaderTests
{
    [Test]
    public async Task GetChannelPlaylist_sets_no_store()
    {
        var mediator = Substitute.For<IMediator>();
        mediator.Send(Arg.Any<GetChannelPlaylist>(), Arg.Any<CancellationToken>())
            .Returns(new ChannelPlaylist("https", "tv.example.com", string.Empty, [], "VLC/3.0", accessToken: null));

        var controller = new IptvController(
            mediator,
            Substitute.For<IGraphicsEngine>(),
            Substitute.For<ILogger<IptvController>>(),
            Substitute.For<IFFmpegSegmenterService>(),
            Substitute.For<IDirectStreamSessionTracker>())
        {
            ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }
        };

        await controller.GetChannelPlaylist();

        controller.Response.Headers.CacheControl.ToString().ShouldBe("private, no-store");
    }
}
