module AutomationService
    class BatchEmployerUpdate

        def initialize(pl_plan_keys = nil, full_sync = false)
            @pl_plan_keys = pl_plan_keys
            @full_sync = full_sync
        end

        def call
            sql_query = "SELECT PLPlanKey, PlanId, ShortDesc FROM PLPlanHeader 
                        WHERE ActiveInactive = 'Active'"
            if @pl_plan_keys.present?
                sql_query = sql_query.concat(" AND PLPlanKey IN (#{@pl_plan_keys.join(',')})")
            end
            employer_plan_headers = VhcsRecord.connection.select_all(sql_query)

            employer_update_futures = employer_plan_headers.map do |employer_plan_header|
                Concurrent::Future.execute do
                    ActiveRecord::Base.connection_pool.with_connection do
                        UpdateEmployerJob.perform_later(employer_plan_header: employer_plan_header, full_sync: @full_sync)
                    end
                end
            end

            employer_update_futures.map(&:value)
        end
        
    end
end

# AutomationService::MemberUpdate('13', 337710)