using FluentAssertions;
using Microsoft.Extensions.Options;
using VigilCare.Simulation;

public class SessionCatalogTests
{
    [Fact]
    public void SessionCatalog_InvalidScenarioId_FailsAtStartup()
    {
        var dir = Path.Combine(Path.GetTempPath(), "vigilcare-session-catalog-" + Guid.NewGuid());
        Directory.CreateDirectory(dir);
        try
        {
            File.WriteAllText(Path.Combine(dir, "sessions.json"), """
                {
                  "sessions": [
                    {
                      "id": "broken-session",
                      "name": "Broken",
                      "goal": "Should fail",
                      "estimatedMinutes": 1,
                      "defaultSpeed": 60,
                      "scenarios": ["does-not-exist-01"]
                    }
                  ]
                }
                """);

            var scenarios = new StubScenarioCatalog();
            var options = Options.Create(new SimulationOptions
            {
                Enabled = true,
                ScenarioDirectory = dir,
            });

            var act = () => new SessionCatalog(options, scenarios);

            act.Should().Throw<InvalidOperationException>()
                .WithMessage("*does-not-exist-01*");
        }
        finally
        {
            Directory.Delete(dir, recursive: true);
        }
    }

    [Fact]
    public void SessionCatalog_ValidManifest_LoadsPresets()
    {
        var fixtureDir = ApiFixture.SimulationScenarioDirectory;
        var catalog = new ScenarioCatalog(Options.Create(new SimulationOptions
        {
            ScenarioDirectory = fixtureDir,
        }));
        var sessions = new SessionCatalog(
            Options.Create(new SimulationOptions { ScenarioDirectory = fixtureDir }),
            catalog);

        sessions.ListSessions().Should().NotBeEmpty();
        sessions.GetById("session-test-two")!.Scenarios.Should().HaveCount(2);
    }

    private sealed class StubScenarioCatalog : IScenarioCatalog
    {
        public IReadOnlyList<ScenarioFile> ListScenarios() => Array.Empty<ScenarioFile>();
        public ScenarioFile? GetById(string scenarioId) => null;
    }
}
