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

namespace TeleWave.Application.Library.RenameShow;

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

        show.SetName(command.Name.Trim());
        return Result.Success();
    }
}
