using SpaceGame.Api.Universe.Bootstrap;

namespace SpaceGame.Api.Universe.Scenario;

public sealed class WorldTopologyBuilder(
    IStaticDataProvider staticData,
    SystemGenerationService generationService,
    SpatialBuilder spatialBuilder)
{
    public WorldBuildTopology Build(WorldGenerationOptions worldGenerationOptions)
    {
        var systems = generationService.GenerateSystems(
          generationService.PrepareKnownSystems(staticData.KnownSystems),
          worldGenerationOptions);

        var systemRuntimes = systems
          .Select(definition => new SystemRuntime
          {
              Definition = definition,
              Position = LoaderSupport.ToVector(definition.Position),
          })
          .ToList();

        var systemsById = systemRuntimes.ToDictionary(system => system.Definition.Id, StringComparer.Ordinal);
        var spatialLayout = spatialBuilder.BuildLayout(systemRuntimes);

        return new WorldBuildTopology(systems, systemRuntimes, systemsById, spatialLayout);
    }
}
