# ███╗   ██╗ ██████╗ ██████╗ ███████╗███╗   ███╗ ██████╗ ███╗   ██╗██╗  ██╗███████╗███████╗
# ████╗  ██║██╔═══██╗██╔══██╗██╔════╝████╗ ████║██╔═══██╗████╗  ██║██║ ██╔╝██╔════╝██╔════╝
# ██╔██╗ ██║██║   ██║██║  ██║█████╗  ██╔████╔██║██║   ██║██╔██╗ ██║█████╔╝ █████╗  ███████╗
# ██║╚██╗██║██║   ██║██║  ██║██╔══╝  ██║╚██╔╝██║██║   ██║██║╚██╗██║██╔═██╗ ██╔══╝  ╚════██║
# ██║ ╚████║╚██████╔╝██████╔╝███████╗██║ ╚═╝ ██║╚██████╔╝██║ ╚████║██║  ██╗███████╗███████║
# ╚═╝  ╚═══╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝     ╚═╝ ╚═════╝ ╚═╝  ╚═══╝╚═╝  ╚═╝╚══════╝╚══════╝

#
# Allocate nodemonke inscriptions randomly to a set of addresses with allocations.
#
# To reproduce, take the nodemonk inscriptions IDs from inscription
# 50f19bbb5cf385b04aea8c1e4039786800635921f96849d774354ff4c72fd218i0 and the allocations
# from inscription f028dabb5a46aebc670f4080189cb8eaf20456867ff33253bff503476b2e42b3i0

# For provable randomness, we will use the hashes of three future blocks.
# Specifically, if this script is inscribed in block N, we will use the hashes
# of blocks N+3, N+4, and N+5 as the seeds for the random number generator.
#

import argparse
import random

def main():
    parser = argparse.ArgumentParser(description="Randomly allocate inscriptions to addresses")
    parser.add_argument("inscriptions_file", help="File containing inscription IDs, one per line")
    parser.add_argument("allocations_file", help="File containing allocations as pairs of address,count, one per line")
    parser.add_argument("block_hash1", help="First bitcoin block hash to be used as random seed")
    parser.add_argument("block_hash2", help="Second bitcoin block hash to be used as random seed")
    parser.add_argument("block_hash3", help="Third bitcoin block hash to be used as random seed")
    args = parser.parse_args()

    # Read inscriptions
    with open(args.inscriptions_file, 'r') as file:
        inscriptions = [line.strip() for line in file]

    # Remove the golden monke king from available inscriptions
    inscriptions.remove("c7f83d24c9f868aecfa16d1efd12619430cf0fab7fdcc9bea3e5ae850b611303i0")

    # Read allocations
    allocations = []
    with open(args.allocations_file, 'r') as file:
        for alloc in file:
            address, count = alloc.split(',')
            allocations.extend([address.strip()] * int(count))

    print(f"Total # inscriptions: {len(inscriptions)}")
    print(f"Total # allocations: {len(allocations)}")

    # Shuffle inscriptions using first block hash as seed
    # This is using the mersenne twister algorithm,
    # and only produces a subset of the possible permutations,
    # so should not be the only source of randomness
    random.seed(args.block_hash1)
    random.shuffle(inscriptions)

    # Shuffle allocations using second block hash as seed
    # This is using the mersenne twister algorithm,
    # and only produces a subset of the possible permutations,
    # so should not be the only source of randomness
    random.seed(args.block_hash2)
    random.shuffle(allocations)

    # We use the third block hash as the seed for randomly chosing
    # inscriptions from the shuffled list    
    random.seed(args.block_hash3)
    allocated_inscriptions = []
    for i in range(len(allocations)):
        random_insc = random.choice(inscriptions)
        inscriptions.remove(random_insc)
        allocated_inscriptions.append((random_insc, allocations[i]))

    # If there are any inscriptions left, allocate them to 'NA'
    for insc in inscriptions:
        allocated_inscriptions.append((insc, 'NA')) 

    # Write output to a text file
    output_file = f"allocations-{args.block_hash1}-{args.block_hash2}-{args.block_hash3}.txt"
    with open(output_file, 'w') as file:
        for insc, address in allocated_inscriptions:
            file.write(f"{insc}\t{address}\n")

if __name__ == "__main__":
    main()

# ███████╗███████╗███╗   ██╗██████╗     ███╗   ██╗ ██████╗ ██████╗ ███████╗███████╗
# ██╔════╝██╔════╝████╗  ██║██╔══██╗    ████╗  ██║██╔═══██╗██╔══██╗██╔════╝██╔════╝
# ███████╗█████╗  ██╔██╗ ██║██║  ██║    ██╔██╗ ██║██║   ██║██║  ██║█████╗  ███████╗
# ╚════██║██╔══╝  ██║╚██╗██║██║  ██║    ██║╚██╗██║██║   ██║██║  ██║██╔══╝  ╚════██║
# ███████║███████╗██║ ╚████║██████╔╝    ██║ ╚████║╚██████╔╝██████╔╝███████╗███████║
# ╚══════╝╚══════╝╚═╝  ╚═══╝╚═════╝     ╚═╝  ╚═══╝ ╚═════╝ ╚═════╝ ╚══════╝╚══════╝