using System;
using System.IO;

namespace AxCopilot.Services;

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

	public static string CreateTempFile(string prefix, string extension)
	{
		Directory.CreateDirectory(TempDir);
		CleanupOldFiles();
		string path = $"{prefix}_{DateTime.Now:yyyyMMdd_HHmmss_fff}{extension}";
		return Path.Combine(TempDir, path);
	}

	private static void CleanupOldFiles()
	{
		try
		{
			if (!Directory.Exists(TempDir))
			{
				return;
			}
			DateTime dateTime = DateTime.Now.AddDays(-1.0);
			foreach (string item in Directory.EnumerateFiles(TempDir))
			{
				try
				{
					if (File.GetCreationTime(item) < dateTime)
					{
						File.Delete(item);
					}
				}
				catch (IOException)
				{
				}
				catch (UnauthorizedAccessException)
				{
				}
			}
		}
		catch (Exception ex3)
		{
			LogService.Debug("임시 파일 정리 중 오류: " + ex3.Message);
		}
	}
}
