using Intentor.Models;
using Intentor.Services;

namespace Intentor.Logic;

public interface IIntentActionExecutor
{
    Task ExecuteAsync(Intent intent, CancellationToken ct = default);
}

public sealed class IntentActionExecutor : IIntentActionExecutor
{
    private readonly HomeAssistantService _homeAssistant;

    public IntentActionExecutor(HomeAssistantService homeAssistant) => _homeAssistant = homeAssistant;

    public Task ExecuteAsync(Intent intent, CancellationToken ct = default)
    {
        if (intent.Action is null)
            throw new InvalidOperationException("Intent.Action is required.");

        return intent.Action.Type switch
        {
            HomeAssistantActionType.ActivateScene => _homeAssistant.ActivateSceneAsync(intent.Action.EntityId, ct),
            HomeAssistantActionType.RunScript => _homeAssistant.RunScriptAsync(intent.Action.EntityId, ct),
            _ => throw new NotSupportedException($"Unsupported action type: {intent.Action.Type}")
        };
    }
}
