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

namespace TeleWave.Application.Metadata.UpdateShowMetadata;

public sealed class UpdateShowMetadataCommandHandler(IAppDbContext dbContext)
    : ICommandHandler<UpdateShowMetadataCommand, Result>
{
    public async Task<Result> Handle(
        UpdateShowMetadataCommand 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.UpdateMetadataManual(command.Description, command.Year);
        return Result.Success();
    }
}
