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

namespace TeleWave.Application.Library.Collections.UpdateCollection;

public sealed class UpdateCollectionCommandHandler(IAppDbContext dbContext)
    : ICommandHandler<UpdateCollectionCommand, Result>
{
    public async Task<Result> Handle(
        UpdateCollectionCommand command,
        CancellationToken cancellationToken
    )
    {
        var collection = await dbContext.Collections.FirstOrDefaultAsync(
            c => c.Id == command.CollectionId,
            cancellationToken
        );
        if (collection is null)
            return Result.Failure(CollectionErrors.NotFound);

        collection.Rename(command.Name, command.Description);
        return Result.Success();
    }
}
