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

namespace TeleWave.Application.Programming.Templates.Generate;

public sealed class PreviewGeneratedGridQueryHandler(GridPlanner planner)
    : IQueryHandler<PreviewGeneratedGridQuery, Result<GridPlanDto>>
{
    public async Task<Result<GridPlanDto>> Handle(
        PreviewGeneratedGridQuery query,
        CancellationToken cancellationToken
    )
    {
        var plan = await planner.BuildAsync(
            query.ChannelId,
            query.Profile,
            query.Mode,
            cancellationToken
        );

        return plan.IsSuccess
            ? Result.Success(plan.Value.ToDto())
            : Result.Failure<GridPlanDto>(plan.Error);
    }
}
