private void CreateInitialAutoBookCollection()
        {
            var autobookConfigurations = _autoBookInstanceConfigurationRepository.GetAll();

            _autoBooks.AddRange(_autoBookRepository.GetAll().Select(ab =>
            {
                var autobookConfiguration = autobookConfigurations.FirstOrDefault();
                return(new AutoBookStubItem
                {
                    Id = ab.Id,
                    Name = $"{nameof(AWSPAAutoBook.Name)}-{ab.Id}",
                    Url = ab.Api,
                    Provisioned = true,
                    InstanceType = autobookConfiguration?.InstanceType,
                    Version = _autoBookSettings.ApplicationVersion,
                    StorageSizeGb = autobookConfiguration?.StorageSizeGb.ToString(CultureInfo.InvariantCulture),
                    Status = AutoBookStatuses.Idle
                });
            }));
        }
        public void CreateAndDelete(Run run, AutoBookInstanceConfiguration autoBookInstanceConfiguration)
        {
            IEnumerable <AutoBook> allWorkingAutoBooks = _autoBooks.WorkingAutoBooks;
            var  allAutoBookInstanceConfigurations     = _autoBookInstanceConfigurationRepository.GetAll();
            bool singleInstance = allAutoBookInstanceConfigurations.Count == 1;

            RaiseInfo($"Enter ManageAutoBooksCreateAndDelete for runid: {run.Id}, AutoBooks.Instances: {_autoBooks.Instances.ToString()}");
            RaiseInfo($"Scenario count: {run.Scenarios.Count.ToString()}, max autobooks allowed: {_autoBooks.Settings.MaxInstances.ToString()}");
            RaiseInfo(singleInstance == true ?
                      $"Single instance type available: {autoBookInstanceConfiguration.Id.ToString()}" :
                      $"Varying instance types available, autobook type required: {autoBookInstanceConfiguration.Id.ToString()}");

            if (!singleInstance && allWorkingAutoBooks.Any())
            {
                var otherRunsAreActive      = new Lazy <bool>(() => OtherActiveRunsWithWorkingScenarios(_runRepository, run.Id));
                var autobooksOfCorrectType  = allWorkingAutoBooks.Where(w => w.InstanceConfigurationId >= autoBookInstanceConfiguration.Id).ToList();
                var autobooksOfTooSmallType = allWorkingAutoBooks.Where(w => w.InstanceConfigurationId < autoBookInstanceConfiguration.Id).ToList();
                RaiseInfo($"Varying Instance Types & Some working autobooks - Scenarios count: {run.Scenarios.Count} AutoBooks correct type: {autobooksOfCorrectType.Count}, AutoBooks too small type: {autobooksOfTooSmallType.Count}");

                if (run.Scenarios.Count > autobooksOfCorrectType.Count)
                {
                    foreach (RunScenario currentScenario in run.Scenarios)
                    {
                        if (run.Scenarios.Count > autobooksOfCorrectType.Count)
                        {
                            RaiseInfo($"Need autobooks: {run.Scenarios.Count}, Existing autobooks of required type: {autobooksOfCorrectType.Count}");

                            if (((autobooksOfCorrectType.Count + autobooksOfTooSmallType.Count) < _autoBooks.Settings.MaxInstances) || _autoBooks.Settings.MaxInstances == 0)
                            {
                                //create autobook
                                var newAutobook = CreateAutoBookOfType(autoBookInstanceConfiguration.Id);
                                autobooksOfCorrectType.Add(newAutobook);
                                RaiseInfo($"Within the max allowed so created one of suitable size: {newAutobook.Id.ToString()} autobooktype.Id: {autoBookInstanceConfiguration.Id.ToString()}");
                            }
                            else if (autobooksOfTooSmallType.Any())
                            {
                                if (!otherRunsAreActive.Value)
                                {
                                    RaiseInfo($"Max reached - checking for autobooks to delete, number of autobooks of too small type: {autobooksOfTooSmallType.Count.ToString()}, ScenarioID = {currentScenario.Id.ToString()}");
                                    var autoBook = GetAutoBookAbleToDelete(autobooksOfTooSmallType);
                                    if (autoBook != null)
                                    {
                                        //delete autobook
                                        var delid = autoBook.Id.ToString();
                                        DeleteAutoBook(autoBook);
                                        _ = autobooksOfTooSmallType.Remove(autoBook);

                                        //create autobook
                                        var newAutobook = CreateAutoBookOfType(autoBookInstanceConfiguration.Id);
                                        autobooksOfCorrectType.Add(newAutobook);
                                        RaiseInfo($"Max reached - deleted smaller autobook: {delid}, created larger autobook: {newAutobook.Id.ToString()}, autobooktype.Id: {autoBookInstanceConfiguration.Id.ToString()}, ScenarioID = {currentScenario.Id.ToString()}");
                                    }
                                    else
                                    {
                                        RaiseInfo("Max reached but couldn't delete an autobook (none with suitable status of Idle or Fatal Error)");
                                    }
                                }
                                else
                                {
                                    RaiseInfo("Other Active Runs, can't delete an autobook");
                                }
                            }
                        }
                    }
                }
            }

            else
            {
                RaiseInfo($"Single Instance Type or No working autobooks - scenario count: {run.Scenarios.Count.ToString()}, max allowed: {_autoBooks.Settings.MaxInstances.ToString()}, so creating autobooks of type: {autoBookInstanceConfiguration.Id.ToString()}");
                CreateAnAutoBookForEachScenario(run, autoBookInstanceConfiguration.Id, allWorkingAutoBooks.Count());
            }

            RaiseInfo($"Exit ManageAutoBooksCreateAndDelete for runid: {run.Id.ToString()}, AutoBooks.Instances: {_autoBooks.Instances.ToString()}");
        }