using OF_DL.Models.Dtos.Common;
using OF_DL.Models.Dtos.Streams;
using OF_DL.Models.Entities.Streams;
using OF_DL.Models.Mappers;

namespace OF_DL.Tests.Models.Mappers;

public class StreamsMapperTests
{
    [Fact]
    public void FromDto_ReturnsDefaults_WhenDtoNull()
    {
        Streams result = StreamsMapper.FromDto(null);

        Assert.False(result.HasMore);
        Assert.Null(result.TailMarker);
        Assert.Empty(result.List);
    }

    [Fact]
    public void FromDto_MapsListItems()
    {
        DateTime postedAt = new(2024, 8, 9, 10, 11, 12, DateTimeKind.Utc);

        StreamsDto dto = new()
        {
            HasMore = true,
            TailMarker = "tail",
            List =
            [
                new ListItemDto
                {
                    Id = 333,
                    PostedAt = postedAt,
                    Author = new AuthorDto { Id = 9 },
                    Text = "stream",
                    RawText = "stream",
                    Price = "1.00",
                    IsOpened = null,
                    IsArchived = null,
                    Media =
                    [
                        new MediumDto
                        {
                            Id = 444,
                            Type = "video",
                            CanView = true,
                            Files = new FilesDto
                            {
                                Full = new FullDto { Url = "https://example.com/full.mp4" },
                                Drm = new DrmDto
                                {
                                    Manifest = new ManifestDto { Dash = "dash.mpd" },
                                    Signature = new SignatureDto
                                    {
                                        Dash = new DashDto { CloudFrontPolicy = "policy" }
                                    }
                                }
                            }
                        }
                    ]
                }
            ]
        };

        Streams result = StreamsMapper.FromDto(dto);

        Assert.True(result.HasMore);
        Assert.Equal("tail", result.TailMarker);
        Assert.Single(result.List);

        ListItem item = result.List[0];
        Assert.Equal(333, item.Id);
        Assert.Equal(postedAt, item.PostedAt);
        Assert.NotNull(item.Author);
        Assert.Equal(9, item.Author.Id);
        Assert.Equal("stream", item.Text);
        Assert.False(item.IsOpened);
        Assert.False(item.IsArchived);
        Assert.NotNull(item.Media);
        Assert.Single(item.Media);

        Medium media = item.Media[0];
        Assert.Equal(444, media.Id);
        Assert.NotNull(media.Files);
        Assert.NotNull(media.Files.Full);
        Assert.Equal("https://example.com/full.mp4", media.Files.Full.Url);
        Assert.NotNull(media.Files.Drm?.Manifest);
        Assert.Equal("dash.mpd", media.Files.Drm.Manifest.Dash);
    }
}
