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

namespace TeleWave.Application.Broadcast.Bumpers;

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

        template.SetAudio(command.Extension, command.DurationSeconds);
        return Result.Success();
    }
}
