# frozen_string_literal: true

require 'optparse'
require 'active_support/inflector'

module Gitlab
  module QA
    # rubocop:disable Metrics/AbcSize
    class Runner
      def self.run(args)
        Runtime::Scenario.define(:teardown, true)
        Runtime::Scenario.define(:run_tests, true)
        Runtime::Scenario.define(:qa_image, Runtime::Env.qa_image) if Runtime::Env.qa_image
        Runtime::Scenario.define(:omnibus_configuration, Runtime::OmnibusConfiguration.new)

        # Omnibus Configurators specified by flags
        @active_configurators = []
        @omnibus_configurations = %w[default] # always load default configuration

        @options = OptionParser.new do |opts|
          opts.banner = 'Usage: gitlab-qa [options] Scenario URL [[--] path] [rspec_options]'

          opts.on('--no-teardown', 'Skip teardown of containers after the scenario completes.') do
            Runtime::Scenario.define(:teardown, false)
          end

          opts.on('--no-tests', 'Orchestrates the docker containers but does not run the tests. Implies --no-teardown') do
            Runtime::Scenario.define(:run_tests, false)
            Runtime::Scenario.define(:teardown, false)
          end

          opts.on('--qa-image QA_IMAGE', String, 'Specifies a QA image to be used instead of inferring it from the GitLab image. See Gitlab::QA::Release#qa_image') do |value|
            Runtime::Scenario.define(:qa_image, value)
          end

          opts.on_tail('-v', '--version', 'Show the version') do
            require 'gitlab/qa/version'
            puts "#{$PROGRAM_NAME} : #{VERSION}"
            exit
          end

          opts.on('--omnibus-config config1[,config2,...]', 'Use Omnibus Configuration package') do |configuration|
            configuration.split(',').map do |config|
              @omnibus_configurations << config
            end
          end

          opts.on_tail('-h', '--help', 'Show the usage') do
            puts opts
            exit
          end

          begin
            opts.parse(args)
          rescue OptionParser::InvalidOption
            # Ignore invalid options and options that are passed through to the tests
          end
        end

        args.reject! { |arg| gitlab_qa_options.include?(arg) }
        if args.size >= 1
          load_omnibus_configurations

          begin
            @active_configurators.compact.each do |configurator|
              configurator.instance(skip_teardown: true)
            end

            Scenario
              .const_get(args.shift)
              .perform(*args)
          ensure
            @active_configurators.compact.each(&:teardown)
          end
        else
          puts @options
          exit 1
        end
      end

      def self.gitlab_qa_options
        @gitlab_qa_options ||= @options.top.list.map(&:long).flatten
      end

      def self.load_omnibus_configurations
        # OmnibusConfiguration::Test       => --test
        # OmnibusConfiguration::HelloThere => --hello_there
        @omnibus_configurations.uniq.each do |config|
          Runtime::OmnibusConfigurations.const_get(config.camelize).new.tap do |configurator|
            @active_configurators << configurator.prepare

            # */etc/gitlab/gitlab.rb*
            # -----------------------
            # # Runtime::OmnibusConfiguration::Packages
            # gitlab_rails['packages_enabled'] = true
            Runtime::Scenario.omnibus_configuration << "# #{configurator.class.name}"

            # Load the configuration
            configurator.configuration.split("\n").each { |c| Runtime::Scenario.omnibus_configuration << c }
          end
        rescue NameError
          raise <<~ERROR
                Invalid Omnibus Configuration `#{config}`.
                Possible configurations: #{Runtime::OmnibusConfigurations.constants.map { |c| c.to_s.underscore }.join(',')}"
          ERROR
        end
      end
    end
    # rubocop:enable Metrics/AbcSize
  end
end