using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace AxCopilot.Services.Agent;

public class BatchSkill : IAgentTool
{
	private static readonly string[] BlockedCommands = new string[23]
	{
		"reg ", "reg.exe", "regedit", "sc ", "sc.exe", "net stop", "net start", "net user", "bcdedit", "diskpart",
		"format ", "shutdown", "schtasks", "wmic", "powercfg", "Set-Service", "Stop-Service", "Start-Service", "New-Service", "Remove-Service",
		"Set-ItemProperty.*HKLM", "Set-ItemProperty.*HKCU", "Remove-Item.*-Recurse.*-Force"
	};

	public string Name => "script_create";

	public string Description => "Create a batch (.bat) or PowerShell (.ps1) script file. The script is ONLY created, NOT executed. System-level commands are blocked.";

	public ToolParameterSchema Parameters
	{
		get
		{
			ToolParameterSchema obj = new ToolParameterSchema
			{
				Properties = new Dictionary<string, ToolProperty>
				{
					["path"] = new ToolProperty
					{
						Type = "string",
						Description = "Output file path (.bat or .ps1). Relative to work folder."
					},
					["content"] = new ToolProperty
					{
						Type = "string",
						Description = "Script content. Each line should have Korean comments explaining the command."
					},
					["description"] = new ToolProperty
					{
						Type = "string",
						Description = "Brief description of what this script does."
					}
				}
			};
			int num = 2;
			List<string> list = new List<string>(num);
			CollectionsMarshal.SetCount(list, num);
			Span<string> span = CollectionsMarshal.AsSpan(list);
			span[0] = "path";
			span[1] = "content";
			obj.Required = list;
			return obj;
		}
	}

	public async Task<ToolResult> ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct)
	{
		string path = args.GetProperty("path").GetString() ?? "";
		string content = args.GetProperty("content").GetString() ?? "";
		JsonElement d;
		string desc = (args.TryGetProperty("description", out d) ? (d.GetString() ?? "") : "");
		string fullPath = FileReadTool.ResolvePath(path, context.WorkFolder);
		if (context.ActiveTab == "Cowork")
		{
			fullPath = AgentContext.EnsureTimestampedPath(fullPath);
		}
		string ext = Path.GetExtension(fullPath).ToLowerInvariant();
		if (ext != ".bat" && ext != ".ps1" && ext != ".cmd")
		{
			return ToolResult.Fail("지원하는 스크립트 형식: .bat, .cmd, .ps1");
		}
		if (!context.IsPathAllowed(fullPath))
		{
			return ToolResult.Fail("경로 접근 차단: " + fullPath);
		}
		string contentLower = content.ToLowerInvariant();
		string[] blockedCommands = BlockedCommands;
		foreach (string blocked in blockedCommands)
		{
			if (contentLower.Contains(blocked.ToLowerInvariant()))
			{
				return ToolResult.Fail("시스템 수준 명령이 포함되어 차단됨: " + blocked.Trim());
			}
		}
		if (!(await context.CheckWritePermissionAsync(Name, fullPath)))
		{
			return ToolResult.Fail("쓰기 권한 거부: " + fullPath);
		}
		try
		{
			string dir = Path.GetDirectoryName(fullPath);
			if (!string.IsNullOrEmpty(dir))
			{
				Directory.CreateDirectory(dir);
			}
			StringBuilder sb = new StringBuilder();
			if (!string.IsNullOrEmpty(desc))
			{
				string commentPrefix = ((ext == ".ps1") ? "#" : "REM");
				StringBuilder stringBuilder = sb;
				StringBuilder stringBuilder2 = stringBuilder;
				StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(9, 2, stringBuilder);
				handler.AppendFormatted(commentPrefix);
				handler.AppendLiteral(" === ");
				handler.AppendFormatted(desc);
				handler.AppendLiteral(" ===");
				stringBuilder2.AppendLine(ref handler);
				stringBuilder = sb;
				StringBuilder stringBuilder3 = stringBuilder;
				handler = new StringBuilder.AppendInterpolatedStringHandler(32, 1, stringBuilder);
				handler.AppendFormatted(commentPrefix);
				handler.AppendLiteral(" 이 스크립트는 AX Copilot에 의해 생성되었습니다.");
				stringBuilder3.AppendLine(ref handler);
				stringBuilder = sb;
				StringBuilder stringBuilder4 = stringBuilder;
				handler = new StringBuilder.AppendInterpolatedStringHandler(20, 1, stringBuilder);
				handler.AppendFormatted(commentPrefix);
				handler.AppendLiteral(" 실행 전 내용을 반드시 확인하세요.");
				stringBuilder4.AppendLine(ref handler);
				sb.AppendLine();
			}
			sb.Append(content);
			await File.WriteAllTextAsync(fullPath, sb.ToString(), new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), ct);
			return ToolResult.Ok($"스크립트 파일 생성 완료: {fullPath}\n형식: {ext}, 설명: {(string.IsNullOrEmpty(desc) ? "(없음)" : desc)}\n⚠ 자동 실행되지 않습니다. 내용을 확인한 후 직접 실행하세요.", fullPath);
		}
		catch (Exception ex)
		{
			return ToolResult.Fail("스크립트 생성 실패: " + ex.Message);
		}
	}
}
