require 'optparse'

Options = Struct.new(:period_id, :input_file, :commit_run, :skip_validation, :environment, :output_type)

class CommandlineParser
  def self.parse(args)
    options = Options.new(nil, nil, false, false, nil, 'tsv')

    option_parser = OptionParser.new do |opts|
      opts.banner = "Manual adjustment validation and import
Usage: ingest_adjustments.rb [options]
      "
      opts.on('-e', '--environment ENVIRONMENT', "REQUIRED. Tells the script which database you want to validate and/or commit
                                     adjustments to. Normal options are 'local', 'qa', or 'production'.

                                     You can add new environments by creating a new .env file with a
                                     specific enviroment postfixed. For example, if you wanted to support
                                     a new environment called 'staging', create .env.staging and include the
                                     required key/values for the staging database.
              "){ |v| options[:environment] = v }

      opts.on('-f', '--input_file FILENAME', 'REQUIRED. The full path to the .xlsx file with the manual adjustments to be
                                     imported.
              '){ |v| options[:input_file] = v }

      opts.on('-p', '--period_id ID', "REQUIRED. The Abacus statement period ID against which this manual adjustment
                                     is to be applied. This will be a 3 digit number, such as '286'
              "){ |v| options[:period_id] = v }

      opts.on('-c', '--commit_run', 'OPTIONAL. Tells the script to commit the adjustments to the database.
                                     This does NOT apply the adjustments, it only inserts the records
              '){ |v| options[:commit_run] = v }

      opts.on('-o', '--output_type STRING', "OPTIONAL. The default value is 'tsv'. Valid values are 'tsv' and 'excel'.

                                     Output from the import process includes the batch and Abacus record
                                     IDs on each row where the input is valid.

                                     This output will also include validation failure messages on rows that are invalid.

                                     The default output type is .tsv. You can also choose to output as .xlsx which is
                                     best if you will be providing the validation errors back to the business users.

                                     Note: writing .xlsx is much slower than writing .tsv which is why .tsv is the default.
                                     For large data sets, over 10,000 rows, it could take over 20 minutes to write the
                                     output file in Excel format. Only use it when you have to provide the output sheet
                                     back to the business.
              "){ |v| options[:output_type] = v }

      opts.on('-v', '--skip_validation', 'OPTIONAL. If you are _sure_ the sheet you are importing is fully valid for the
                                     environment you are importing into, you can skip the validation step and go straight to
                                     the commit. For very large files this can speed things up. But mostly you should validate
                                     before committing.
              '){ |v| options[:skip_validation] = v }

      opts.on('-h', '--help', 'Print this help' ) do
        puts opts
        exit
      end
    end

    option_parser.parse!(args)
    options
  end
end

