using System.IO;
using System.Text;
using System.Text.Json;

namespace AxCopilot.Services.Agent;

/// <summary>
/// 지정 경로의 파일 변경을 감지하고 변경 내역을 반환하는 도구.
/// FileSystemInfo의 타임스탬프 기반으로 최근 변경 파일을 조회합니다.
/// </summary>
public class FileWatchTool : IAgentTool
{
    public string Name => "file_watch";
    public string Description =>
        "Detect recent file changes in a folder. Returns a list of created, modified, and deleted files " +
        "since a given time. Useful for monitoring data folders, detecting log updates, " +
        "or tracking file system changes.";

    public ToolParameterSchema Parameters => new()
    {
        Properties = new()
        {
            ["path"] = new()
            {
                Type = "string",
                Description = "Folder path to watch. Relative to work folder."
            },
            ["pattern"] = new()
            {
                Type = "string",
                Description = "File pattern filter (e.g. '*.csv', '*.log', '*.xlsx'). Default: '*' (all files)"
            },
            ["since"] = new()
            {
                Type = "string",
                Description = "Time threshold: ISO 8601 datetime (e.g. '2026-03-30T09:00:00') " +
                    "or relative duration ('1h', '6h', '24h', '7d', '30d'). Default: '24h'"
            },
            ["recursive"] = new()
            {
                Type = "boolean",
                Description = "Search subdirectories recursively. Default: true"
            },
            ["include_size"] = new()
            {
                Type = "boolean",
                Description = "Include file sizes in output. Default: true"
            },
            ["top_n"] = new()
            {
                Type = "integer",
                Description = "Limit results to most recent N files. Default: 50"
            },
        },
        Required = ["path"]
    };

    public Task<ToolResult> ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct)
    {
        var path = args.GetProperty("path").GetString() ?? "";
        var pattern = args.TryGetProperty("pattern", out var patEl) ? patEl.GetString() ?? "*" : "*";
        var sinceStr = args.TryGetProperty("since", out var sinceEl) ? sinceEl.GetString() ?? "24h" : "24h";
        var recursive = !args.TryGetProperty("recursive", out var recEl) || recEl.GetBoolean(); // default true
        var includeSize = !args.TryGetProperty("include_size", out var sizeEl) || sizeEl.GetBoolean();
        var topN = args.TryGetProperty("top_n", out var topEl) && topEl.TryGetInt32(out var n) ? n : 50;

        var fullPath = FileReadTool.ResolvePath(path, context.WorkFolder);
        if (!context.IsPathAllowed(fullPath))
            return Task.FromResult(ToolResult.Fail($"경로 접근 차단: {fullPath}"));
        if (!Directory.Exists(fullPath))
            return Task.FromResult(ToolResult.Fail($"폴더 없음: {fullPath}"));

        try
        {
            var since = ParseSince(sinceStr);
            var searchOpt = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

            var files = Directory.GetFiles(fullPath, pattern, searchOpt)
                .Select(f => new FileInfo(f))
                .Where(fi => fi.LastWriteTime >= since || fi.CreationTime >= since)
                .OrderByDescending(fi => fi.LastWriteTime)
                .Take(topN)
                .ToList();

            if (files.Count == 0)
                return Task.FromResult(ToolResult.Ok(
                    $"📂 {sinceStr} 이내 변경된 파일이 없습니다. (경로: {path}, 패턴: {pattern})"));

            var sb = new StringBuilder();
            sb.AppendLine($"📂 파일 변경 감지: {files.Count}개 파일 ({sinceStr} 이내)");
            sb.AppendLine($"   경로: {path} | 패턴: {pattern}");
            sb.AppendLine();

            // 생성/수정 분류
            var created = files.Where(f => f.CreationTime >= since && f.CreationTime == f.LastWriteTime).ToList();
            var modified = files.Where(f => f.LastWriteTime >= since && f.CreationTime < since).ToList();
            var recentlyCreated = files.Where(f => f.CreationTime >= since && f.CreationTime != f.LastWriteTime).ToList();

            if (created.Count > 0)
            {
                sb.AppendLine($"🆕 신규 생성 ({created.Count}개):");
                foreach (var f in created)
                    AppendFileInfo(sb, f, fullPath, includeSize);
                sb.AppendLine();
            }

            if (modified.Count > 0)
            {
                sb.AppendLine($"✏️ 수정됨 ({modified.Count}개):");
                foreach (var f in modified)
                    AppendFileInfo(sb, f, fullPath, includeSize);
                sb.AppendLine();
            }

            if (recentlyCreated.Count > 0)
            {
                sb.AppendLine($"📝 생성 후 수정됨 ({recentlyCreated.Count}개):");
                foreach (var f in recentlyCreated)
                    AppendFileInfo(sb, f, fullPath, includeSize);
                sb.AppendLine();
            }

            // 요약 통계
            var totalSize = files.Sum(f => f.Length);
            sb.AppendLine($"── 요약: 총 {files.Count}개 파일, {FormatSize(totalSize)}");

            // 파일 유형별 분포
            var byExt = files.GroupBy(f => f.Extension.ToLowerInvariant())
                .OrderByDescending(g => g.Count())
                .Take(10);
            sb.Append("   유형: ");
            sb.AppendLine(string.Join(", ", byExt.Select(g => $"{g.Key}({g.Count()})")));

            return Task.FromResult(ToolResult.Ok(sb.ToString()));
        }
        catch (Exception ex)
        {
            return Task.FromResult(ToolResult.Fail($"파일 감시 실패: {ex.Message}"));
        }
    }

    private static DateTime ParseSince(string since)
    {
        if (DateTime.TryParse(since, out var dt))
            return dt;

        // 상대 시간: "1h", "24h", "7d", "30d"
        var match = System.Text.RegularExpressions.Regex.Match(since, @"^(\d+)(h|d|m)$");
        if (match.Success)
        {
            var amount = int.Parse(match.Groups[1].Value);
            var unit = match.Groups[2].Value;
            return unit switch
            {
                "h" => DateTime.Now.AddHours(-amount),
                "d" => DateTime.Now.AddDays(-amount),
                "m" => DateTime.Now.AddMinutes(-amount),
                _ => DateTime.Now.AddHours(-24)
            };
        }

        return DateTime.Now.AddHours(-24); // default: 24시간
    }

    private static void AppendFileInfo(StringBuilder sb, FileInfo f, string basePath, bool includeSize)
    {
        var relPath = Path.GetRelativePath(basePath, f.FullName);
        var timeStr = f.LastWriteTime.ToString("MM-dd HH:mm");
        if (includeSize)
            sb.AppendLine($"  {relPath}  ({FormatSize(f.Length)}, {timeStr})");
        else
            sb.AppendLine($"  {relPath}  ({timeStr})");
    }

    private static string FormatSize(long bytes)
    {
        if (bytes < 1024) return $"{bytes}B";
        if (bytes < 1024 * 1024) return $"{bytes / 1024.0:F1}KB";
        if (bytes < 1024 * 1024 * 1024) return $"{bytes / (1024.0 * 1024):F1}MB";
        return $"{bytes / (1024.0 * 1024 * 1024):F2}GB";
    }
}
