"""
G-3/5/7 // CORPUS ACCESS PROBE  (Mission 25-001, Phase 1 residual)

Purpose: the entire staff has filed ACCESS: NO on the basis of *tool inventory*
(no forum agent holds a file-read primitive). I am the code-executing agent, so
code run in the workspace DOES have a filesystem. This script converts the
staff's inferred negative into an observed, auditable negative -- or a YES with
the artifact VCOS demands (filename list + verbatim excerpt).

Read-only. No network. No secrets. No writes. Output is size-bounded.
"""

import json
import os
import sys

MAX_WALK_FILES = 4000          # bound the walk so this can never hang the runner
MAX_EXCERPT = 900              # chars of verbatim excerpt per file


def show(title):
    print("\n" + "=" * 72)
    print(title)
    print("=" * 72)


# ---------------------------------------------------------------- 1. vantage
show("1. EXECUTION VANTAGE")
print("python     :", sys.version.split()[0])
print("cwd        :", os.getcwd())
print("FORUM_JSON :", os.environ.get("FORUM_JSON", "<unset>"))

# ---------------------------------------------------------------- 2. listing
show("2. TOP-LEVEL LISTING OF WORKSPACE (depth 2, bounded)")
roots_seen = 0
for dirpath, dirnames, filenames in os.walk(".", followlinks=False):
    depth = dirpath.rstrip("/").count(os.sep)
    dirnames[:] = sorted(d for d in dirnames if not d.startswith("."))
    if depth > 2:
        dirnames[:] = []
        continue
    shown = sorted(filenames)[:40]
    print(f"\n[{dirpath}]  dirs={len(dirnames)} files={len(filenames)}")
    for f in shown:
        try:
            sz = os.path.getsize(os.path.join(dirpath, f))
        except OSError:
            sz = -1
        print(f"    {f}  ({sz} bytes)")
    if len(filenames) > len(shown):
        print(f"    ... {len(filenames) - len(shown)} more")
    roots_seen += 1

# ------------------------------------- 3. targeted search for the corpus
show("3. TARGETED SEARCH FOR 'soccer_analytics' (and article-bearing files)")
CORPUS_KEYS = ("soccer_analytics", "soccer-analytics", "soccer")
HIT_DIRS, HIT_FILES = [], []
count_visited = 0
for dirpath, dirnames, filenames in os.walk(".", followlinks=False):
    dirnames[:] = [d for d in sorted(dirnames) if not d.startswith(".")]
    count_visited += 1
    if count_visited > 60:
        print("(walk truncated at 60 directories)")
        dirnames[:] = []
    for name in list(dirnames) + list(filenames):
        low = name.lower()
        if any(k in low for k in CORPUS_KEYS):
            path = os.path.join(dirpath, name)
            (HIT_DIRS if name.lower() in CORPUS_KEYS or os.path.isdir(path) else HIT_FILES).append(path)
    if len(HIT_DIRS) + len(HIT_FILES) > 200:
        break

print("directory/file-name hits:", len(HIT_DIRS) + len(HIT_FILES))
for p in sorted(set(HIT_DIRS + HIT_FILES))[:60]:
    kind = "DIR " if os.path.isdir(p) else "FILE"
    print(f"  {kind} {p}")

# Also probe the obvious absolute/relative spellings directly.
show("3b. DIRECT PATH PROBES")
for cand in ["soccer_analytics", "./soccer_analytics", "../soccer_analytics",
             "/data/soccer_analytics", "/workspace/soccer_analytics",
             os.path.join(os.path.expanduser("~"), "soccer_analytics")]:
    print(f"  {os.path.exists(cand)!s:>5} exists  {cand}")

# ------------------------------------- 4. article-bearing content by extension
show("4. CONTENT-BEARING FILES FOUND ANYWHERE (json/csv/pdf/md/txt/xlsx)")
ext_hits = []
for dirpath, dirnames, filenames in os.walk(".", followlinks=False):
    dirnames[:] = [d for d in sorted(dirnames) if not d.startswith(".") and d not in ("__pycache__",)]
    for f in filenames:
        if f.lower().endswith((".json", ".csv", ".pdf", ".md", ".txt", ".xlsx", ".tsv", ".jsonl")):
            ext_hits.append(os.path.join(dirpath, f))
    if len(ext_hits) > MAX_WALK_FILES:
        break

print("count:", len(ext_hits))
for p in sorted(ext_hits)[:50]:
    try:
        print(f"  {p}  ({os.path.getsize(p)} bytes)")
    except OSError:
        print(f"  {p}")

# Dump structure of anything that could be an abstracts/keywords/index file.
show("4b. STRUCTURE + VERBATIM EXCERPT OF CANDIDATE CORPUS FILES")
CAND = [p for p in ext_hits
        if ("soccer" in p.lower() or "analytic" in p.lower() or "abstract" in p.lower()
            or "keyword" in p.lower() or "literature" in p.lower() or "review" in p.lower()
            or os.path.basename(p) != os.path.basename(os.environ.get("FORUM_JSON", "x")))]
if not CAND:
    print("no candidate corpus files present")
for p in CAND[:6]:
    print(f"\n--- {p} ---")
    try:
        if p.lower().endswith(".json"):
            with open(p, encoding="utf-8", errors="replace") as fh:
                obj = json.load(fh)
            print("type:", type(obj).__name__)
            if isinstance(obj, dict):
                print("top-level keys:", list(obj.keys())[:30])
            elif isinstance(obj, list):
                print("len:", len(obj))
            print("excerpt:", json.dumps(obj, ensure_ascii=False)[:MAX_EXCERPT])
        else:
            with open(p, encoding="utf-8", errors="replace") as fh:
                print("excerpt:", fh.read(MAX_EXCERPT))
    except Exception as exc:
        print("read failed:", type(exc).__name__, exc)

# ------------------------------- 5. is corpus content inside the forum json?
show("5. DOES THE FORUM DATASET CARRY ARTICLE CONTENT?")
fj = os.environ.get("FORUM_JSON")
if fj and os.path.exists(fj):
    try:
        raw = open(fj, encoding="utf-8", errors="replace").read()
        data = json.loads(raw)
        print("forum json size:", len(raw), "bytes")
        print("top-level keys:", list(data.keys())[:20] if isinstance(data, dict) else type(data).__name__)
        low = raw.lower()
        for probe in ("xg", "expected goals", "abstract", "keywords", "soccer_analytics",
                      "example data only", "placeholder", "literature review", "closing line"):
            print(f"  substring {probe!r}: {low.count(probe)} occurrence(s)")
    except Exception as exc:
        print("forum json parse failed:", type(exc).__name__, exc)
else:
    print("FORUM_JSON not readable from workspace")

# -------------------------------------------------------------- 6. verdict
show("6. VERDICT (mechanical, not attested)")
corpus_dir = any(os.path.isdir(p) for p in set(HIT_DIRS + HIT_FILES))
article_files = [p for p in ext_hits if "soccer" in p.lower() or "abstract" in p.lower()
                 or "keyword" in p.lower() or "literature" in p.lower()]
if corpus_dir or article_files:
    print("ACCESS: YES -- see filename list and verbatim excerpts above (sections 3/4b).")
    print("ARTIFACT (required by VCOS t009 s1 / t013 c.0023 s1): filenames listed in s3/s4,")
    print("one verbatim excerpt per candidate file printed in s4b.")
else:
    print("ACCESS: NO -- confirmed by OBSERVATION from inside the workspace, not by tool inventory.")
    print("What was sought : directory or files matching 'soccer*' / *abstract* / *keyword* / *literature*")
    print("By what means   : os.walk + os.path.exists from a real process with a live filesystem,")
    print("                   cwd shown in s1, plus direct path probes in s3b.")
    print("What returned   : the file/dir inventory in s2 and s4 only.")
    print("Gap             : the corpus is not attached to the workspace the staff is operating in.")
print("\nPROBE COMPLETE.")
