// ------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
// </auto-generated>
// ------------------------------------------------------------------------------

#nullable enable
#pragma warning disable IDE0005 // Extra using directive is ok.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Workflows;
using Microsoft.Agents.AI.Workflows.Declarative;
using Microsoft.Agents.AI.Workflows.Declarative.Kit;
using Microsoft.Extensions.AI;

namespace Test.WorkflowProviders;

/// <summary>
/// This class provides a factory method to create a <see cref="Workflow" /> instance.
/// </summary>
/// <remarks>
/// The workflow defined here was generated from a declarative workflow definition.
/// Declarative workflows utilize Power FX for defining conditions and expressions.
/// To learn more about Power FX, see:
/// https://learn.microsoft.com/power-platform/power-fx/formula-reference-copilot-studio
/// </remarks>
public static class WorkflowProvider
{
    /// <summary>
    /// The root executor for a declarative workflow.
    /// </summary>
    internal sealed class MyWorkflowRootExecutor<TInput>(
        DeclarativeWorkflowOptions options,
        Func<TInput, ChatMessage> inputTransform) :
        RootExecutor<TInput>("my_workflow_Root", options, inputTransform)
        where TInput : notnull
    {
        protected override async ValueTask ExecuteAsync(TInput message, IWorkflowContext context, CancellationToken cancellationToken)
        {
        }
    }
    
    /// <summary>
    /// Formats a message template and sends an activity event.
    /// </summary>
    internal sealed class SendActivity1Executor(FormulaSession session) : ActionExecutor(id: "send_activity_1", session)
    {
        // <inheritdoc />
        protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
        {
            string activityText =
                await context.FormatTemplateAsync(
                    """
                    NEVER 1!
                    """
                );
            AgentResponse response = new([new ChatMessage(ChatRole.Assistant, activityText)]);
            await context.AddEventAsync(new AgentResponseEvent(this.Id, response)).ConfigureAwait(false);
    
            return default;
        }
    }
    
    /// <summary>
    /// Formats a message template and sends an activity event.
    /// </summary>
    internal sealed class SendActivity2Executor(FormulaSession session) : ActionExecutor(id: "send_activity_2", session)
    {
        // <inheritdoc />
        protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
        {
            string activityText =
                await context.FormatTemplateAsync(
                    """
                    NEVER 2!
                    """
                );
            AgentResponse response = new([new ChatMessage(ChatRole.Assistant, activityText)]);
            await context.AddEventAsync(new AgentResponseEvent(this.Id, response)).ConfigureAwait(false);
    
            return default;
        }
    }
    
    /// <summary>
    /// Formats a message template and sends an activity event.
    /// </summary>
    internal sealed class SendActivity3Executor(FormulaSession session) : ActionExecutor(id: "send_activity_3", session)
    {
        // <inheritdoc />
        protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken)
        {
            string activityText =
                await context.FormatTemplateAsync(
                    """
                    NEVER 3!
                    """
                );
            AgentResponse response = new([new ChatMessage(ChatRole.Assistant, activityText)]);
            await context.AddEventAsync(new AgentResponseEvent(this.Id, response)).ConfigureAwait(false);
    
            return default;
        }
    }
    
    public static Workflow CreateWorkflow<TInput>(
        DeclarativeWorkflowOptions options,
        Func<TInput, ChatMessage>? inputTransform = null) 
        where TInput : notnull
    {
        // Create root executor to initialize the workflow.
        inputTransform ??= (message) => DeclarativeWorkflowBuilder.DefaultTransform(message);
        MyWorkflowRootExecutor<TInput> myWorkflowRoot = new(options, inputTransform);
        DelegateExecutor myWorkflow = new(id: "my_workflow", myWorkflowRoot.Session);
        DelegateExecutor gotoEnd = new(id: "goto_end", myWorkflowRoot.Session);
        DelegateExecutor endAll = new(id: "end_all", myWorkflowRoot.Session);
        DelegateExecutor gotoEndRestart = new(id: "goto_end_Restart", myWorkflowRoot.Session);
        SendActivity1Executor sendActivity1 = new(myWorkflowRoot.Session);
        SendActivity2Executor sendActivity2 = new(myWorkflowRoot.Session);
        SendActivity3Executor sendActivity3 = new(myWorkflowRoot.Session);

        // Define the workflow builder
        WorkflowBuilder builder = new(myWorkflowRoot);

        // Connect executors
        builder.AddEdge(myWorkflowRoot, myWorkflow);
        builder.AddEdge(myWorkflow, gotoEnd);
        builder.AddEdge(gotoEnd, endAll);
        builder.AddEdge(gotoEndRestart, sendActivity1);
        builder.AddEdge(sendActivity1, sendActivity2);
        builder.AddEdge(sendActivity2, sendActivity3);
        builder.AddEdge(sendActivity3, endAll);

        // Build the workflow
        return builder.Build(validateOrphans: false);
    }
}
