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

namespace TeleWave.Application.Metadata.ClearShowMetadata;

public sealed class ClearShowMetadataCommandHandler(IAppDbContext dbContext)
    : ICommandHandler<ClearShowMetadataCommand, Result>
{
    public async Task<Result> Handle(
        ClearShowMetadataCommand 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.ClearMetadata();
        return Result.Success();
    }
}
