using System;
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("object list", Description = "List templates and/or instances in a galaxy")]
    public class ObjectListCommand : 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("type", 't', Description = "Object type: all, template, instance")]
        public string Type { get; init; } = "all";

        [CommandOption("pattern", 'p', Description = "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>
            {
                ["type"] = Type,
                ["pattern"] = Pattern,
                ["json"] = Json.ToString(),
                ["llm-json"] = LlmJson
            };

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

        internal static GRAccessObjectKind ParseKind(string value)
        {
            return GRAccessQueryCommandHandler.ParseKind(value);
        }
    }
}
