using Femto.Modules.Blog.Domain.Posts;
using MediatR;

namespace Femto.Modules.Blog.Application.Commands.CreatePost;

internal class CreatePostCommandHandler(BlogContext context)
    : IRequestHandler<CreatePostCommand, Guid>
{
    public async Task<Guid> Handle(CreatePostCommand request, CancellationToken cancellationToken)
    {
        var post = new Post(
            request.AuthorId,
            request.Content,
            request
                .Media.Select(media => new PostMedia(
                    media.MediaId,
                    media.Url,
                    media.Type,
                    media.Order,
                    media.Width,
                    media.Height
                ))
                .ToList()
        );
        
        post.IsPublic = request.IsPublic is true;

        await context.AddAsync(post, cancellationToken);

        return post.Id;
    }
}
