# frozen_string_literal: true

# What this script does:
# ----------------------
# Takes one or more pallet numbers and rebuilds the FG code using inputs from the sequences instead of the product setup.
#
# Reason for this script:
# -----------------------
# Sequences can be created and have the FG code lookup fail, but later changes to the sequence data means that the lookup would succeed because the attributes of the sequence have changed to enable a match.
#
# To run: (nnn represents the number of days back to go to select pallets)
# -------
# Debug : DEBUG=y RACK_ENV=production ruby scripts/base_script.rb UpdateExtendedFGCodePallets nnn,nnn
# Live  : RACK_ENV=production ruby scripts/base_script.rb UpdateExtendedFGCodePallets nnn,nnn
# Dev   : ruby scripts/base_script.rb UpdateExtendedFGCodePallets nnn,nnn
#
class UpdateExtendedFGCodePallets < BaseScript
  def run # rubocop:disable Metrics/AbcSize
    raise ArgumentError, 'Please supply one or more pallets separated by commas' if args.first.nil?

    pallet_ids = args.first.split(',').uniq

    if debug_mode
      puts "Update extended FG codes #{pallet_ids.length} pallets to be checked"
      puts "Enqueue FinishedGoodsApp::Job::CalculateExtendedFgCodesFromSeqs with #{pallet_ids.inspect}"
    else
      FinishedGoodsApp::Job::CalculateExtendedFgCodesFromSeqs.enqueue(pallet_ids)
    end

    infodump = <<~STR
      Script: UpdateExtendedFGCodePallets

      What this script does:
      ----------------------
      Takes one or more pallet numbers and rebuilds the FG code using inputs from the sequences instead of the product setup.

      Reason for this script:
      -----------------------
      Sequences can be created and have the FG code lookup fail, but later changes to the sequence data means that the lookup would succeed because the attributes of the sequence have changed to enable a match.

      Results:
      --------
      Pallet ids for update - job enqued for each set of 50 ids:
      #{pallet_ids.join("\n")}
    STR

    log_infodump(:data_fix,
                 :pallet_seqs,
                 :update_fg_code,
                 infodump)

    if debug_mode
      success_response('Dry run complete')
    else
      success_response('Updates enqueued')
    end
  end
end
