import os, subprocess, sys, platform
from pathlib import Path
from datetime import datetime, timezone
root = Path(sys.argv[1]).resolve()
out = Path(sys.argv[2]).resolve()
out.mkdir(parents=True, exist_ok=True)
fixture = root / "packages/shared/src/zz-repro"
fixture.mkdir(parents=True, exist_ok=True)
for filename, namespace in [("control.ts", "Schema"), ("aliased.ts", "S")]:
    (fixture / filename).write_text(f'import * as {namespace} from "effect/Schema";\n\nexport const parseUser = (input: unknown) =>\n  {namespace}.decodeUnknownEffect({namespace}.Struct({{ name: {namespace}.String }}))(input);\n')
sha = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=root, text=True).strip()
env = dict(os.environ, NO_COLOR="1", FORCE_COLOR="0", TERM="dumb")
lines = [f"Commit: {sha}", f"Environment: {platform.system()} {platform.machine()}; real vite-plus lint CLI; no app server or seed", "UTC: " + datetime.now(timezone.utc).isoformat()]
for tool in [["vp", "--version"], ["node", "--version"]]:
    lines.append(subprocess.check_output(tool, cwd=root, text=True).strip())
for filename in ["control.ts", "aliased.ts"]:
    lines.extend(["", f"--- {filename} ---", (fixture / filename).read_text()])
commands = [["vp", "lint", "packages/shared/src/zz-repro/control.ts", "packages/shared/src/zz-repro/aliased.ts"], ["vp", "lint", "packages/shared/src/zz-repro/aliased.ts"]]
for command in commands:
    run = subprocess.run(command, cwd=root, env=env, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    lines.extend(["", "$ " + " ".join(command), run.stdout.replace(str(root), "<checkout>"), f"Exit code: {run.returncode}"])
text = "\n".join(lines) + "\n"
(out / "lint-output.txt").write_text(text)
print(text)
