/*
 * Magic Cloud, copyright (c) 2023 Thomas Hansen. See the attached LICENSE file for details. For license inquiries you can send an email to thomas@ainiro.io
 */

using System.Threading.Tasks;
using magic.node;
using magic.signals.contracts;
using magic.lambda.scheduler.contracts;

namespace magic.lambda.scheduler.slots.tasks
{
    /// <summary>
    /// [tasks.update] slot that will update an existing task.
    /// </summary>
    [Slot(
        Name = "tasks.update",
        Description = "Overwrites a stored task's body or description without altering its existing schedules",
        ValueKind = "task-id",
        ValueDescription = "Task ID to update",
        ValueRequired = false,
        ValueMode = SlotValueMode.ValueOrExpression,
        ReturnsMode = SlotReturnsMode.None,
        ClonesLambda = true,
        SignatureType = typeof(global::magic.lambda.scheduler.signatures.CreateTaskSignature))]
    public class UpdateTask : ISlotAsync
    {
        readonly ITaskStorage _storage;

        /// <summary>
        /// Creates a new instance of your slot.
        /// </summary>
        /// <param name="storage">Storage to use for tasks.</param>
        public UpdateTask(ITaskStorage storage)
        {
            _storage = storage;
        }

        /// <summary>
        /// Slot implementation.
        /// </summary>
        /// <param name="signaler">Signaler that raised signal.</param>
        /// <param name="input">Arguments to slot.</param>
        /// <returns>Awaitable task.</returns>
        public async Task SignalAsync(ISignaler signaler, Node input)
        {
            await _storage.UpdateTaskAsync(CreateTask.Create(signaler, input));
            input.Clear();
            input.Value = null;
        }
    }
}
