using Newtonsoft.Json.Linq;
namespace GxMcp.Gateway.Routers
{
    public class SystemRouter : IMcpModuleRouter
    {
        public string ModuleName => "System";

        public object? ConvertToolCall(string toolName, JObject? args)
        {
            string? action = args?["action"]?.ToString();
            string? target = args?["target"]?.ToString();

            switch (toolName)
            {
                case "genexus_lifecycle":
                    switch (action)
                    {
                        case "build": return new {
                            module = "Build",
                            action = "Build",
                            target = target,
                            includeCallees = args?["includeCallees"]?.ToString(),
                            buildPlanCap = args?["buildPlanCap"]?.ToObject<int?>()
                        };
                        case "cancel": return new { module = "Build", action = "Cancel", target = target };
                        case "rebuild": return new { module = "Build", action = "RebuildAll", target = target };
                        case "reorg": return new { module = "Build", action = "Reorg", target = target };
                        case "validate": return new { module = "Validation", action = "Check", target = target, payload = args?["code"]?.ToString() };
                        case "validate-kb": return new { module = "KB", action = "ValidateConditions", limit = args?["limit"]?.ToObject<int?>() };
                        case "snapshots-list": return new { module = "KB", action = "ListPatternSnapshots", target = target };
                        case "snapshots-restore": return new { module = "KB", action = "RestorePatternSnapshot", target = target, snapshotPath = args?["snapshotPath"]?.ToString() };
                        case "sync": return new { module = "Build", action = "Sync", target = target };
                        case "index": return new {
                            module = "KB",
                            action = "BulkIndex",
                            force = args?["force"]?.ToObject<bool?>() ?? false
                        };
                        case "status":
                            if (!string.IsNullOrEmpty(target))
                            {
                                int? page = args?["page"]?.ToObject<int?>();
                                int? pageSize = args?["pageSize"]?.ToObject<int?>() ?? args?["page_size"]?.ToObject<int?>();
                                return new { module = "Build", action = "Status", target = target, page = page ?? 1, pageSize = pageSize ?? 50 };
                            }
                            return new { module = "KB", action = "GetIndexStatus" };
                        case "result":
                            if (!string.IsNullOrEmpty(target))
                            {
                                int? page = args?["page"]?.ToObject<int?>();
                                int? pageSize = args?["pageSize"]?.ToObject<int?>() ?? args?["page_size"]?.ToObject<int?>();
                                return new { module = "Build", action = "Result", target = target, page = page ?? 1, pageSize = pageSize ?? 50 };
                            }
                            return null;
                        default: return null;
                    }

                case "genexus_forge":
                    switch (action)
                    {
                        case "scaffold":
                            return new {
                                module = "Forge",
                                action = "Scaffold",
                                type = args?["type"]?.ToString(),
                                name = args?["name"]?.ToString(),
                                code = args?["content"]?.ToString(),
                                description = args?["description"]?.ToString()
                            };
                        case "translate":
                            return new {
                                module = "Conversion",
                                action = "TranslateTo",
                                target = args?["name"]?.ToString(),
                                language = args?["content"]?.ToString()
                            };
                        case "sample": return new { module = "Pattern", action = "GetSample", target = args?["type"]?.ToString() };
                        default: return null;
                    }

                case "genexus_doc":
                    switch (action)
                    {
                        case "wiki": return new { module = "Wiki", action = "Generate", target = target };
                        case "visualize": return new { module = "Visualizer", action = "Generate", target = target };
                        case "health": return new { module = "Health", action = "GetReport" };
                        default: return null;
                    }

                case "genexus_test":
                    return new { module = "Test", action = "Run", target = args?["name"]?.ToString() };
                
                // Legados
                case "genexus_validate":
                    return new { module = "Validation", action = "Check", target = target, payload = args?["code"]?.ToString() };
                case "genexus_build":
                    return new { module = "Build", action = args?["action"]?.ToString(), target = target };
                case "genexus_history":
                    return new {
                        module = "History",
                        action = args?["action"]?.ToString(),
                        target = args?["name"]?.ToString(),
                        versionId = args?["versionId"]?.ToObject<int?>()
                    };

                default:
                    return null;
            }
        }
    }
}
