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

namespace TeleWave.Application.Broadcast.Bumpers;

public sealed class AddBumperVariantCommandHandler(IAppDbContext dbContext)
    : ICommandHandler<AddBumperVariantCommand, Result<Guid>>
{
    public async Task<Result<Guid>> Handle(
        AddBumperVariantCommand command,
        CancellationToken cancellationToken
    )
    {
        var template = await BumperTemplateLoader.LoadAsync(
            dbContext,
            command.TemplateId,
            cancellationToken
        );
        if (template is null)
            return Result.Failure<Guid>(BumperErrors.TemplateNotFound);

        var variant = template.AddVariant(command.Name);
        variant.SetLines(BumperTemplateLoader.DefaultLines());
        return Result.Success(variant.Id);
    }
}
