#!/usr/bin/env python3
"""Builds cases.json: tag lists for the tool run. Writes one list per case, plus the files used by the commands."""
import json, os

def cost(t): return len(t) + (2 if " " in t else 0)
def total(tags): return sum(cost(t) for t in tags) + max(0, len(tags) - 1)

os.makedirs("in", exist_ok=True)
cases = []
def add(name, text, note=""):
    cases.append({"name": name, "text": text, "note": note})

add("empty", "")
add("control Foo-Baz (YouTube says 7)", "Foo-Baz")
add("control Foo Baz (YouTube says 9)", "Foo Baz")
add("ten short single-word tags", ", ".join(["cats", "kitten", "play", "funny", "cute", "pets", "home", "vlog", "short", "daily"]))
phrase = "how to count youtube tags now"
add(f"one {len(phrase)}-character phrase with spaces", phrase)
# exactly 500 and 501: mix of single-word and two-word tags, built by script
base = []
i = 0
while True:
    t = ("word%02d" % i) if i % 2 == 0 else ("two word%02d" % i)
    if total(base + [t]) > 500: break
    base.append(t); i += 1
gap = 500 - total(base)
# pad the last tag with letters to hit exactly 500
base[-1] = base[-1] + "x" * gap
assert total(base) == 500, total(base)
add("list of exactly 500 by the rule", ",".join(base))
over = list(base); over[-1] = over[-1] + "x"
assert total(over) == 501
add("same list plus one character (501)", ",".join(over))
add("quoted multi-word tags typed by the user", '"foo bar", "baz qux"', "typed quotation marks")
add("Hindi tags with spaces", "भारत यात्रा, दिल्ली, खाना बनाना")
add("Japanese tags, one with a space", "料理, 東京 旅行, 犬")
add("emoji tags", "🔥, 😀 fun, 🍕")
add("space after each comma", "cats, kitten, play, funny")
add("one tag per line (no commas)", "cats\nkitten\nplay\nfunny")
add("two tags on two lines, each with a space", "red apple\nblue sky")
add("non-breaking space inside a tag", "foo bar")
add("two-word tags, ten of them", ", ".join(f"red apple{n}" for n in range(10)))
# a realistic long list that is over 500 (30 two-word tags)
long_tags = [f"cat video {w}" for w in "alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa quebec romeo sierra tango uniform victor whiskey xray yankee zulu one two three four five".split()]
open("in/tags-over.txt", "w", encoding="utf-8").write(",".join(long_tags) + "\n")
add("realistic list: 31 two-word tags (tags-over.txt)", ",".join(long_tags))
json.dump(cases, open("cases.json", "w"), ensure_ascii=False, indent=1)
open("in/tags-ok.txt", "w", encoding="utf-8").write("cats, kitten, play, funny, cute pets, home vlog\n")
print(len(cases), "cases; over list total", total(long_tags))
