using MediatR;
using Modules.Library.Application.Commands.CommonModels;
using Modules.Library.Application.Gateways;
using Modules.Library.Domain.Entities.MediaContent.Items.Anime;

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

public class SetPreviewCommand : IRequest<Unit>
{
    public Guid TitleId { get; set; }
    public Guid SeasonId { get; set; }
    public string Url { get; set; } = default!;
    public MediaInfoType Type { get; set; }
    public string ContentType { get; set; } = default!;
}

public class SetPreviewCommandHandler(IAnimeTitleGateway titleGateway) : IRequestHandler<SetPreviewCommand, Unit>
{
    public async Task<Unit> Handle(SetPreviewCommand request, CancellationToken cancellationToken)
    {
        var title = await titleGateway.GetById(request.TitleId);
        var season = title.Items.OfType<AnimeSeason>().FirstOrDefault(q => q.Id == request.SeasonId) ??
            throw new Exception("Season not found");
        season.CommonProperties.SetPreview(request.Url, (Domain.Entities.MediaInfoType)request.Type, request.ContentType);
        await titleGateway.Update(title);
        return Unit.Value;
    }
}