using Sbroenne.ExcelMcp.ComInterop.Session;

namespace Sbroenne.ExcelMcp.Core.Tests.Helpers;

/// <summary>
/// Shared helper for Core integration tests.
/// Provides utilities for creating unique test files to ensure test isolation.
/// </summary>
public static class CoreTestHelper
{
    /// <summary>
    /// Creates a unique test file for isolated testing.
    /// Supports Excel files (.xlsx, .xlsm) and data files (.csv, .txt, etc.).
    /// </summary>
    /// <param name="testClassName">Name of the test class (e.g., "NamedRangeCommandsTests")</param>
    /// <param name="testName">Name of the test method (e.g., "Create_WithValidParameter_ReturnsSuccess")</param>
    /// <param name="tempDir">Temporary directory where the file will be created</param>
    /// <param name="extension">File extension including dot (e.g., ".xlsx", ".csv", ".txt")</param>
    /// <param name="content">Optional file content. Only used for non-Excel files. For CSV, defaults to sample data.</param>
    /// <returns>Absolute path to the created file</returns>
    /// <exception cref="InvalidOperationException">Thrown if Excel file creation fails</exception>
    /// <remarks>
    /// Usage patterns:
    /// <code>
    /// // Excel file
    /// var excelFile = CoreTestHelper.CreateUniqueTestFile(
    ///     nameof(MyTests), nameof(MyTest), _tempDir, ".xlsx");
    ///
    /// // CSV file with default data
    /// var csvFile = CoreTestHelper.CreateUniqueTestFile(
    ///     nameof(MyTests), nameof(MyTest), _tempDir, ".csv");
    ///
    /// // CSV file with custom data
    /// var csvFile = CoreTestHelper.CreateUniqueTestFile(
    ///     nameof(MyTests), nameof(MyTest), _tempDir, ".csv", "Col1,Col2\nA,B");
    /// </code>
    /// </remarks>
    public static string CreateUniqueTestFile(
        string testClassName,
        string testName,
        string tempDir,
        string extension = ".xlsx",
        string? content = null)
    {
        // Generate unique filename: ClassName_TestName_GUID.{extension}
        var fileName = $"{testClassName}_{testName}_{Guid.NewGuid():N}{extension}";
        var filePath = Path.Join(tempDir, fileName);

        // Handle Excel files (.xlsx, .xlsm)
        if (extension is ".xlsx" or ".xlsm")
        {
            try
            {
                // Use SessionManager to create file and immediately close the session
                using var manager = new SessionManager();
                var sessionId = manager.CreateSessionForNewFile(filePath, show: false);
                manager.CloseSession(sessionId, save: true);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(
                    $"Failed to create test Excel file '{filePath}': {ex.Message}. " +
                    "Excel may not be installed or the path may be invalid.", ex);
            }
        }
        // Handle data files (.csv, .txt, etc.)
        else
        {
            const string defaultContent = "Name,Value\nSample,123\n";
            var finalContent = content ?? defaultContent;
            File.WriteAllText(filePath, finalContent);
        }

        return filePath;
    }
}




