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

namespace OF_DL.Tests.Models.Mappers;

public class MessagesMapperTests
{
    [Fact]
    public void FromDto_ReturnsDefaults_WhenDtoNull()
    {
        Messages result = MessagesMapper.FromDto(null as MessagesDto);

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

    [Fact]
    public void FromDto_MapsListItems()
    {
        DateTime createdAt = new(2024, 3, 4, 5, 6, 7, DateTimeKind.Utc);

        MessagesDto dto = new()
        {
            HasMore = true,
            List =
            [
                new ListItemDto
                {
                    Id = 11,
                    Text = "message",
                    Price = "4.99",
                    CreatedAt = createdAt,
                    FromUser = new FromUserDto { Id = 77 },
                    Media =
                    [
                        new MediumDto
                        {
                            Id = 22,
                            Type = "photo",
                            CanView = true,
                            Files = new FilesDto { Full = new FullDto { Url = "https://example.com/full.jpg" } }
                        }
                    ]
                }
            ]
        };

        Messages result = MessagesMapper.FromDto(dto);

        Assert.True(result.HasMore);
        Assert.Single(result.List);

        ListItem item = result.List[0];
        Assert.Equal(11, item.Id);
        Assert.Equal("message", item.Text);
        Assert.Equal("4.99", item.Price);
        Assert.Equal(createdAt, item.CreatedAt);
        Assert.NotNull(item.FromUser);
        Assert.Equal(77, item.FromUser.Id);
        Assert.NotNull(item.Media);
        Assert.Single(item.Media);
        Assert.Equal(22, item.Media[0].Id);
    }

    [Fact]
    public void FromDto_MapsSingleMessage()
    {
        DateTime createdAt = new(2024, 4, 5, 6, 7, 8, DateTimeKind.Utc);

        SingleMessageDto dto = new()
        {
            Id = 99,
            Text = "single",
            Price = 1.23,
            CreatedAt = createdAt,
            FromUser = new FromUserDto { Id = 55 },
            Media =
            [
                new MediumDto
                {
                    Id = 33,
                    Type = "video",
                    CanView = true,
                    Files = new FilesDto { Full = new FullDto { Url = "https://example.com/full.mp4" } }
                }
            ]
        };

        SingleMessage result = MessagesMapper.FromDto(dto);

        Assert.Equal(99, result.Id);
        Assert.Equal("single", result.Text);
        Assert.Equal(1.23, result.Price);
        Assert.Equal(createdAt, result.CreatedAt);
        Assert.NotNull(result.FromUser);
        Assert.Equal(55, result.FromUser.Id);
        Assert.NotNull(result.Media);
        Assert.Single(result.Media);
        Assert.Equal(33, result.Media[0].Id);
    }

    [Fact]
    public void FromDto_SingleMessage_ReturnsNullFromUser_WhenIdMissing()
    {
        SingleMessageDto dto = new() { Id = 1, FromUser = new FromUserDto { Id = null } };

        SingleMessage result = MessagesMapper.FromDto(dto);

        Assert.Null(result.FromUser);
    }
}
