#!/usr/bin/env python3
"""Reports: which reports wait for the user, and in what state (brief 0120). No model.

    bin/reports [--json] [--days N] [--chat DIR]

Lists every report Argoni may see, with its state, and writes the same list to
data/.summary/reports.json (a cache the app reads; never a project page, never a file in
a project). Nothing else is written anywhere.

Where: data/reports/*.md (the user's own), and docs/reports/*.md of every repo the user
chose (lifecfg.repos, the app's grants winning). Only files directly in that folder, named
NNNN[A-Z]?-*.md; a folder beside them (0215-files/) is not a report.
What counts: front matter (`key: value` lines between `---` lines at the top) with a
`brief:` key. Others are only counted (not_reports).
How read: never through a link, a plain file with one name, at most 256 KB (a bigger one
is listed as too-big and never opened).

States:
    accepted               it holds read-back lines (accepted: ... / in my words: ..., the
                           runner's own pattern, so an empty placeholder does not close a
                           report); for the user's own reports, also a filled ## Read-back
    writing                not accepted and changed in the last 10 minutes
    open                   waits for the read-back; only those changed in the last --days
                           (14) are listed one by one, older ones are counted per project
    needs-director         accepted by Argoni's read-back step (the record says so, with this
                           file's current hash), the repo has docs/standing.md, and that page
                           has not changed since: the project's director has not seen it
    changed-after-accepted the record has it, but the file's hash is not the recorded one
    too-big                over 256 KB, not opened
The record (accepted.jsonl in the runner's chat folder: ARGONI_CHAT or --chat) is the only
source for the last two. Without it they are never given, and the output says
"accepted_record": "not reached".

Report text is never printed or copied: a report is referred to by its path.
Standard library only (Python 3.9).
"""
import datetime
import hashlib
import json
import os
import re
import stat
import sys
import time

sys.dont_write_bytecode = True
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import lifecfg  # noqa: E402

LIFE = lifecfg.root()
MAX_BYTES = 256 * 1024
WRITING_SECONDS = 600
NAME = re.compile(r"(\d{4})([A-Z]?)-.*\.md")
# The runner's own pattern for a read-back line (READBACK_LINE in runner/argoni-runner).
READBACK_LINE = re.compile(r"^[ \t]*(accepted|in my words)[ \t]*:[ \t]*\S", re.I | re.M)


def front(text):
    """({key: value} of the front matter, the text after it)."""
    lines = text.split("\n")
    if not lines or lines[0].strip() != "---":
        return {}, text
    meta = {}
    for i in range(1, min(len(lines), 60)):
        if lines[i].strip() == "---":
            return meta, "\n".join(lines[i + 1:])
        m = re.match(r"([A-Za-z_]+):\s*(.*)", lines[i])
        if m:
            meta[m.group(1)] = m.group(2).strip().strip('"')
    return {}, text


# Control characters (C0, DEL, C1) and the bidi overrides: a title or a name must not move a
# terminal's cursor, colour its line, or flip the text around it.
CONTROL = re.compile("[\x00-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]")


def clean(s):
    """The text with every control character taken out (one space for a tab or newline)."""
    return CONTROL.sub(lambda m: " " if m.group(0) in "\t\n\r" else "", s)


def plain(s):
    """For the plain print: each control character as \\uXXXX, so a path stays whole and honest."""
    return CONTROL.sub(lambda m: "\\u%04x" % ord(m.group(0)), s)


def title_of(body):
    fence = False
    for line in body.split("\n"):
        if line.lstrip().startswith(("```", "~~~")):
            fence = not fence
        m = None if fence else re.match(r"# +(.*)", line)
        if m:
            return clean(m.group(1)).strip()
    return ""


def filled_readback(body):
    """A ## Read-back section that holds a read-back: the runner's own lines (an empty
    placeholder `accepted:` does not count), or the "Their words, DATE, from ..." block that
    bin/words writes in the user's own reports (the runner's pattern does not see that form)."""
    m = re.search(r"(?mi)^## read[- ]?back.*\n", body)
    if not m:
        return False
    nxt = re.search(r"(?m)^#{1,2} ", body[m.end():])
    section = body[m.end():m.end() + nxt.start()] if nxt else body[m.end():]
    return bool(READBACK_LINE.search(section) or re.search(r"(?m)^Their words\b", section))


def read_plain(dfd, name, cap):
    """(bytes, stat) of a plain file with one name below dfd, never through a link; (None,
    stat) when it is over cap; None when it is not such a file."""
    try:
        fd = os.open(name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=dfd)
    except OSError:
        return None
    with os.fdopen(fd, "rb") as fh:
        s = os.fstat(fh.fileno())
        if not stat.S_ISREG(s.st_mode) or s.st_nlink != 1:
            return None
        if s.st_size > cap:
            return None, s
        return fh.read(cap), s


def standing_info(root):
    """(exists, mtime, read_back_words or None) of the repo's docs/standing.md."""
    try:
        dfd, name = lifecfg.open_dir(os.path.join(root, "docs", "standing.md"), root)
    except OSError:
        return False, 0, None
    try:
        got = read_plain(dfd, name, 64 * 1024)
    finally:
        os.close(dfd)
    if not got or got[0] is None:
        return bool(got), got[1].st_mtime if got else 0, None
    meta, _ = front(got[0].decode("utf-8", "replace"))
    n = meta.get("read_back_words", "")
    return True, got[1].st_mtime, int(n) if re.fullmatch(r"[0-9]{1,4}", n) else None


def accepted_record(chat):
    """({real path: last record}, True) from accepted.jsonl, or ({}, False) when not reached."""
    path = chat or os.environ.get("ARGONI_CHAT") or ""
    if not path:
        return {}, False
    try:
        dfd = os.open(os.path.abspath(os.path.expanduser(path)), os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
    except OSError:
        return {}, False
    try:
        s = os.fstat(dfd)
        if s.st_uid != os.getuid() or s.st_mode & 0o077:
            return {}, False
        got = read_plain(dfd, "accepted.jsonl", 16 * 1024 * 1024)
        # No such file is a record with nothing accepted yet; a link, a second name, or a
        # file too big to read is a record that was not reached.
        if got is None and os.path.lexists(os.path.join(os.path.abspath(os.path.expanduser(path)), "accepted.jsonl")):
            return {}, False
        if got is not None and got[0] is None:
            return {}, False
    finally:
        os.close(dfd)
    out = {}
    for line in ((got[0] if got else None) or b"").decode("utf-8", "replace").splitlines():
        try:
            rec = json.loads(line)
        except ValueError:
            continue
        if isinstance(rec, dict) and isinstance(rec.get("path"), str) and isinstance(rec.get("sha256"), str):
            out[os.path.realpath(rec["path"])] = rec
    return out, True


def when(stamp):
    try:
        d = datetime.datetime.fromisoformat(str(stamp))
        return d.timestamp() if d.tzinfo else d.replace(tzinfo=datetime.timezone.utc).timestamp()
    except ValueError:
        return 0


def labels():
    """[(label, real folder, is the user's own)]: the life folder, then every chosen repo,
    named as memory names them (the folder's name; with its parent's when two share one)."""
    out = [(lifecfg.OWN, os.path.realpath(LIFE), True)]
    repos, _ = lifecfg.repos(LIFE)
    names = {}
    for r in repos:
        names.setdefault(os.path.basename(r.rstrip("/")), []).append(r)
    for name, paths in names.items():
        for r in paths:
            label = name if len(paths) == 1 else "%s/%s" % (os.path.basename(os.path.dirname(r)), name)
            out.append((clean(label)[:80], os.path.realpath(r), False))
    return out


def scan(label, root, own, record, have_record, days, now):
    """(items, not_reports, older_open, older_accepted) of one project."""
    sub = ["reports"] if own else ["docs", "reports"]
    try:
        dfd, _ = lifecfg.open_dir(os.path.join(root, *sub, "x"), root)
    except OSError:
        return [], 0, 0, 0
    items, not_reports, older_open, older_accepted = [], 0, 0, 0
    try:
        has_standing, standing_mtime, standing_words = (False, 0, None) if own else standing_info(root)
        for name in sorted(os.listdir(dfd)):
            m = NAME.fullmatch(name)
            if not m:
                continue
            try:
                ls = os.stat(name, dir_fd=dfd, follow_symlinks=False)
            except OSError:
                continue
            if not stat.S_ISREG(ls.st_mode):
                continue   # a link, a folder: never followed
            real = os.path.join(root, *sub, name)
            item = {"project": label, "path": real, "number": m.group(1) + m.group(2), "title": "", "brief": "",
                    "date": "", "state": "", "sha256": None, "mtime": ls.st_mtime, "words_needed": standing_words or 1}
            got = read_plain(dfd, name, MAX_BYTES)
            if got is None:
                continue
            raw, s = got
            if raw is None:
                item["state"] = "too-big"
                items.append(item)
                continue
            text = raw.decode("utf-8", "replace")
            meta, body = front(text)
            if "brief" not in meta:
                not_reports += 1
                continue
            # Copied into reports.json, which other programs read: short fixed shapes only.
            brief = meta["brief"] if re.fullmatch(r"[0-9]{1,4}[A-Z]?", meta["brief"]) else ""
            date = meta.get("date", "") if re.fullmatch(r"[0-9]{4}-[0-9]{2}-[0-9]{2}", meta.get("date", "")) else ""
            item.update(title=title_of(body)[:120], brief=brief, date=date,
                        sha256=hashlib.sha256(raw).hexdigest(), mtime=s.st_mtime)
            if re.fullmatch(r"[0-9]{1,4}", meta.get("read_back_words", "")):
                item["words_needed"] = max(1, int(meta["read_back_words"]))
            rec = record.get(os.path.realpath(real)) if have_record else None
            accepted = bool(READBACK_LINE.search(text)) or (own and filled_readback(body))
            age = now - s.st_mtime
            if rec and rec["sha256"] != item["sha256"]:
                item["state"] = "changed-after-accepted"
            elif rec and has_standing and standing_mtime <= when(rec.get("at")):
                item["state"] = "needs-director"
            elif accepted or rec:
                item["state"] = "accepted"
            elif age < WRITING_SECONDS:
                item["state"] = "writing"
            else:
                item["state"] = "open"
            if age > days * 86400 and item["state"] in ("open", "accepted"):
                if item["state"] == "open":
                    older_open += 1
                else:
                    older_accepted += 1
                continue
            items.append(item)
    finally:
        os.close(dfd)
    return items, not_reports, older_open, older_accepted


PHRASE = {"open": "open", "writing": "being written", "accepted": "accepted", "needs-director": "needs its director",
          "changed-after-accepted": "changed after accepted", "too-big": "too big to read"}


def main(argv):
    as_json, days, chat = False, 14, None
    args = list(argv)
    while args:
        a = args.pop(0)
        if a == "--json":
            as_json = True
        elif a in ("--days", "--chat") and args:
            v = args.pop(0)
            if a == "--chat":
                chat = v
            elif re.fullmatch(r"[0-9]{1,5}", v):
                days = int(v)
            else:
                print("reports: --days needs a number", file=sys.stderr)
                return 2
        else:
            print(__doc__.strip().split("\n\n")[0] + "\n\n    bin/reports [--json] [--days N] [--chat DIR]", file=sys.stderr)
            return 2
    record, have_record = accepted_record(chat)
    now = time.time()
    reports, older_open, older_accepted, not_reports = [], {}, {}, 0
    for label, root, own in labels():
        items, nr, oo, oa = scan(label, root, own, record, have_record, days, now)
        reports += items
        not_reports += nr
        if oo:
            older_open[label] = oo
        if oa:
            older_accepted[label] = oa
    res = {"reports": reports, "not_reports": not_reports, "older_open": older_open,
           "older_accepted": older_accepted, "days": days,
           "accepted_record": "read" if have_record else "not reached",
           "at": datetime.datetime.now().astimezone().isoformat(timespec="seconds")}
    try:
        os.makedirs(os.path.join(LIFE, ".summary"), exist_ok=True)
        lifecfg.write_atomic(os.path.join(LIFE, ".summary", "reports.json"),
                             json.dumps(res, ensure_ascii=False, indent=1) + "\n")
    except OSError as e:
        print("reports: the list was not saved: %s" % e, file=sys.stderr)
    if as_json:
        print(json.dumps(res, ensure_ascii=False, indent=1))
        return 0
    if not have_record:
        print("(the record of accepted read-backs was not reached: needs-director and changed-after-accepted are not given)")
    shown = False
    for label, _, _ in labels():
        mine = [r for r in reports if r["project"] == label]
        counts = {}
        for r in mine:
            counts[r["state"]] = counts.get(r["state"], 0) + 1
        if not mine and not older_open.get(label):
            continue
        shown = True
        bits = ["%d %s" % (counts[s], PHRASE[s]) for s in PHRASE if counts.get(s)]
        if older_open.get(label):
            bits.append("%d older open" % older_open[label])
        print("%s: %s" % (plain(label), ", ".join(bits)))
        for r in mine:
            print("  %s  %-22s %s  (%s)" % (r["number"], PHRASE[r["state"]], r["title"] or r["brief"], plain(r["path"])))
    if not shown:
        print("No reports found in your folder or the folders you chose.")
    return 0


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