// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Teams.Api.Activities;
using Microsoft.Teams.Api.Activities.Invokes;
using Microsoft.Teams.Apps.Routing;

namespace Microsoft.Teams.Apps.Activities.Invokes;

public static partial class MessageExtension
{
    [AttributeUsage(AttributeTargets.Method, Inherited = true)]
    public class QueryAttribute() : InvokeAttribute(Api.Activities.Invokes.Name.MessageExtensions.Query, typeof(MessageExtensions.QueryActivity))
    {
        public override object Coerce(IContext<IActivity> context) => context.ToActivityType<MessageExtensions.QueryActivity>();
    }
}

public static partial class AppInvokeActivityExtensions
{
    public static App OnQuery(this App app, Func<IContext<MessageExtensions.QueryActivity>, Task> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.MessageExtensions.Query]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context =>
            {
                await handler(context.ToActivityType<MessageExtensions.QueryActivity>());
                return null;
            },
            Selector = activity => activity is MessageExtensions.QueryActivity
        });

        return app;
    }

    public static App OnQuery(this App app, Func<IContext<MessageExtensions.QueryActivity>, Task<Response<Api.MessageExtensions.Response>>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.MessageExtensions.Query]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<MessageExtensions.QueryActivity>()),
            Selector = activity => activity is MessageExtensions.QueryActivity
        });

        return app;
    }

    public static App OnQuery(this App app, Func<IContext<MessageExtensions.QueryActivity>, Task<Api.MessageExtensions.Response>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.MessageExtensions.Query]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<MessageExtensions.QueryActivity>()),
            Selector = activity => activity is MessageExtensions.QueryActivity
        });

        return app;
    }

    public static App OnQuery(this App app, Func<IContext<MessageExtensions.QueryActivity>, CancellationToken, Task> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.MessageExtensions.Query]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context =>
            {
                await handler(context.ToActivityType<MessageExtensions.QueryActivity>(), context.CancellationToken);
                return null;
            },
            Selector = activity => activity is MessageExtensions.QueryActivity
        });

        return app;
    }

    public static App OnQuery(this App app, Func<IContext<MessageExtensions.QueryActivity>, CancellationToken, Task<Response<Api.MessageExtensions.Response>>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.MessageExtensions.Query]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<MessageExtensions.QueryActivity>(), context.CancellationToken),
            Selector = activity => activity is MessageExtensions.QueryActivity
        });

        return app;
    }

    public static App OnQuery(this App app, Func<IContext<MessageExtensions.QueryActivity>, CancellationToken, Task<Api.MessageExtensions.Response>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.MessageExtensions.Query]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<MessageExtensions.QueryActivity>(), context.CancellationToken),
            Selector = activity => activity is MessageExtensions.QueryActivity
        });

        return app;
    }
}