require 'thor'
require 'qa_helpers'

module QAHelpers
  class CLI < Thor
    desc 'grasshelper', 'To get the grass token for specific vendor_id (-v) or vend_contact_id (-c),
    e.g. qa_helpers grasshelper -v 10303'
    method_option :vendor_id, :aliases => '-v', :desc => 'The Vendor ID'
    method_option :vend_contact_id, :aliases => '-c', :desc => 'The Vendor Contact ID'
    def grasshelper
      token_helper = QAHelpers::GrassTokenHelper.new
      if options['vendor_id']
        puts token_helper.user_grass(vendor_id: options['vendor_id'])
      elsif options['vend_contact_id']
        puts token_helper.user_grass(vend_contact_id: options['vend_contact_id'])
      else
        puts 'Provide -v <VENDOR_ID> or -c <VEND_CONTACT_ID> please, e.g. qa_helpers grasshelper -v 10303'
      end
    end

    desc 'get_test_matrix', 'Generates User Feature matrix based on cucumber_tests project'
    method_option 'cucumber-path', aliases: '-p', desc: 'The path to cucumber_tests project'
    method_option 'format', aliases: '-f',
                            desc: 'The output format "stdout" or "xls"'

    def get_test_matrix
      if options['cucumber-path'] && !options['format'] || options['format'] == 'xls'
        features_parser = QAHelpers::FeaturesParser.new(options['cucumber-path'])
        puts features_parser.users_to_features_xls

      elsif options['cucumber-path'] && options['format'] == 'stdout'
        features_parser = QAHelpers::FeaturesParser.new(options['cucumber-path'])
        features_parser.users_to_features_stdout
      else
        puts 'Provide --cucumber-path (-p) /path/to/cucumber_tests to get get_test_matrix XLS please'
        puts 'Example $ qa_helpers get_test_matrix -p /Users/<USER>/theotchard/cucumber_tests'
      end
    end

    desc 'e2e_status', 'Collects E2E Cucumber test results form Jenkins legacy builds'
    method_option 'job-count', aliases: '-c', desc: 'The amount of latest jobs to parse.'
    method_option 'workers', aliases: '-w', desc: 'The amount of workers for paralleling. 5 is by default.'

    def e2e_status
      if options['job-count'] && options['job-count'].to_i > 0
        parser = QAHelpers::JenkinsBuildParser.new(builds_to_consider: options['job-count'].to_i,
                                                   workers: options['workers'])
        parser.parse
      else
        puts 'Provide --job-count n to get the e2e failures'
        puts 'Example $ qa_helpers e2e_status --job-count 10 # to get results for latest 10 jobs'
      end
    end
  end
end
