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

namespace TeleWave.Application.Library.SetShowAudience;

public sealed class SetShowAudienceCommandHandler(IAppDbContext dbContext)
    : ICommandHandler<SetShowAudienceCommand, Result>
{
    public async Task<Result> Handle(
        SetShowAudienceCommand 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.SetAudience(command.Audience);
        return Result.Success();
    }
}
