using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;
using QuantEngine.Application.Services;

namespace QuantEngine.Core.Tests
{
    public class PipelineOrchestratorTests
    {
        [Fact]
        public async Task RunPipelineAsync_ExecutesAll7Steps_AndOutputsJson()
        {
            var orchestrator = new PipelineOrchestrator();
            var result = await orchestrator.RunPipelineAsync();

            Assert.NotNull(result);
            Assert.Equal("PASS", result.Gate);
            Assert.Equal(7, result.Steps.Count);
            Assert.Contains(result.Steps, step => step.StepName == "scores_calculation");
            Assert.Contains(result.Steps, step => step.StepName == "routing_decision");
            Assert.Contains(result.Steps, step => step.StepName == "golden_check");

            foreach (var step in result.Steps)
            {
                Assert.True(step.Success);
                Assert.False(string.IsNullOrEmpty(step.StepName));
                Assert.True(step.ElapsedMilliseconds > 0);
            }

            var tempRoot = Environment.GetEnvironmentVariable("QE_TEMP_ROOT")
                ?? Path.Combine(Directory.GetCurrentDirectory(), "Temp");
            var expectedJsonPath = Path.Combine(tempRoot, "dotnet_pipeline_e2e_v1.json");
            Assert.True(File.Exists(expectedJsonPath));

            var jsonContent = File.ReadAllText(expectedJsonPath);
            Assert.Contains("\"gate\": \"PASS\"", jsonContent);
        }
    }
}
