using System.Collections.Generic;
using System.Threading.Tasks;
using CliFx;
using CliFx.Attributes;
using CliFx.Infrastructure;
using ZB.MOM.WW.GRAccess.Cli.GRAccess;
using ZB.MOM.WW.GRAccess.Cli.Infrastructure;

namespace ZB.MOM.WW.GRAccess.Cli.Commands.Objects
{
    [Command("template list", Description = "List templates in a galaxy")]
    public class TemplateListCommand : ICommand
    {
        [CommandOption("galaxy", 'g', Description = "Galaxy name", IsRequired = true)]
        public string GalaxyName { get; init; }

        [CommandOption("node", 'n', Description = "GR node name. Blank = local node; . = local machine")]
        public string NodeName { get; init; } = "";

        [CommandOption("pattern", 'p', Description = "Template name pattern for GRAccess namedLike query. Use % as wildcard.")]
        public string Pattern { get; init; } = "%";

        [CommandOption("json", Description = "Output as JSON")]
        public bool Json { get; init; }

        [CommandOption("llm-json", Description = "Output stable machine JSON envelope")]
        public bool LlmJson { get; init; }

        public async ValueTask ExecuteAsync(IConsole console)
        {
            var args = new Dictionary<string, object>
            {
                ["pattern"] = Pattern,
                ["json"] = Json.ToString(),
                ["llm-json"] = LlmJson
            };

            await CommandRouter.ExecuteAsync(
                    console,
                    GalaxyName,
                    NodeName,
                    "template",
                    "list",
                    args,
                    galaxy => GRAccessCommandDispatcher.Execute(galaxy, "template", "list", args))
                .ConfigureAwait(false);
        }
    }
}
