module IdCardQueueService
    class GetQueuedMembers

        def initialize(pl_plan_keys = nil)
            if pl_plan_keys
                @employer_pl_plan_keys = pl_plan_keys
            else
                @employer_pl_plan_keys = IdCard::Setup.active.pluck(:pl_plan_key).join(',')
            end
        end

        def call
            raw_employers_member_keys = CallStoredProc.new('HLGetQueuedIdCardMemberKeysTPA', { PLPlanKeys: @employer_pl_plan_keys }).call.to_ary

            format_employers_member_keys(raw_employers_member_keys)
        end

        private

        def format_employers_member_keys(raw_employers_member_keys)
            key_map = { "PlanKey" => :pl_plan_key, "MemberKeys" => :member_keys }

            raw_employers_member_keys.map do |hash|
                hash["PlanKey"] = hash["PlanKey"].to_s
                hash["MemberKeys"] = hash["MemberKeys"].split(", ").map(&:to_i)
                hash.transform_keys(key_map)
            end
        end
        
    end
end