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

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

public class SetEstimatedReleaseDateCommand : IRequest<Unit>
{
    public Guid TitleId { get; init; }
    public Guid SeasonId { get; init; }
    public DateTimeOffset? Value { get; init; }

}

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