using LiteCqrs;
using Microsoft.EntityFrameworkCore;
using TeleWave.Application.Common.Interfaces;
using TeleWave.Application.Common.Models;
using TeleWave.Application.Library;

namespace TeleWave.Application.Metadata.SetShowPoster;

public sealed class SetShowPosterCommandHandler(IAppDbContext dbContext)
    : ICommandHandler<SetShowPosterCommand, Result>
{
    public async Task<Result> Handle(
        SetShowPosterCommand command,
        CancellationToken cancellationToken
    )
    {
        var show = await dbContext.Shows.FirstOrDefaultAsync(
            s => s.Id == command.ShowId,
            cancellationToken
        );
        if (show is null)
            return Result.Failure(ShowErrors.NotFound);

        show.SetPosterImage(command.ImageId);
        return Result.Success();
    }
}
