"""What privacy settings can the new SoundCloud uploader actually set?

PLN asked for a PRIVATE album upload. The form's visible text offers only
"Public" and "Schedule public release (ARTIST PRO)", but the DOM carries five
radios whose labels did not extract — so before assuming private is unavailable
(and before uploading 15 files publicly by accident), read the radios properly.

Uploads nothing: stops before the Upload button.
"""
import sys
from pathlib import Path

sys.path.insert(0, "/home/pln/Work/Sound/tidal-ears/src")
from tidal_ears import consent  # noqa: E402
from tidal_ears.scbrowser import Browser  # noqa: E402

PROBE = Path(sys.argv[1])

RADIO_JS = """() => {
  const out = [];
  document.querySelectorAll('input[type=radio]').forEach((el, i) => {
    // Walk up until an ancestor has text of its own, which is where these
    // designs park the label.
    let node = el, text = '', depth = 0;
    while (node && depth < 6) {
      const t = (node.innerText || '').replace(/\\s+/g, ' ').trim();
      if (t.length > text.length) text = t;
      if (text.length > 20) break;
      node = node.parentElement; depth++;
    }
    const r = el.getBoundingClientRect();
    out.push({i, name: el.name || '(none)', value: el.value || '',
              checked: el.checked, disabled: el.disabled,
              visible: r.width > 0 && r.height > 0,
              text: text.slice(0, 110)});
  });
  return out;
}"""

with Browser() as b:
    b.open()
    consent.ensure(b.page, policy="reject")
    b.require_login()
    b.page.goto("https://soundcloud.com/upload", wait_until="domcontentloaded")
    frame = b.upload_frame()
    if frame is None:
        sys.exit("no upload frame")
    frame.locator('input[type="file"]').first.set_input_files(str(PROBE))
    b.page.wait_for_timeout(15000)

    print("--- radios ---")
    for r in frame.evaluate(RADIO_JS):
        print(f"  [{r['i']}] name={r['name']:<8} value={r['value']!r:<12} "
              f"checked={str(r['checked']):<5} vis={str(r['visible']):<5} "
              f"{r['text']!r}")

    # Also: does the word "private" appear anywhere in this frame at all?
    hits = frame.evaluate("""() => {
      const t = document.body.innerText || '';
      return t.split('\\n').filter(l => /privat|only you|secret|unlisted/i.test(l))
              .map(l => l.trim().slice(0, 90)).slice(0, 8);
    }""")
    print("\n--- lines mentioning private/unlisted ---")
    print("  " + ("\n  ".join(hits) if hits else "(none)"))
