def unique_chains(buss_obj, location):
    """Counts the unique number of chains in the given location"""
    # storing for unique ids
    seen_id = set()
    # dict for chain and number of stores of that chain
    chains = {}
    for buss in buss_obj:
        if location == buss.location:
            # eleminating the duplicated stores by checking ID
            if buss._id not in seen_id:
                seen_id.add(buss._id)
                # if we are seeing the store first time
                # the num is 1
                if buss.name not in chains:
                    chains[buss.name] = 1
                # each time we see the chain we increment
                # the number of stores
                else:
                    chains[buss.name] += 1

    # make a list of list from chains dictionary keys and values
    chains = list(map(list, chains.items()))

    # sorting the number of stores by descending,
    # and the names by alphabetical order
    return sorted(chains, key=lambda x: (-x[1], x[0]))