"""
Final snippet builder. Precisely crafted with exact difficult character counts.
Each snippet annotated with its diff char count.
"""
SHIFTED = set('~!@#$%^&*()_+{}|:"<>?ABCDEFGHIJKLMNOPQRSTUVWXYZ')
REACH = set('0123456789[]=-`')
RARE = set('\\|`~^')
DIFFICULT = SHIFTED | REACH | RARE

def c(s):
    """Count and show diff info"""
    dc = sum(1 for ch in s if ch in DIFFICULT)
    L = len(s)
    pct = round(dc/L*100, 1)
    dchars = ''.join(ch for ch in s if ch in DIFFICULT)
    return L, dc, pct, dchars

def report(tier, snippets, target_len, target_pct):
    lo, hi = target_len - 5, target_len + 5
    ok_count = 0
    print(f"\n{'='*60}")
    print(f"  {tier} (target: {lo}-{hi} chars, {target_pct}% ±1.5%)")
    print(f"{'='*60}")
    for i, s in enumerate(snippets):
        L, dc, pct, dchars = c(s)
        len_ok = lo <= L <= hi
        pct_ok = abs(pct - target_pct) <= 1.5
        ok = len_ok and pct_ok
        if ok: ok_count += 1
        status = "OK" if ok else "FAIL"
        flags = []
        if not len_ok: flags.append(f"LEN({L})")
        if not pct_ok: flags.append(f"PCT({pct}%)")
        print(f"  {i+1:2d}. [{status:>4s}] len={L:3d} diff={dc:2d} ({pct:5.1f}%) {' '.join(flags)}")
        if not ok:
            ideal_dc = round(target_pct * L / 100)
            print(f"       diff_chars: {dchars}  (need ~{ideal_dc}, have {dc}, delta={ideal_dc-dc})")
    return ok_count

# ════════════════════════════════════════════════════════════════
# NOVICE: 115-125 chars, 10% diff
# Need ~12 diff chars in ~120 chars
# Pattern: 4 pipes + 3 hyphens + 3 digits + 2 other = 12
# ════════════════════════════════════════════════════════════════

novice = [
    # 1: len=120, pipes=4, -=3(rn,30), digits=3(22,30->2,2,3,0=4), let's see
    # |grep -rn 'port 22' |sort |tail -30 |tee  -> |,-,2,2,|,|,-,3,0,| = 4 pipes + 2 hyphens + 4 digits = 10
    # Need 2 more. Add: 'echo' -> nothing. Use "2>/dev/null" somewhere? That's 2,> = 2 more diff = 12. But changes the meaning.
    # Or just capitalize: echo 'OK, filtered' -> O,K = 2 more = 12
    # Let me try:
    "cat /var/log/auth.log | grep -rn 'port 22' | sort | tail -30 | tee /tmp/ssh.scan.log; echo 'OK filtered login entries'",
    # diff: | - 2 2 | | - 3 0 | O K = 12. len should be ~117

    # 2: need 12 diff
    "find /etc -name '*.conf' | sort | head -15 | tee /tmp/conf.list.log; echo 'OK, found 15 config files on this host now'",
    # diff: - * | | - 1 5 | O K 1 5 = 12. len ~120

    # 3: need 12 diff in ~120
    "ps aux | grep -v 'root' | sort -rn | head -10 | tee /tmp/user.procs.scan.log; echo 'OK, process scan is complete'",
    # diff: | - | - | - 1 0 | O K = 4pipes + 3hyphens + 2digits + 2upper = 11... need 1 more
    # Add another digit: "scan 10" or rename path
    # "ps aux" - no diff there actually. p,s,a,u,x are lowercase = easy
    # Let me recount: | grep -v | sort -rn | head -10 | tee
    # |=d, -=d, |=d, -=d, |=d, -=d, 1=d, 0=d, |=d, = 9 diff + O,K = 11
    # * in '*.conf'... wait this is find, not this one
    # Actually for ps aux: p=easy s=easy a=easy u=easy x=easy, no diff
    # | = d, - = d, | = d, - = d, - = d(the 'n' in -rn is easy), | = d, - = d, 1 = d, 0 = d, | = d
    # That's 4 pipes + 4 hyphens + 2 digits = 10. Plus O,K = 12.
    # Wait: -rn has ONE hyphen. -v has ONE hyphen. sort -rn: that's just -rn = 1 hyphen
    # Let me list: |, -, |, -, -, |, -, 1, 0, |, O, K
    # pipes: 4 (|grep, |sort, |head, |tee), hyphens: 3 (-v, -rn -> only -, -10 -> only -)
    # Wait, -rn = "-" then "r" then "n". The "-" is 1 diff char. "r" and "n" are easy.
    # -10: "-" is 1 diff, "1" is 1 diff, "0" is 1 diff. So -10 = 3 diff chars.
    # So: |, -, |, -(of-rn), -(wait, -rn is one hyphen)
    # grep -v: the "-" = 1 diff
    # sort -rn: the "-" = 1 diff
    # head -10: the "-" = 1 diff, "1" = 1 diff, "0" = 1 diff
    # pipes: between commands = 4
    # Total: 4 + 3 + 2 + 2(OK) = 11. Need 1 more.
    # Easy fix: change to "head -10; echo 'OK, scan complete'" or add a digit somewhere
    # Better: echo 'OK, 10 process scans complete'
    # That adds 1,0 = 2 more diff = 13. Too many.
    # Just use echo 'OK process scan is now complete' (same count)
    # Or add one more digit somewhere: /tmp/user.procs.scan1.log = +1 diff = 12
    # Actually wait, * in the find command IS difficult. But ps doesn't have *.
    # Let me just use a simpler approach:

    "ps aux | grep -v 'root' | sort -rn | head -10 | tee /tmp/user.proc.scan.list.log; echo 'OK process scan complete'",
    # |,-,|,-,-,|,-,1,0,|,O,K = 4+3+2+2 = 11. Hmm still 11.
    # Wait let me recount. "ps aux |" - the | is 1. "grep -v" - the - is 1. "'root' |" - the | is 1.
    # "sort -rn |" - the - is 1, | is 1. "head -10 |" - the - is 1, 1 is 1, 0 is 1, | is 1.
    # "tee /tmp/user.proc.scan.list.log;" - no diff chars (all lowercase, dots, slashes, semicolons)
    # "echo 'OK process scan complete'" - O is 1, K is 1.
    # Total: 1+1+1+1+1+1+1+1+1+1+1 = 11. Need 12.
    # Fix: change to "echo 'OK, process 10 scan complete'" -> adds 1,0 = 2 more = 13. Too many.
    # Change to use tee /tmp/user.proc.scan3.log -> adds 3 = 1 more = 12.

    # 4:
    "ls -la /var/log/nginx | grep '.log' | sort -k5 | head -10 | tee /tmp/biggest.sort.log; echo 'top 10 log files pulled ok'",
    # -, |, |, -, 5, |, -, 1, 0, |, 1, 0 = 4 pipes + 3 hyphens + 4 digits + 0 upper = 11. Need 1 more.
    # Hmm, -la: the '-' is 1 diff, 'l' and 'a' are easy.
    # So: -(la), |, |, -(k5), 5, |, -(10), 1, 0, |, 1, 0
    # That's 4 pipes + 3 hyphens + 5 digits = 12.

    # 5:
    "who | sort | head -10; last -n 20 | grep -v 'reboot' | tee /tmp/session.user.history.log; echo 'all history is saved now'",
    # |, |, -, 1, 0, -, 2, 0, |, -, |
    # pipes: 4, hyphens: 3, digits: 4(1,0,2,0) = 11. Need 1 more.
    # Use "echo 'history saved'" -> no help. Add a digit: /tmp/session.user2.history.log -> +1 = 12

    # OK I'm going to take a completely different approach. Let me just write all 60 snippets
    # in a careful way, run the checker, fix one at a time. I'll do this iteratively.
    # Let me first figure out a system.

    # SYSTEM: for each snippet, I'll note the diff budget:
    # - Each | pipe counts as 1 diff
    # - Each - hyphen counts as 1 diff
    # - Each digit 0-9 counts as 1 diff
    # - Each uppercase A-Z counts as 1 diff
    # - Each of these counts as 1 diff: * > < $ { } ( ) [ ] = _ + ! @ # % ^ & " : ? ~ ` \
    # - These are easy (0 diff): a-z space . / , ; '

    # So I need to add the exact number of diff chars and pad with easy chars.
    # Let me just build them correctly now.
    "placeholder",
    "placeholder",
    "placeholder",
    "placeholder",
    "placeholder",
    "placeholder",
    "placeholder",
]

# OK this line-by-line approach is too slow. Let me write all snippets in one shot,
# then run the checker, then fix them iteratively.
# I'll output only the checker results.

print("Skipping manual approach. Will write directly to verify_snippets.py and iterate.")
