#!/usr/bin/env python3
"""Know: the scripted half of the get-to-know-you skill. Gather, show, write.

    bin/know gather [--no-sessions] [--no-codex] [--no-repos] [--no-github]
                    read what is already on this Mac into .summary/know/facts.json
    bin/know show [--json]
                    the draft (.summary/know/draft.md, written by the skill), numbered
    bin/know write [--strike 2,5]
                    write only the kept lines: projects/ here, me/ as a proposal the
                    user clicks (inside a run Argoni started; outside one, refused)

Run only on the user's yes. The skill runs `gather`, writes the draft from
facts.json, and stops: nothing is written into me/ or projects/ until the user
has seen the draft and said which lines to strike. The user strikes by number
(`--strike`, from the app or chat) or by deleting lines in draft.md by hand;
both are first-class.

The sources, each the user's to switch off (config.json `know_sources`, or a
`--no-*` flag for one run):

    sessions   Claude Code sessions (bin/sessions): projects, titles, first prompts
    codex      Codex sessions (bin/sessions --codex)
    repos      every repo Argoni sees (lifecfg.repos()): last commit, languages, README line
    github     the logged-in gh account(s): name, bio, public and private repo names
               and descriptions. Read-only; nothing is sent. Not logged in: one line.

The draft's shape (bin/know show refuses anything else, by line number):

    ## profile                      lines for me/profile.md
    ## preferences                  lines for me/preferences.md
    ## project <slug>               lines for projects/<slug>.md; `State:`, `Side:`,
                                    `Last touched:` and `Next:` lines also fill its row
                                    in projects/README.md
    N. <one fact> (from <source>)   one line, numbered across the whole draft

Every line written carries ` (derived: from <source>)` at its end: Argoni drew
it, the user did not say it. The user confirms a line by deleting that marker
(in the file, or the app does it on "Keep"); it is then theirs. New project
pages are `confidence: derived` until the user changes that.

Health, money and family: when they are off (lifecfg.sensitive_allowed), a
draft line that touches them is held back, shown as held, and never written,
whatever the model wrote. The skill is told the same; this is the net under it.

In a demo folder it reads nothing and says NOT ESTABLISHED. The home it reads
is lifecfg.scan_home() (ARGONI_SCAN_HOME for a made-up Mac); with that set, gh
gets that home's config only, so a test never reaches a real GitHub login.

Exit status: 0 done; 1 nothing to show or write; 2 usage, or a draft that does
not parse (the line is named).
"""
import datetime
import json
import os
import re
import subprocess
import sys

BIN = os.path.dirname(os.path.abspath(__file__))
sys.dont_write_bytecode = True  # no __pycache__ beside the engine's scripts
sys.path.insert(0, BIN)
import boxes  # noqa: E402
import lifecfg  # noqa: E402

LIFE = lifecfg.root()
CFG = lifecfg.config(LIFE)
DIR = os.path.join(LIFE, ".summary", "know")
FACTS = os.path.join(DIR, "facts.json")
DRAFT = os.path.join(DIR, "draft.md")
SOURCES = ("sessions", "codex", "repos", "github")
TODAY = datetime.date.today().isoformat()

SECTION = re.compile(r"^## (profile|preferences|project ([a-z0-9][a-z0-9-]{0,60}))\s*$")
LINE = re.compile(r"^(\d+)\. (.+) \(from ([^()]+)\)\s*$")
ROW_FIELDS = {"state": 2, "side": 1, "last touched": 3, "next": 4}
# The net under "nothing about health, money or family unless that setting is on".
SENSITIVE = re.compile(
    r"\b(health\w*|doctor\w*|medic\w*|hospital\w*|therap\w*|diagnos\w*|illness\w*|sick\w*|pregnan\w*|"
    r"diet\w*|weight|fitness|gym|money|salar\w*|income|debt\w*|loan\w*|mortgage\w*|bank\w*|invest\w*|"
    r"tax\w*|budget\w*|finance\w*|financial|famil\w*|wife|husband|son|daughter\w*|kids?|child\w*|"
    r"parents?|mother|father|marri\w*|divorc\w*|wedding\w*)\b", re.I)


def say(msg):
    print("know: " + msg)


def run(cmd, env=None, timeout=60):
    try:
        p = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout, env=env)
    except (OSError, subprocess.TimeoutExpired) as e:
        return 1, "", str(e)
    return p.returncode, p.stdout, p.stderr


# ---------------------------------------------------------------- gather

def gather_sessions(codex):
    code, out, err = run([sys.executable, os.path.join(BIN, "sessions"), "--json"]
                         + (["--codex"] if codex else []), timeout=300)
    if code != 0:
        return None, None, (err.strip() or "bin/sessions failed").splitlines()[-1]
    data = json.loads(out)
    by_cwd = {}
    for rows in data.get("claude", {}).values():
        for r in rows:
            p = by_cwd.setdefault(r["cwd"] or "(unknown folder)", {"folder": r["cwd"], "sessions": 0, "turns": 0,
                                                                   "first": r["first"], "last": r["last"],
                                                                   "titles": [], "prompts": []})
            p["sessions"] += 1
            p["turns"] += r["turns"]
            p["first"], p["last"] = min(p["first"], r["first"]), max(p["last"], r["last"])
            if r["title"] != "(untitled)" and len(p["titles"]) < 8:
                p["titles"].append(r["title"])
            if r["prompt"] and len(p["prompts"]) < 3:
                p["prompts"].append(r["prompt"])
    claude = sorted(by_cwd.values(), key=lambda p: p["last"], reverse=True)[:15]
    cx = None
    if codex:
        groups = {}
        for r in data.get("codex", []):
            if r.get("error"):
                continue
            g = groups.setdefault(r["cwd"], {"folder": r["cwd"], "sessions": 0, "last": "", "prompts": []})
            g["sessions"] += 1
            g["last"] = max(g["last"], r["date"])
            if r["prompt"] and len(g["prompts"]) < 3:
                g["prompts"].append(r["prompt"])
        cx = sorted(groups.values(), key=lambda g: g["last"], reverse=True)[:10]
    return claude, cx, None


def git(repo, *args):
    code, out, _ = lifecfg.git(repo, *args, timeout=30)
    return out if code == 0 else ""


def readme_line(repo):
    for name in ("README.md", "README", "readme.md", "README.rst"):
        path = os.path.join(repo, name)
        if os.path.isfile(path):
            with open(path, encoding="utf-8", errors="replace") as fh:
                for line in fh.read(4000).splitlines():
                    line = line.strip()
                    if line and not line.startswith(("#", "!", "[", "<", "=", "-", "`")):
                        return line[:200]
    return None


def identities(repo):
    """The user's own git names and emails as this repo sees them (its config, then the user's), plus the
    GitHub logins Argoni knows: only commits by these count as theirs (0475: 495 commits in a repo of
    another team were counted as the user's)."""
    out = set()
    for key in ("user.email", "user.name"):
        v = git(repo, "config", "--get", key).strip()
        if v:
            out.add(v)
    for a in CFG.get("github_accounts") or []:
        if isinstance(a, dict) and a.get("login"):
            out.add("%s@users.noreply.github.com" % a["login"])
    return sorted(out)


def own_log(repo, ids, *args):
    """The dates of the user's own commits only, newest first: the author's email equal to one of theirs
    (case aside), or the whole name equal to theirs; never a name that only contains theirs (staff 0475 r1 5)."""
    if not ids:
        return []
    want = {i.strip().casefold() for i in ids if i.strip()}
    out = []
    # Narrowed in git first (a substring match), then checked exactly here: a huge repo stays fast.
    narrow = ["--fixed-strings", "--regexp-ignore-case"] + ["--author=%s" % i for i in ids]
    for line in git(repo, "log", *narrow, "--format=%ae%x09%an%x09%cs", *args).splitlines():
        parts = line.split("\t")
        if len(parts) == 3 and (parts[0].strip().casefold() in want or parts[1].strip().casefold() in want):
            out.append(parts[2])
    return out


def gather_repos():
    found, skipped = lifecfg.repos(LIFE)
    out = []
    since = (datetime.date.today() - datetime.timedelta(days=90)).isoformat()
    for repo in found:
        ids = identities(repo)
        mine = own_log(repo, ids)
        exts = {}
        for f in git(repo, "ls-files").splitlines()[:5000]:
            ext = os.path.splitext(f)[1].lower()
            if ext:
                exts[ext] = exts.get(ext, 0) + 1
        remote = git(repo, "remote", "get-url", "origin").strip()
        remote = re.sub(r"^https://[^@/]+@", "https://", remote)  # never a token in a URL
        out.append({
            "name": os.path.basename(repo.rstrip("/")),
            "folder": repo,
            # "yours": the user committed here; "read": a repo they only read (another team's, a clone).
            "role": "yours" if mine else "read",
            "your_commits": len(mine),
            "your_commits_90_days": sum(1 for d in mine if d >= since),
            "your_last_commit": mine[0] if mine else None,
            "your_first_commit": mine[-1] if mine else None,
            "last_commit_by_anyone": git(repo, "log", "-1", "--format=%cs").strip() or None,
            "file_types": dict(sorted(exts.items(), key=lambda kv: -kv[1])[:6]),
            "readme": readme_line(repo),
            "remote": remote or None,
            "standing": os.path.isfile(os.path.join(repo, "docs", "standing.md")),
        })
    out.sort(key=lambda r: (r["role"] == "yours", r["your_last_commit"] or "", r["last_commit_by_anyone"] or ""), reverse=True)
    return out, skipped


def gh_env():
    env = dict(os.environ)
    if os.environ.get("ARGONI_SCAN_HOME"):
        home = lifecfg.scan_home()
        env.update(HOME=home, GH_CONFIG_DIR=os.path.join(home, ".config", "gh"))
        env.pop("GH_TOKEN", None)
        env.pop("GITHUB_TOKEN", None)
    return env


def gather_github():
    env = gh_env()
    logins = [a["login"] for a in CFG["github_accounts"] if a.get("login")] or [None]
    accounts, lines = [], []
    for login in logins:
        e = dict(env)
        if login:
            code, tok, _ = run(["gh", "auth", "token", "--user", login], env=env, timeout=20)
            if code != 0:
                lines.append("%s: not logged in to gh, not read" % login)
                continue
            e["GH_TOKEN"] = tok.strip()
        else:
            code, _, _ = run(["gh", "auth", "status"], env=env, timeout=20)
            if code != 0:
                lines.append("not logged in to gh, not read")
                continue
        code, out, err = run(["gh", "api", "user"], env=e)
        if code != 0:
            lines.append("%s: gh api user failed: %s" % (login or "gh", (err.strip() or "no output").splitlines()[-1]))
            continue
        user = json.loads(out)
        code, out, _ = run(["gh", "repo", "list", user.get("login", ""), "--limit", "30", "--json",
                            "name,description,pushedAt,primaryLanguage,isPrivate,isFork"], env=e)
        repos = json.loads(out) if code == 0 and out.strip() else []
        accounts.append({"login": user.get("login"), "name": user.get("name"), "bio": user.get("bio"),
                         "company": user.get("company"), "location": user.get("location"),
                         "repos": [{"name": r.get("name"), "description": r.get("description"),
                                    "pushed": (r.get("pushedAt") or "")[:10], "private": r.get("isPrivate"),
                                    "fork": r.get("isFork"),
                                    "language": (r.get("primaryLanguage") or {}).get("name")} for r in repos]})
    return accounts, lines


def gather(args):
    if lifecfg.demo(LIFE):
        say("NOT ESTABLISHED, demo folder: this Mac is not read")
        return 0
    on = dict((k, True) for k in SOURCES)
    on.update({k: bool(v) for k, v in (CFG.get("know_sources") or {}).items() if k in on})
    for a in args:
        if not a.startswith("--no-") or a[5:] not in on:
            print("usage: bin/know gather [--no-sessions] [--no-codex] [--no-repos] [--no-github]", file=sys.stderr)
            return 2
        on[a[5:]] = False
    facts = {"generated_at": datetime.datetime.now().astimezone().isoformat(timespec="seconds"),
             "sources": {}, "sensitive_allowed": lifecfg.sensitive_allowed(CFG), "skipped": [],
             "work_computer": CFG.get("work_computer") is not False}
    if on["sessions"] or on["codex"]:
        claude, codex, err = gather_sessions(on["codex"])
        if err:
            facts["sources"]["sessions"] = "not read: " + err
        else:
            if on["sessions"]:
                facts["sessions"] = claude
                facts["sources"]["sessions"] = "%d project folder(s)" % len(claude)
            if on["codex"]:
                facts["codex"] = codex
                facts["sources"]["codex"] = "%d folder(s)" % len(codex or [])
    for k in ("sessions", "codex"):
        if not on[k]:
            facts["sources"][k] = "off"
    if on["repos"]:
        facts["repos"], skipped = gather_repos()
        facts["skipped"] += skipped
        facts["sources"]["repos"] = "%d repo(s)" % len(facts["repos"])
    else:
        facts["sources"]["repos"] = "off"
    if on["github"]:
        facts["github"], lines = gather_github()
        facts["skipped"] += lines
        facts["sources"]["github"] = ("%d account(s)" % len(facts["github"])) if facts["github"] else "not read"
    else:
        facts["sources"]["github"] = "off"
    os.makedirs(DIR, exist_ok=True)
    lifecfg.write_atomic(FACTS, json.dumps(facts, indent=1, ensure_ascii=False) + "\n")
    say("%s; %s" % (os.path.relpath(FACTS, LIFE),
                    "; ".join("%s: %s" % kv for kv in facts["sources"].items())))
    for line in facts["skipped"]:
        say("skipped: " + line)
    return 0


# ---------------------------------------------------------------- show and write

def parse():
    """[(n, target, text, source, held)] from draft.md. Exits 2 on a line that is not the shape."""
    if not os.path.isfile(DRAFT):
        say("no draft yet (%s); the skill writes it after gather" % os.path.relpath(DRAFT, LIFE))
        sys.exit(1)
    allowed = lifecfg.sensitive_allowed(CFG)
    out, target, numbers = [], None, set()
    with open(DRAFT, encoding="utf-8") as fh:
        text = fh.read()
    body = re.sub(r"\A---\n.*?\n---\n", "", text, flags=re.S)
    for i, line in enumerate(body.splitlines(), start=1):
        s = line.strip()
        if s.startswith("## "):
            m = SECTION.match(s)
            if not m:
                say("draft: `%s` is not `## profile`, `## preferences` or `## project <slug>`" % s)
                sys.exit(2)
            target = m.group(1)
            continue
        if not re.match(r"^\d+\. ", s):
            continue
        m = LINE.match(s)
        if not m or target is None:
            say("draft: not `N. <fact> (from <source>)` under a section: %s" % s)
            sys.exit(2)
        n = int(m.group(1))
        if n in numbers:
            say("draft: number %d is used twice" % n)
            sys.exit(2)
        numbers.add(n)
        held = not allowed and bool(SENSITIVE.search(m.group(2)))
        out.append({"n": n, "target": target, "text": m.group(2).strip(), "from": m.group(3).strip(),
                    "held": held})
    return out


def show(args):
    lines = parse()
    if "--json" in args:
        json.dump({"draft": os.path.relpath(DRAFT, LIFE), "lines": lines}, sys.stdout, indent=1, ensure_ascii=False)
        print()
        return 0
    target = None
    for l in lines:
        if l["target"] != target:
            target = l["target"]
            print("\n## %s" % target)
        print("%d. %s (from %s)%s" % (l["n"], l["text"], l["from"],
                                      "  [held back: health, money or family are off]" if l["held"] else ""))
    return 0 if lines else 1


def set_updated(text):
    return re.sub(r"(?m)^updated: .*$", "updated: %s" % TODAY, text, count=1)


def append_section(path, items):
    """Adds `## Found by Argoni, DATE` (or extends today's) with one bullet per item."""
    with open(path, encoding="utf-8") as fh:
        text = fh.read()
    heading = "## Found by Argoni, %s" % TODAY
    # A line already in the file is not added twice (a write cut short and run again).
    # Whole lines, as new_project() and this function write them ("- X", "- **State:** X", "X").
    have = {re.sub(r"^(?:- )?", "", x.strip()) for x in text.split("\n")}

    def written(l):
        key, value = field(l["text"])
        forms = [l["text"]] + (["**%s:** %s" % (key.capitalize(), value)] if key else [])
        return any("%s (derived: from %s)" % (f, l["from"]) in have for f in forms)
    bullets = "\n".join("- %s (derived: from %s)" % (l["text"], l["from"]) for l in items if not written(l))
    if not bullets:
        return
    if heading in text:
        start = text.index(heading)
        nxt = text.find("\n## ", start + len(heading))
        end = len(text) if nxt < 0 else nxt
        text = text[:end].rstrip("\n") + "\n" + bullets + "\n" + text[end:]
    else:
        text = text.rstrip("\n") + "\n\n" + heading + "\n\n" + bullets + "\n"
    lifecfg.write_atomic(path, set_updated(text))


def field(text):
    """("state", "active") for `State: active`, else (None, text)."""
    m = re.match(r"^(State|Side|Last touched|Next):\s*(.+?)\.?$", text, re.I)
    return (m.group(1).lower(), m.group(2)) if m else (None, text)


def parts(text):
    """One project line (0475: one line per project): "What it is · State: active · Last touched: ..." as
    (what it is, {field: value}). A line of one field alone still works as before."""
    desc, keys = [], {}
    for piece in text.split(" · "):
        key, value = field(piece.strip())
        if key:
            keys[key] = value
        elif piece.strip():
            desc.append(piece.strip())
    return " · ".join(desc), keys


def new_project(slug, items):
    path = os.path.join(LIFE, "projects", slug + ".md")
    sources = sorted({l["from"].split(":")[0].strip() for l in items})
    lines = ["---", "kind: note", "subject: %s" % slug, "author: argoni", "confidence: derived",
             "area: projects", "provenance: get-to-know-you (from %s)" % ", ".join(sources),
             "updated: %s" % TODAY, "---", "", "# %s" % slug, ""]
    desc = [l for l in items if parts(l["text"])[0]]
    if desc:
        lines += ["%s (derived: from %s)" % (parts(desc[0]["text"])[0], desc[0]["from"]), ""]
    for l in items:
        what, keys = parts(l["text"])
        if what and l is not (desc[0] if desc else None):
            lines.append("- %s (derived: from %s)" % (what, l["from"]))
        for key, value in keys.items():
            lines.append("- **%s:** %s (derived: from %s)" % (key.capitalize(), value, l["from"]))
    if os.path.lexists(path):  # made since the caller looked: never written over
        raise OSError("%s appeared meanwhile" % os.path.relpath(path, LIFE))
    lifecfg.write_atomic(path, "\n".join(lines).rstrip("\n") + "\n")


def add_row(slug, items):
    """A row in projects/README.md for a page that has none, its cells from the
    State/Side/Last touched/Next lines; empty when the draft did not say."""
    index = os.path.join(LIFE, "projects", "README.md")
    if not os.path.isfile(index):
        return False
    with open(index, encoding="utf-8") as fh:
        text = fh.read()
    if "](%s.md)" % slug in text:
        return False
    cells = ["[%s](%s.md)" % (slug, slug), "", "", "", ""]
    for l in items:
        for key, value in parts(l["text"])[1].items():
            cells[ROW_FIELDS[key]] = value.replace("|", "/")
    row = "| " + " | ".join(cells) + " |"
    lines = text.rstrip("\n").split("\n")
    last = max((i for i, l in enumerate(lines) if l.startswith("|")), default=None)
    if last is None:
        return False
    lines.insert(last + 1, row)
    lifecfg.write_atomic(index, set_updated("\n".join(lines) + "\n"))
    return True


def write(args):
    struck = set()
    it = iter(args)
    for a in it:
        if a != "--strike":
            print("usage: bin/know write [--strike 2,5]", file=sys.stderr)
            return 2
        val = next(it, "")
        if not re.fullmatch(r"\s*(\d+\s*(,\s*\d+\s*)*)?", val):
            print("know: --strike takes numbers, like 2,5", file=sys.stderr)
            return 2
        struck |= {int(x) for x in re.findall(r"\d+", val)}
    lines = parse()
    unknown = struck - {l["n"] for l in lines}
    if unknown:
        say("no line %s in the draft; nothing written" % ", ".join(map(str, sorted(unknown))))
        return 2
    kept = [l for l in lines if l["n"] not in struck and not l["held"]]
    held = [l for l in lines if l["held"]]
    written, failed, proposed = {}, [], {}
    rows = 0
    for slug in sorted({l["target"][8:] for l in kept if l["target"].startswith("project ")}):
        items = [l for l in kept if l["target"] == "project " + slug]
        path = os.path.join(LIFE, "projects", slug + ".md")
        try:
            if os.path.lexists(path):
                append_section(path, items)
            else:
                os.makedirs(os.path.dirname(path), exist_ok=True)
                new_project(slug, items)
            rows += add_row(slug, items)
            written["projects/%s.md" % slug] = len(items)
        except OSError as e:
            failed.append("projects/%s.md (%s)" % (slug, e))
    # What Argoni knows about the user steers every later run, so these lines are never
    # written into me/ here: they go to the user as one proposal per file, all or none, and
    # their click in Argoni adds them (brief 0117; the runner fences me/ for every run).
    # After the project pages: a write cut short keeps the draft, nothing proposed twice.
    docs, counts = [], {}
    hidden = [l["n"] for l in kept if l["target"] in ("profile", "preferences") and boxes.HIDDEN.search(l["text"] + l["from"])]
    if hidden:
        say("line %s held back: it holds text that would not show on the card" % ", ".join(map(str, hidden)))
        kept = [l for l in kept if l["n"] not in hidden]
        held = held + [l for l in lines if l["n"] in hidden]
    for target in ("profile", "preferences"):
        items = [l for l in kept if l["target"] == target]
        if items:
            docs.append({"kind": target, "text": "## Found by Argoni, %s\n\n%s" % (TODAY, "\n".join(
                "- %s (derived: from %s)" % (l["text"], l["from"]) for l in items))})
            counts["me/%s.md" % target] = len(items)
    if docs and not failed:
        try:
            boxes.propose_all(docs, LIFE)
            proposed = counts
        except (boxes.Refused, OSError) as e:
            failed.append("me/ (%s)" % e)
    if failed:
        # The draft stays: run write again and the lines already written are not doubled.
        done_now = ["%s (%d)" % kv for kv in written.items()] + ["%s (%d, proposed)" % kv for kv in proposed.items()]
        say("not written: %s; the draft is kept, %s" % ("; ".join(failed), ", ".join(done_now) + " done"
                                                        if done_now else "nothing else written"))
        return 1
    done = os.path.join(DIR, "written-%s.md" % datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S"))
    os.replace(DRAFT, done)
    say("kept %d, struck %d, held back %d; %s%s%s; the draft is now %s" % (
        len(kept), len(struck), len(held),
        ", ".join("%s (%d)" % kv for kv in written.items()) or "nothing written",
        "; proposed for your click: %s" % ", ".join("%s (%d)" % kv for kv in proposed.items()) if proposed else "",
        "; %d new row(s) in projects/README.md" % rows if rows else "", os.path.relpath(done, LIFE)))
    return 0 if kept else 1


def main(argv):
    verbs = {"gather": gather, "show": show, "write": write}
    if len(argv) < 2 or argv[1] not in verbs:
        print("usage: bin/know gather|show|write ...", file=sys.stderr)
        return 2
    return verbs[argv[1]](argv[2:])


if __name__ == "__main__":
    try:
        sys.exit(main(sys.argv))
    except OSError as e:  # a write refused (lifecfg.inside) or the disk: one line, no traceback
        print("know: FAIL, %s" % e, file=sys.stderr)
        sys.exit(1)
