namespace LiquidCode.Tester.Worker.Services;

/// <summary>
/// Factory for execution services - always uses Isolate sandbox for security
/// </summary>
public class ExecutionServiceFactory : IExecutionServiceFactory
{
    private readonly IServiceProvider _serviceProvider;
    private readonly ILogger<ExecutionServiceFactory> _logger;

    public ExecutionServiceFactory(
        IServiceProvider serviceProvider,
        ILogger<ExecutionServiceFactory> logger)
    {
        _serviceProvider = serviceProvider;
        _logger = logger;

        _logger.LogInformation("Execution services configured to use Isolate sandbox");
    }

    public IExecutionService GetExecutionService(string language)
    {
        var normalizedLanguage = language.ToLowerInvariant().Replace(" ", "");

        _logger.LogDebug("Getting Isolate execution service for language: {Language}", normalizedLanguage);

        return normalizedLanguage switch
        {
            "c++" or "cpp" => _serviceProvider.GetRequiredService<CppExecutionServiceIsolate>(),
            "java" => _serviceProvider.GetRequiredService<JavaExecutionServiceIsolate>(),
            "kotlin" => _serviceProvider.GetRequiredService<KotlinExecutionServiceIsolate>(),
            "c#" or "csharp" => _serviceProvider.GetRequiredService<CSharpExecutionServiceIsolate>(),
            "python" => _serviceProvider.GetRequiredService<PythonExecutionServiceIsolate>(),
            _ => throw new NotSupportedException($"Language '{language}' is not supported")
        };
    }
}
