"""Turn a list of (label, eleven-words) tasks into a durable chunk queue,
planting one detectable seed drawn from the tasks themselves."""
import os, json, shutil, sys
import sweeplib as S

def build(root, tasks, chunk=400, plant_task=0, layer=42, path_i=0):
    if os.path.isdir(root):
        shutil.rmtree(root)
    for d in ("todo", "claim", "done"):
        os.makedirs(root + "/" + d)
    tasks = list(tasks)
    lab, w11 = tasks[plant_task]
    mn, addr = S.plant_address(w11, layer, path_i)
    json.dump({"label": lab, "mn": mn, "addr": addr, "layer": layer,
               "path": S.PATH_STRS[path_i], "task_index": plant_task},
              open(root + "/plant.json", "w"), indent=1)
    for i in range(0, len(tasks), chunk):
        json.dump(tasks[i:i + chunk], open("%s/todo/c_%08d.json" % (root, i), "w"))
    n = len(os.listdir(root + "/todo"))
    print("%s: %d tasks, %d chunks, %d seeds; plant=%s L%d %s -> %s"
          % (root, len(tasks), n, len(tasks) * 128, lab, layer,
             S.PATH_STRS[path_i], addr), flush=True)
    return len(tasks)
