using LiteCqrs;
using Microsoft.EntityFrameworkCore;
using TeleWave.Application.Broadcast;
using TeleWave.Application.Common.Interfaces;
using TeleWave.Application.Common.Models;
using TeleWave.Domain.Programming;

namespace TeleWave.Application.Programming.Templates.Junctions;

public sealed class CreateJunctionCommandHandler(IAppDbContext dbContext)
    : ICommandHandler<CreateJunctionCommand, Result<Guid>>
{
    public async Task<Result<Guid>> Handle(
        CreateJunctionCommand command,
        CancellationToken cancellationToken
    )
    {
        if (!await dbContext.Channels.AnyAsync(c => c.Id == command.ChannelId, cancellationToken))
            return Result.Failure<Guid>(ChannelErrors.NotFound);

        var junction = JunctionTemplate.Create(command.ChannelId, command.Name);
        dbContext.JunctionTemplates.Add(junction);
        return Result.Success(junction.Id);
    }
}
