using MediatR;
using Modules.Library.Application.Gateways;
using Modules.Library.Application.Services;

namespace Modules.Library.Application.Commands.Anime.Title.Properties.Preview;

public class DeletePreviewCommand : IRequest<Unit>
{
    public Guid TitleId { get; set; }
}

public class DeletePreviewCommandHandler(IAnimeTitleGateway titleGateway, MediaInfoService mediaInfoService) : IRequestHandler<DeletePreviewCommand, Unit>
{
    public async Task<Unit> Handle(DeletePreviewCommand request, CancellationToken cancellationToken)
    {
        var title = await titleGateway.GetById(request.TitleId);
        var objectId = title.CommonProperties.Preview?.ObjectId;
        if (!string.IsNullOrWhiteSpace(objectId))
        {
            var deleted = await mediaInfoService.Remove(objectId, true, cancellationToken);
            if (deleted)
            {
                title.CommonProperties.DeletePreview();
                await titleGateway.Update(title);
            }
        }
        return Unit.Value;
    }
}