# frozen_string_literal: true

module Gitlab
  module QA
    module Scenario
      module Test
        module Integration
          class SSHTunnel < Scenario::Template
            # rubocop:disable Metrics/AbcSize
            def perform(release, *rspec_args)
              Runtime::Env.override_default_password!
              Runtime::Env.require_ssh_tunnel!

              Component::Gitlab.perform do |gitlab|
                gitlab.release = release
                gitlab.network = 'test'

                Component::InternetTunnel.perform do |tunnel_gitlab|
                  Component::InternetTunnel.perform do |tunnel_registry|
                    tunnel_gitlab.gitlab_hostname = gitlab.hostname
                    tunnel_gitlab.network = 'test'
                    tunnel_registry.gitlab_hostname = gitlab.hostname
                    tunnel_registry.network = 'test'

                    gitlab.omnibus_config = <<~OMNIBUS
                      external_url '#{tunnel_gitlab.url}';
                      nginx['listen_port'] = 80;
                      nginx['listen_https'] = false;
                      # documentation: https://docs.gitlab.com/omnibus/settings/database.html#seed-the-database-fresh-installs-only
                      gitlab_rails['initial_root_password'] = '#{Runtime::Env.require_initial_password!}'

                      registry_external_url '#{tunnel_registry.url}';
                      registry_nginx['listen_port'] = 80;
                      registry_nginx['listen_https'] = false;
                    OMNIBUS

                    tunnel_gitlab.instance do
                      tunnel_registry.instance do
                        gitlab.instance do
                          Component::Specs.perform do |specs|
                            specs.suite = 'Test::Integration::SSHTunnel'
                            specs.release = gitlab.release
                            specs.network = gitlab.network
                            specs.args = [tunnel_gitlab.url, *rspec_args]
                          end
                        end
                      end
                    end
                  end
                end
              end
            end
            # rubocop:enable Metrics/AbcSize
          end
        end
      end
    end
  end
end