#!/bin/sh
""":"
case "$0" in
    # Inside the app: always this bundle's Python, never a home inherited from another one.
    *.app/Contents/Resources/*) bundle=${0%%.app/Contents/Resources/*}.app
                                export PYTHONHOME="$bundle/Contents/Resources/python" ;;
esac
exec "$PYTHONHOME/bin/python3" -I -S -B "$0" "$@"
": """
__doc__ = """Read bounded, sourced status from one life folder's data only."""
import datetime
import hashlib
import importlib.machinery
import importlib.util
import json
import os
import re
import stat
import sys

sys.dont_write_bytecode = True
MAX_FILE = 256 * 1024
MAX_OUTPUT = 8 * 1024
TODAY = datetime.date.today().isoformat()


def report_parser():
    path = os.path.join(os.path.dirname(__file__), "reports")
    loader = importlib.machinery.SourceFileLoader("argoni_reports_parser", path)
    spec = importlib.util.spec_from_loader(loader.name, loader)
    mod = importlib.util.module_from_spec(spec)
    loader.exec_module(mod)
    return mod


def read(data, rel):
    """Open a plain, singly linked file through data/ only; never follow a link."""
    parts = rel.split("/")
    if not parts or any(not p or p in (".", "..") or p.startswith(".") for p in parts):
        return None
    fd = os.open(data, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
    try:
        for i, part in enumerate(parts):
            nxt = os.open(part, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK |
                          (os.O_DIRECTORY if i < len(parts) - 1 else 0), dir_fd=fd)
            os.close(fd)
            fd = nxt
        s = os.fstat(fd)
        if not stat.S_ISREG(s.st_mode) or s.st_nlink != 1 or s.st_size > MAX_FILE:
            return None
        return os.read(fd, MAX_FILE + 1).decode("utf-8", "replace")
    except OSError:
        return None
    finally:
        os.close(fd)


def names(data, sub, pattern=r".*"):
    if sub.startswith(".") or "/." in sub:
        return []
    path = os.path.join(data, sub)
    if os.path.islink(path) or not os.path.isdir(path):
        return []
    return [n for n in sorted(os.listdir(path)) if re.fullmatch(pattern, n) and not n.startswith(".")]


def item(text, path, line, **more):
    return dict(text=text[:500], source={"path": path, "line": line}, **more)


def lines_under(text, heading):
    active = False
    for n, line in enumerate(text.splitlines(), 1):
        if line.startswith("## "):
            active = line[3:].strip().casefold() == heading.casefold()
        elif active and re.match(r"^\s*(?:[-*]|\d+\.)\s+", line):
            yield n, re.sub(r"^\s*(?:[-*]|\d+\.)\s+", "", line).strip()


def own_needs(raw):
    """His own to-dos under "What needs you", as bin/summary groups them: lines under "Decisions waiting in
    projects:" are the decisions' own items (listed by decisions()), never a need of their own (0484, F-009:
    one showed twice before the first summary, under a key the summary never gives)."""
    active = waiting = False
    for n, line in enumerate(raw.splitlines(), 1):
        if line.startswith("## "):
            active, waiting = line[3:].strip().casefold() == "what needs you", False
        elif active and re.match(r"^[A-Z][^:]*:\s*$", line):
            waiting = line.strip()[:-1].strip() == "Decisions waiting in projects"
        elif active and not waiting and re.match(r"^\s*(?:[-*]|\d+\.)\s+", line):
            yield n, re.sub(r"^\s*(?:[-*]|\d+\.)\s+", "", line).strip()


def projects(data, parser):
    index = read(data, "projects/README.md") or ""
    rows = {}
    for n, line in enumerate(index.splitlines(), 1):
        cells = [c.strip() for c in line.strip().strip("|").split("|")]
        if len(cells) != 5:
            continue
        m = re.fullmatch(r"\[([^]]+)\]\(([a-z0-9-]+)\.md\)", cells[0])
        if m:
            rows[m[2]] = (n, m[1], cells)
    out = []
    for name in names(data, "projects", r"[a-z0-9-]+\.md"):
        if name == "README.md":
            continue
        slug = name[:-3]
        raw = read(data, "projects/" + name)
        if raw is None:
            continue
        meta, body = parser.front(raw)
        title = parser.title_of(body) or slug
        row = rows.get(slug)
        cells = row[2] if row else ["", "", "", "", ""]
        out.append({"id": slug, "name": title[:120], "state": cells[2] or None,
                    "last_touched": cells[3] or meta.get("updated"), "next": cells[4] or None,
                    "source": {"path": "projects/README.md" if row else "projects/" + name,
                               "line": row[0] if row else 1}, "page": "projects/" + name})
    return out


def reports(data, parser):
    out = []
    for name in names(data, "reports", r"\d{4}[A-Z]?-[^.]+\.md"):
        path = "reports/" + name
        raw = read(data, path)
        if raw is None:
            continue
        meta, body = parser.front(raw)
        if "brief" not in meta:
            continue
        state = "accepted" if parser.filled_readback(body) or parser.READBACK_LINE.search(raw) else "open"
        out.append(item(parser.title_of(body) or name, path, next((i for i, x in enumerate(raw.splitlines(), 1) if x.startswith("# ")), 1),
                        state=state, date=meta.get("date"), brief=meta.get("brief"), subject=meta.get("subject")))
    return out


def decisions(data, parser):
    out = []
    for name in names(data, "decisions", r"\d{4}-[^.]+\.md"):
        path = "decisions/" + name
        raw = read(data, path)
        if raw is None:
            continue
        meta, body = parser.front(raw)
        out.append(item(parser.title_of(body) or name, path, next((i for i, x in enumerate(raw.splitlines(), 1) if x.startswith("# ")), 1),
                        state=meta.get("status"), date=meta.get("date"), revisit=meta.get("revisit")))
    return out


def read_letter(data):
    """The list the app's letter and greeting are made from (0432): data/.summary/for-you.json, read
    the same careful way as every other file (no link, one name, bounded). None when it is not there
    or not version 1."""
    fd = os.open(data, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
    try:
        sub = os.open(".summary", os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=fd)
        os.close(fd)
        fd = sub
        f = os.open("for-you.json", os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=fd)
        try:
            s = os.fstat(f)
            if not stat.S_ISREG(s.st_mode) or s.st_nlink != 1 or s.st_size > 4 * MAX_FILE:
                return None
            raw = b""
            while len(raw) <= 4 * MAX_FILE:
                chunk = os.read(f, 65536)
                if not chunk:
                    break
                raw += chunk
        finally:
            os.close(f)
        doc = json.loads(raw.decode("utf-8", "replace"))
    except (OSError, ValueError):
        return None
    finally:
        os.close(fd)
    return doc if isinstance(doc, dict) and doc.get("version") == 1 else None


def stale_reminder_need_keys(data):
    """Legacy one-off lines cease to be needs after the day they fired."""
    raw = read(data, "now.md") or ""
    return {"need:" + hashlib.sha1(t.encode("utf-8") + b"\0").hexdigest()[:10]
            for _, t in lines_under(raw, "What needs you")
            for m in [re.search(r"\(scheduled remind-[a-z0-9-]+\).*?from log/(\d{4}-\d{2}-\d{2})\.md", t)]
            if m and m.group(1) < TODAY}


def letter_needs(data):
    """What waits for the user exactly as the letter counts it: the needs oldest first (by since, then
    key, as the app sorts them), then the decisions waiting on the user's word or due for a look
    within 7 days. None when there is no letter list yet."""
    doc = read_letter(data)
    if doc is None:
        return None
    stale = stale_reminder_need_keys(data)
    rows = [n for n in doc.get("needs") or [] if isinstance(n, dict) and isinstance(n.get("key"), str)
            and n["key"] not in stale]
    rows.sort(key=lambda n: (str(n.get("since") or ""), n["key"]))
    out = []
    for n in rows:
        words = n.get("for_you") if isinstance(n.get("for_you"), dict) else {}
        head = str(words.get("headline") or "").strip()
        project = n.get("project") if isinstance(n.get("project"), str) else None
        src = n.get("source") if n.get("source") in ("now.md",) else ".summary/for-you.json"
        out.append(dict(item((head + (" (%s)" % project if project else "")) if head else "something without words yet",
                             src, 1, kind="need"),
                        key=n["key"], since=n.get("since"), project=project, came_from=str(n.get("source") or "")[:40],
                        detail=str(words.get("body") or "")[:500] or None))
    decisions = [d for d in doc.get("decisions") or [] if isinstance(d, dict)]
    # Oldest first, as the needs are (0433's s14: "the oldest decision" named the newer one).
    decisions.sort(key=lambda d: (str(d.get("date") or "9999"), str(d.get("key") or "")))
    for d in decisions:
        status = str(d.get("status") or "")
        days = d.get("days_to_revisit")
        if not (status == "proposed" or status.startswith("waiting") or (isinstance(days, int) and days <= 7)):
            continue
        words = d.get("for_you") if isinstance(d.get("for_you"), dict) else {}
        name = str(d.get("name") or d.get("id") or d.get("path") or "a decision")
        out.append(dict(item(name + (" (%s)" % d["project"] if isinstance(d.get("project"), str) else ""),
                             ".summary/for-you.json", 1, kind="decision"),
                        key=d.get("key"), status=status or None, revisit=d.get("revisit"),
                        project=d.get("project") if isinstance(d.get("project"), str) else None,
                        came_from="repo" if d.get("repo") else "decisions",
                        decided=str(words.get("decided") or "")[:500] or None))
    return out


def needs(data, parser):
    """The chat's "what needs me": the letter's own list when there is one (0432: the greeting said
    "Four things need you; the oldest: enterprise brain ..." while this read now.md alone and found
    three reminders, so the chat denied what the greeting said), else the files themselves."""
    got = letter_needs(data)
    return got if got is not None else needs_files(data, parser)


def needs_files(data, parser):
    raw = read(data, "now.md") or ""
    stale = stale_reminder_need_keys(data)
    # Each with the key the summary will give it (bin/foryou), so Not now hides it before the first summary too (0480, F-009).
    out = [dict(item(t, "now.md", n, kind="need"), key=k) for n, t in own_needs(raw)
           for k in ["need:" + hashlib.sha1(t.encode("utf-8") + b"\0").hexdigest()[:10]] if k not in stale]
    out += [dict(x, kind="read_back", key="need:report-" + os.path.basename(x["source"]["path"])[:-3])
            for x in reports(data, parser) if x["state"] == "open"]
    out += [dict(x, kind="decision") for x in decisions(data, parser) if x["state"] in ("proposed", "waiting")]
    jobs = read(data, "jobs.json")
    if jobs:
        try:
            doc = json.loads(jobs)
            for key, val in (doc.items() if isinstance(doc, dict) else []):
                due = str(val.get("at") or "") if isinstance(val, dict) else ""
                if isinstance(val, dict) and val.get("runs") == "remind" and val.get("on") and re.fullmatch(r"\d{4}-\d{2}-\d{2}", due[:10]) and due[:10] == TODAY:
                    out.append(dict(item(str(val.get("text") or key), "jobs.json", 1, kind="reminder_due"), key="need:job-" + key))
        except ValueError:
            pass
    return out


def work_pieces(mode):
    """Optional runner projection on stdin; no state path is opened."""
    loader = importlib.machinery.SourceFileLoader("work_journal_view", os.path.join(os.path.dirname(__file__), "work-journal-view"))
    spec = importlib.util.spec_from_loader(loader.name, loader)
    mod = importlib.util.module_from_spec(spec)
    loader.exec_module(mod)
    try:
        doc = json.load(sys.stdin)
    except (ValueError, UnicodeError):
        return []
    try:
        return mod.view(doc, mode)["pieces"]
    except (KeyError, TypeError, ValueError):
        return []


def project(data, parser, query):
    all_projects = projects(data, parser)
    q = query.casefold().strip()
    matched = [x for x in all_projects if q in (x["id"].casefold(), x["name"].casefold())]
    if len(matched) > 1:
        return {"matches": [{"id": x["id"], "name": x["name"], "source": x["source"]} for x in matched]}
    if not matched:
        return {"found": False, "matches": []}
    x = matched[0]
    name = x["id"]
    waiting = [i for i in needs_files(data, parser) if name.casefold() in i["text"].casefold()]
    related = [i for i in reports(data, parser) if name.casefold() in (i.get("subject") or "").casefold() or
               name.casefold() in i["source"]["path"].casefold()]
    open_decisions = [i for i in decisions(data, parser) if i["state"] in ("proposed", "waiting") and
                      name.casefold() in i["text"].casefold()]
    return {"found": True, "project": x, "reports": related[-5:], "open_decisions": open_decisions,
            "waiting_for_you": waiting[:10]}


def today(data, parser):
    logs = []
    path = "log/" + TODAY + ".md"
    raw = read(data, path)
    if raw:
        logs = [item(line.lstrip("- ").strip(), path, n, kind="log") for n, line in enumerate(raw.splitlines(), 1) if line.startswith("- ")]
    ledger, reminders = [], []
    raw = read(data, "log/runs.jsonl")
    if raw:
        for n, line in enumerate(raw.splitlines(), 1):
            try:
                row = json.loads(line)
            except ValueError:
                continue
            if isinstance(row, dict) and str(row.get("at", ""))[:10] == TODAY:
                status = str(row.get("status") or "")
                # 0222: a job with no model that ended well ("no model: ...", exit 0: a reminder that came) is
                # ok; it was read as failed, and the chat told him his reminder's run had failed.
                outcome = ("ok" if status == "ok" or (status.startswith("no model") and row.get("exit") == 0) else
                           "skipped" if status.startswith("skipped") else
                           "refused" if status.startswith("refused") else "failed")
                ledger.append(item(str(row.get("job") or "run"), "log/runs.jsonl", n, kind="run",
                                   outcome=outcome))
                for reminder in (row.get("reminders_added") or []):
                    if isinstance(reminder, dict):
                        reminders.append(item(str(reminder.get("text") or reminder.get("name") or "reminder"),
                                              "log/runs.jsonl", n, kind="reminder_set"))
                job = str(row.get("job") or "")
                if job == "remind" or re.fullmatch(r"job:remind-[a-z0-9-]{1,30}", job):
                    # 0222: a one-off reminder's own run is "job:remind-<id>" (the scheduler's): it came.
                    # Recognised by that name only: if a reminder ever gets a chosen name, match it here too.
                    reminders.append(item(str(row.get("for") or job.split(":", 1)[-1] or "reminder"), "log/runs.jsonl", n,
                                          kind="reminder_fired", outcome=outcome))
    filed = [i for i in reports(data, parser) if i.get("date") == TODAY]
    for name in names(data, "inbox", r"[^.]+\.md"):
        path = "inbox/" + name
        text = read(data, path)
        if text is not None:
            meta, body = parser.front(text)
            if meta.get("updated") == TODAY or meta.get("date") == TODAY:
                filed.append(item(parser.title_of(body) or name, path, 1, kind="filed_note"))
    decided = [i for i in decisions(data, parser) if i.get("date") == TODAY]
    return {"date": TODAY, "log": logs, "reports": filed, "runs": ledger,
            "decisions": decided, "reminders": reminders}


# 0477: a brief whose status says it is finished or given up is not open, report or not.
CLOSED = re.compile(r"^\W*(done|dropped|parked|superseded|closed|cancell?ed|abandoned|rejected|withdrawn|"
                    r"retired|archived|merged|replaced|reported|complete|completed|finished)\b", re.I)


def open_briefs(data):
    """Open briefs as the Files page lists them (for-you.json): Argoni's own briefs not done, parked or
    reported; another repo's brief with no report at the same number (as `standings` counts them)."""
    doc = read_letter(data) or {}
    files = [f for f in doc.get("files") or [] if isinstance(f, dict) and isinstance(f.get("path"), str)]
    reported = set()
    for f in files:
        m = re.search(r"(?:^|/)reports/(\d{4})", f["path"])
        if f.get("type") == "report" and m:
            reported.add((f.get("repo"), m.group(1)))
    out = []
    for f in files:
        m = re.search(r"(?:^|/)briefs/(\d{4})", f["path"])
        if f.get("type") != "brief" or not m:
            continue
        repo = f.get("repo") if isinstance(f.get("repo"), str) else None
        if repo is None:
            if f.get("pair") or str(f.get("status_words") or "") in ("done", "parked"):
                continue
        elif (repo, m.group(1)) in reported or CLOSED.match(str(f.get("status_words") or "")):
            continue
        out.append({"title": str(f.get("title") or m.group(1))[:200], "number": m.group(1),
                    "project": repo or str(f.get("project") or "") or None, "date": f.get("date")})
    out.sort(key=lambda x: (str(x["project"] or ""), x["number"]))
    return out


def standings(data, parser):
    """Where each project stands (the Projects page) and the open briefs (the Files page)."""
    doc = read_letter(data) or {}
    # 0477: each repo's last commit and when this list was made, so the chat says the date it is from.
    repos = [{"name": str(r.get("name"))[:120], "last_commit": r.get("last_commit")}
             for r in doc.get("repos") or [] if isinstance(r, dict) and r.get("name")]
    commits = {r["name"]: r["last_commit"] for r in repos}
    # The project page's own date is when the page was last written, never the repo's last commit (0477:
    # the chat read it as "no commit since Oct 20"); the repo's real last commit sits beside it when known.
    rows = [{"id": x["id"], "name": x["name"], "state": x["state"], "next": x["next"],
             "page_written": x["last_touched"], "last_commit": commits.get(x["id"])} for x in projects(data, parser)]
    return {"projects": rows, "open_briefs": open_briefs(data), "repos": repos, "as_of": doc.get("generated_at")}


# The Scheduled page's plain names and words (the app's ArgoniScheduledJob.names and .descriptions), so
# the chat says what the page says.
PLAIN_JOBS = {
    "morning": ("Morning summary", "What changed, what needs you today, and decisions due."),
    "today": ("Today's page", "Writes your Today page."),
    "words": ("Words for your lists", "Writes plain words for new things in your lists, only when one waits."),
    "standings": ("Project check", "Checks that each project's page is up to date. No AI."),
    "inbox": ("Your inbox", "Suggests a home for each new file in your inbox, only when one waits. Files nothing itself."),
    "end-of-day": ("End of day", "Your day against the morning, and what is still open."),
    "sessions": ("Your AI sessions", "What each of your AI sessions did today, only when there were some."),
    "revisits": ("Decisions to look at again", "Decisions and ideas due for another look. No AI."),
    "weekly-review": ("Weekly review", "Checks that every project page is still true, and sums up your week."),
}


def scheduled(data):
    """The Scheduled page's list, reminders included: each with its plain name, whether it is on, when it
    next runs, how its last run went, and whether a one-time reminder is done."""
    import subprocess
    program = os.path.join(os.path.dirname(os.path.abspath(__file__)), "scheduled")
    env = {"PATH": "/usr/bin:/bin", "LIFE_FOLDER": data, "HOME": os.environ.get("HOME", "/")}
    if os.environ.get("PYTHONHOME"):
        env["PYTHONHOME"] = os.environ["PYTHONHOME"]
    # The door stops this read after 30 s (F-011, 0480): the child gets 20, and a stop from the door ends the
    # child too (subprocess.run kills it when the wait is broken by the exit the handler raises).
    import signal
    signal.signal(signal.SIGTERM, lambda *_: sys.exit(143))
    try:
        p = subprocess.run([sys.executable, "-I", "-S", "-B", program, "list", "--json"], env=env, cwd=data,
                           capture_output=True, text=True, timeout=20)
        doc = json.loads(p.stdout) if p.returncode == 0 else None
    except (OSError, ValueError, subprocess.SubprocessError):
        doc = None
    if not isinstance(doc, dict):
        return {"jobs": [], "unread": True}
    out = []
    for j in doc.get("jobs") or []:
        if not isinstance(j, dict):
            continue
        reminder = j.get("runs") == "remind"
        key = str(j.get("name") or "")
        plain = PLAIN_JOBS.get(key)
        name = str(j.get("text") or j.get("title") or key) if reminder else (plain[0] if plain else key.replace("-", " ").capitalize())
        code = j.get("exit")
        last = j.get("last_run")
        out.append({"name": name[:200], "kind": "reminder" if reminder else "job",
                    "description": None if reminder else (plain[1] if plain else str(j.get("description") or "")[:300] or None),
                    "on": bool(j.get("on")), "when": re.sub(r";\s*the model only when.*$", "", re.sub(r"\s*\([^)]*(?:config\.json|week_ends)[^)]*\)", "", str(j.get("when") or ""))) or None, "next_run": j.get("next_run"),
                    "last_run": last, "last_result": None if not last else ("ok" if code == 0 else "failed"),
                    "done": bool(j.get("done"))})
    return {"jobs": out}


def bounded(value, limit=MAX_OUTPUT):
    cut = False
    def shorten(obj):
        nonlocal cut
        if isinstance(obj, str):
            if len(obj) > 500:
                cut = True
                return obj[:500]
            return obj
        if isinstance(obj, list):
            return [shorten(x) for x in obj]
        if isinstance(obj, dict):
            return {k: shorten(v) for k, v in obj.items()}
        return obj
    value = shorten(value)
    value["cut"] = cut
    raw = json.dumps(value, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
    if len(raw) <= limit:
        return raw
    value["cut"] = True
    # Remove trailing sourced items until the result fits, preserving valid JSON.
    def trim(obj):
        if isinstance(obj, dict):
            for v in obj.values():
                if trim(v):
                    return True
        if isinstance(obj, list) and obj:
            obj.pop()
            return True
        return False
    while len(json.dumps(value, ensure_ascii=False, separators=(",", ":")).encode("utf-8")) > limit:
        if not trim(value):
            return json.dumps({"kind": value.get("kind"), "cut": True}, separators=(",", ":")).encode("utf-8")
    return json.dumps(value, ensure_ascii=False, separators=(",", ":")).encode("utf-8")


def main(args):
    with_projection = bool(args and args[-1] == "--projection-stdin")
    if with_projection:
        args = args[:-1]
    if len(args) not in (2, 3) or args[0] not in ("project", "today", "needs", "standings", "scheduled") or (args[0] == "project") != (len(args) == 3) or (with_projection and args[0] == "project"):
        return 2
    folder = os.path.abspath(args[-1])
    data = os.path.join(folder, "data")
    if not os.path.isdir(data) or os.path.islink(data):
        return 2
    os.environ["LIFE_FOLDER"] = data
    parser = report_parser()
    command = args[0]
    if command == "needs":
        letter = letter_needs(data)
        answer = {"items": letter, "letter": True} if letter is not None else {"items": needs_files(data, parser)}
    elif command == "standings":
        answer = standings(data, parser)
    elif command == "scheduled":
        answer = scheduled(data)
    else:
        answer = project(data, parser, args[1]) if command == "project" else today(data, parser)
    if with_projection and command in ("needs", "today"):
        work = work_pieces(command)
        if command == "needs":
            answer["items"] += [{"text": row["label"] + ": " + row["title"], "kind": "work",
                                 "work_id": row["id"], "state": row["state"]} for row in work]
        else:
            answer["work"] = work
    answer["kind"] = command
    # The project list and the scheduled list are whole pages: room for a hub with many open briefs.
    sys.stdout.buffer.write(bounded(answer, 4 * MAX_OUTPUT if command in ("standings", "scheduled") else MAX_OUTPUT) + b"\n")
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
