using System;
using System.IO;
using System.Linq;

namespace AxCopilot.Services;

public static class StorageAnalyzer
{
	private static readonly string AppDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AxCopilot");

	public static StorageReport Analyze()
	{
		StorageReport storageReport = new StorageReport();
		storageReport.Conversations = GetFolderSize(Path.Combine(AppDataDir, "conversations"));
		storageReport.AuditLogs = GetFolderSize(Path.Combine(AppDataDir, "audit"));
		storageReport.Logs = GetFolderSize(Path.Combine(AppDataDir, "logs"));
		storageReport.CodeIndex = GetFolderSize(Path.Combine(AppDataDir, "code_index"));
		storageReport.EmbeddingDb = GetFolderSize(Path.Combine(AppDataDir, "embeddings"));
		storageReport.ClipboardHistory = GetFileSize(Path.Combine(AppDataDir, "clipboard_history.dat"));
		storageReport.Plugins = GetFolderSize(Path.Combine(AppDataDir, "plugins"));
		storageReport.Skills = GetFolderSize(Path.Combine(AppDataDir, "skills"));
		storageReport.Settings = GetFileSize(Path.Combine(AppDataDir, "settings.json"));
		string path = Path.GetDirectoryName(Environment.ProcessPath) ?? AppDataDir;
		try
		{
			DriveInfo driveInfo = new DriveInfo(Path.GetPathRoot(path) ?? "C:");
			storageReport.DriveLabel = driveInfo.Name;
			storageReport.DriveFreeSpace = driveInfo.AvailableFreeSpace;
			storageReport.DriveTotalSpace = driveInfo.TotalSize;
		}
		catch
		{
		}
		return storageReport;
	}

	public static long Cleanup(int retainDays, bool cleanConversations, bool cleanAuditLogs, bool cleanLogs, bool cleanCodeIndex, bool cleanClipboard)
	{
		long num = 0L;
		DateTime cutoff = DateTime.Now.AddDays(-retainDays);
		if (cleanConversations)
		{
			num += CleanFolder(Path.Combine(AppDataDir, "conversations"), cutoff, retainDays == 0);
		}
		if (cleanAuditLogs)
		{
			num += CleanFolder(Path.Combine(AppDataDir, "audit"), cutoff, retainDays == 0);
		}
		if (cleanLogs)
		{
			num += CleanFolder(Path.Combine(AppDataDir, "logs"), cutoff, retainDays == 0);
		}
		if (cleanCodeIndex)
		{
			num += CleanFolder(Path.Combine(AppDataDir, "code_index"), cutoff, deleteAll: true);
		}
		if (cleanClipboard)
		{
			string text = Path.Combine(AppDataDir, "clipboard_history.dat");
			if (File.Exists(text))
			{
				num += new FileInfo(text).Length;
				File.Delete(text);
			}
		}
		return num;
	}

	private static long GetFolderSize(string path)
	{
		if (!Directory.Exists(path))
		{
			return 0L;
		}
		try
		{
			return Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Sum(delegate(string f)
			{
				try
				{
					return new FileInfo(f).Length;
				}
				catch
				{
					return 0L;
				}
			});
		}
		catch
		{
			return 0L;
		}
	}

	private static long GetFileSize(string path)
	{
		try
		{
			return File.Exists(path) ? new FileInfo(path).Length : 0;
		}
		catch
		{
			return 0L;
		}
	}

	private static long CleanFolder(string path, DateTime cutoff, bool deleteAll)
	{
		if (!Directory.Exists(path))
		{
			return 0L;
		}
		long num = 0L;
		foreach (string item in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
		{
			try
			{
				FileInfo fileInfo = new FileInfo(item);
				if (deleteAll || fileInfo.LastWriteTime < cutoff)
				{
					num += fileInfo.Length;
					fileInfo.Delete();
				}
			}
			catch
			{
			}
		}
		if (deleteAll)
		{
			try
			{
				Directory.Delete(path, recursive: true);
				Directory.CreateDirectory(path);
			}
			catch
			{
			}
		}
		return num;
	}

	public static string FormatSize(long bytes)
	{
		if (bytes >= 1024)
		{
			if (bytes >= 1048576)
			{
				if (bytes >= 1073741824)
				{
					return $"{(double)bytes / 1073741824.0:F2} GB";
				}
				return $"{(double)bytes / 1048576.0:F1} MB";
			}
			return $"{(double)bytes / 1024.0:F1} KB";
		}
		return $"{bytes} B";
	}
}
