namespace LiquidCode.Tester.Worker.Services;

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

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

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

    public ICompilationService GetCompilationService(string language)
    {
        var normalizedLanguage = language.ToLowerInvariant().Replace(" ", "");

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

        return normalizedLanguage switch
        {
            "c++" or "cpp" => _serviceProvider.GetRequiredService<CppCompilationServiceIsolate>(),
            "java" => _serviceProvider.GetRequiredService<JavaCompilationServiceIsolate>(),
            "kotlin" => _serviceProvider.GetRequiredService<KotlinCompilationServiceIsolate>(),
            "c#" or "csharp" => _serviceProvider.GetRequiredService<CSharpCompilationServiceIsolate>(),
            "python" => _serviceProvider.GetRequiredService<PythonCompilationServiceIsolate>(),
            _ => throw new NotSupportedException($"Language '{language}' is not supported")
        };
    }
}
