#!/usr/bin/env python3
"""Check: run formwork's standalone checks over the life folder, by path.

    bin/check                 check the life folder ($LIFE_FOLDER)
    bin/check --exclude DIR   leave DIR out (sets FORMWORK_EXCLUDE), repeatable;
                              research/, briefs/, reports/ and .claude/ are always left out
    bin/check <directory>     check another directory the same way

The engine has no formwork kit of its own. It borrows four of formwork's checks,
which are plain programs taking a directory and needing nothing else:

    standing-current   docs/standing.md is filled in and not behind its record
    decision-ids       decision numbers are unique, real, and cross-references hold
    work-paired        every brief has a report at the same number
    doc-links          every link between documents points at a file that exists

They live wherever config.json's `formwork_checks` points. Nothing is copied.
When it is not set, these four are skipped with one line saying so, and the
checks below still run.

One wrinkle, stated so nobody is surprised: those checks read `docs/decisions/`
and the life folder keeps `decisions/` at the root. `decision-ids` is therefore
run through a temporary folder that holds only a `docs/decisions` symlink back
to the life folder's `decisions/`. The folder lives in the system temp directory
and is removed when the check ends. `standing-current` has no such shim, so once
`docs/standing.md` exists it will NOT see the root `decisions/` when deciding
whether the brief is behind; `bin/standings` does that comparison instead,
and this script prints the caveat.

It also checks the length of `now.md` and fails above config.json's
`now_max_lines` (64 unless the user set another): past that the user stops
reading. A number is the only honest way to know.

It checks every line under the now.md needs heading (config.json
`needs_heading`). Each line has one shape, its source must exist, its check
must be recent, it must be an action, and it must not name a report
(read-backs are bin/foryou's report items). See `needs()` below and
conventions.md "Need lines".

Last, over the life folder only, it runs `bin/summary` and fails if that exits
non-zero: the views read summary.json, so a format change that breaks the one
reader is caught the same day.

Exit status:
    0   every check that ran passed
    1   at least one check failed; its own output says why
    2   a check is missing, or could not be run
"""
import datetime
import glob
import os
import re
import subprocess
import sys
import tempfile

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 lifecfg  # noqa: E402

CHECKS = ["standing-current", "decision-ids", "work-paired", "doc-links"]

# A need line:  N. <project>: <what the user does>. (from <owner file>; checked YYYY-MM-DD)
# <owner file> is a path in the life folder, or <repo>:<path> for a repo on this Mac.
NEED_LINE = re.compile(r"^(?:\d+\.|-) ([a-z0-9][a-z0-9-]*(?:#\d{4})?): (.+?) "
                       r"\(from ([^;()]+); checked (\d{4}-\d{2}-\d{2})\)$")
STATUS_WORDS = {"nothing", "none", "done", "running", "reported", "waiting", "still", "no"}


def run(formwork, check, target, env):
    path = os.path.join(formwork, check)
    p = subprocess.run([sys.executable, "-I", path, target], capture_output=True,
                       text=True, env=env)
    return p.returncode, (p.stdout + p.stderr).rstrip()


def source_exists(root, source, repos):
    """`projects/app.md` in the life folder, or `app:docs/decisions/0008-x.md` in a repo Argoni sees."""
    repo, sep, path = source.partition(":")
    if not sep:
        return os.path.isfile(os.path.join(root, source))
    return any(os.path.basename(d.rstrip("/")) == repo and os.path.isfile(os.path.join(d, path))
               for d in repos)


def report_ids(root):
    """Every report id in reports/ ("0033A", "0036")."""
    out = set()
    for p in glob.glob(os.path.join(root, "reports", "[0-9][0-9][0-9][0-9]*-*.md")):
        m = re.match(r"^(\d{4}[A-Z]?)-", os.path.basename(p))
        if m:
            out.add(m.group(1))
    return out


def needs(root, today=None):
    """Every list line under the now.md needs heading, Small and Decisions
    alike. Returns a list of failure messages, empty when all pass."""
    today = today or datetime.date.today()
    cfg = lifecfg.config(root)
    heading = "## " + cfg["needs_heading"]
    repos = lifecfg.repos(root)[0]
    max_age = cfg["need_max_age_days"]
    with open(os.path.join(root, "now.md"), encoding="utf-8", errors="replace") as fh:
        lines = fh.read().splitlines()
    reports, fails, inside = report_ids(root), [], False
    for n, line in enumerate(lines, start=1):
        if line.startswith("## "):
            inside = line.strip() == heading
            continue
        if not inside or not re.match(r"^(?:\d+\.|-) ", line):
            continue
        where = "now.md:%d" % n
        m = NEED_LINE.match(line)
        if not m:
            fails.append("%s: not the shape `N. <project>: <what the user does>. (from <owner file>; "
                         "checked YYYY-MM-DD)`: %s" % (where, line))
            continue
        _, what, source, checked = m.groups()
        if not source_exists(root, source.strip(), repos):
            fails.append("%s: owner file %s does not exist" % (where, source.strip()))
        try:
            age = (today - datetime.date.fromisoformat(checked)).days
        except ValueError:
            age = None
        if age is None or age < 0:
            fails.append("%s: checked %s is not a real past date" % (where, checked))
        elif age > max_age:
            fails.append("%s: checked %s, %d days ago (limit %d); re-read %s and move the date, "
                         "or remove the line" % (where, checked, age, max_age, source.strip()))
        words = re.findall(r"[a-z']+", what.lower())
        if words and (words[0] in STATUS_WORDS
                      or (len(words) <= 4 and STATUS_WORDS & set(words))):
            fails.append("%s: a status, not an action (\"%s\"); a status goes in "
                         "\"Where we are now\"" % (where, what))
        named = sorted(r for r in reports if re.search(r"(?<![\w#])(?<!decision )%s(?![\w])" % r, what))
        if named:
            fails.append("%s: names report %s; read-backs are the report items' job "
                         "(bin/foryou), never a now.md line" % (where, ", ".join(named)))
    return fails


# A domain holds at most `domain_max_notes` dated notes (YYYY-MM-DD-*.md, 12
# unless set); the weekly review rolls older ones into the README's "## Facts",
# which holds at most `domain_max_facts` lines (20 unless set). Health, money and
# family are the user's to keep or not (config.json `sensitive_domains`; with
# none set, kept on a personal Mac, not on a work computer or before the
# first-run answer). Not kept: no domain folder by those names, and no note in
# any domain tagged with one (`subject:` or `area:`). Kept on a work computer
# because the user switched them on: one reminder line, never a failure.
SENSITIVE = ("health", "money", "finance", "family")


def work_computer(root):
    """True unless config.json says `work_computer: false`."""
    return lifecfg.config(root).get("work_computer") is not False


def sensitive_line(root):
    """What the domains line says about health, money and family."""
    cfg = lifecfg.config(root)
    if not lifecfg.sensitive_allowed(cfg):
        return "health, money and family off (%s)" % (
            "your setting" if cfg.get("sensitive_domains") is False else
            "the default on a work computer, or not yet answered")
    if work_computer(root):
        return ("reminder: health, money and family are switched on, on a work computer (or one not yet "
                "answered); advised against, an employer can read or wipe a work Mac")
    return "health, money and family kept (a personal Mac)"


def domains(root):
    cfg = lifecfg.config(root)
    off = not lifecfg.sensitive_allowed(cfg)
    max_notes, max_facts = cfg["domain_max_notes"], cfg["domain_max_facts"]
    why = "switch them on in settings (sensitive_domains: true) or move it out"
    fails = ["domains/%s exists, but health, money and family are off here; %s" % (n, why)
             for n in SENSITIVE if off and os.path.exists(os.path.join(root, "domains", n))]
    for readme in sorted(glob.glob(os.path.join(root, "domains", "*", "README.md"))):
        folder = os.path.dirname(readme)
        name = os.path.basename(folder)
        with open(readme, encoding="utf-8", errors="replace") as fh:
            text = fh.read()
        # Files only: archive/ holds notes the weekly review rolled into Facts.
        others = [f for f in os.listdir(folder) if f != "README.md" and not f.startswith(".")
                  and os.path.isfile(os.path.join(folder, f))]
        for f in sorted(others):
            with open(os.path.join(folder, f), encoding="utf-8", errors="replace") as fh:
                head = fh.read(2000)
            tags = re.findall(r"^(?:subject|area): *(\S+)", head, re.M)
            if off and any(tag in SENSITIVE for tag in tags):
                fails.append("domains/%s/%s is tagged %s, but health, money and family are off here; %s"
                             % (name, f, "/".join(tags), why))
        notes = [f for f in others if re.match(r"^\d{4}-\d{2}-\d{2}-.+\.md$", f)]  # person pages are not notes
        if len(notes) > max_notes:
            fails.append("domains/%s holds %d notes (limit %d); roll the oldest into its README facts"
                         % (name, len(notes), max_notes))
        m = re.search(r"^## Facts\n(.*?)(?=^## |\Z)", text, re.M | re.S)
        facts = [l for l in (m.group(1).splitlines() if m else []) if l.startswith("- ")]
        if len(facts) > max_facts:
            fails.append("domains/%s README holds %d facts (limit %d); merge or drop"
                         % (name, len(facts), max_facts))
    return fails


def main(argv):
    # research/ quotes paths in other repos, so doc-links is not run over it.
    # .summary/ is generated and gitignored. briefs/ and reports/ talk about other repos by their own
    # paths, which doc-links would read as broken links in this one.
    # .claude/ holds Claude Code's settings and a session's worktrees, a whole second
    # copy of the folder whose links would fail here.
    args, excludes = [], []
    it = iter(argv[1:])
    for a in it:
        if a == "--exclude":
            excludes.append(next(it, ""))
        elif a.startswith("--"):
            print("usage: bin/check [--exclude DIR]... [<directory>]", file=sys.stderr)
            return 2
        else:
            args.append(a)
    life = lifecfg.root() if not args else None
    root = os.path.abspath(args[0]) if args else life
    # Relative to the folder checked, so a copy leaves out its own reports/.
    # restored/, .proposals/ and .archive-in/ hold what the runner moved aside or keeps for the
    # user's click (brief 0117): copies, not notes, so nothing in them is checked or fails.
    excludes = [os.path.join(root, d) for d in ("research", "briefs", "reports", ".claude")
                + lifecfg.NOT_NOTES] + excludes
    cfg = lifecfg.config(root)

    env = dict(os.environ)
    if excludes:
        env["FORMWORK_EXCLUDE"] = os.pathsep.join(excludes)
        print("excluding: %s" % ", ".join(excludes))

    failed = 0
    formwork = lifecfg.expand(cfg["formwork_checks"]) if cfg["formwork_checks"] else None
    if not formwork:
        print("formwork checks: skipped, config.json has no formwork_checks\n")
    else:
        missing = [c for c in CHECKS if not os.path.isfile(os.path.join(formwork, c))]
        if missing:
            print("ERROR: formwork checks not found under %s: %s" % (formwork, ", ".join(missing)))
            return 2

        root_decisions = os.path.join(root, "decisions")
        docs_decisions = os.path.join(root, "docs", "decisions")
        shim = os.path.isdir(root_decisions) and not os.path.isdir(docs_decisions)

        for check in CHECKS:
            print("== %s" % check)
            if check == "decision-ids" and shim:
                with tempfile.TemporaryDirectory(prefix="life-check-") as tmp:
                    os.makedirs(os.path.join(tmp, "docs"))
                    os.symlink(root_decisions, os.path.join(tmp, "docs", "decisions"))
                    print("(reading %s through a temporary docs/decisions shim)"
                          % os.path.relpath(root_decisions, root))
                    code, out = run(formwork, check, tmp, env)
            else:
                code, out = run(formwork, check, root, env)
            print(out)
            if check == "standing-current" and shim and os.path.isfile(os.path.join(root, "docs", "standing.md")):
                print("caveat: this check compares against docs/decisions only; the root "
                      "decisions/ folder is invisible to it. bin/standings does that comparison.")
            if code == 2:
                print("check could not run (exit 2)")
                return 2
            if code != 0:
                failed += 1
            print()

    now = os.path.join(root, "now.md")
    if os.path.isfile(now):
        with open(now, encoding="utf-8", errors="replace") as fh:
            n = len(fh.read().splitlines())
        print("now.md: %d lines (limit %d)" % (n, cfg["now_max_lines"]))
        if n > cfg["now_max_lines"]:
            print("now.md: FAIL, over the limit; cut before you add")
            failed += 1
        fails = needs(root)
        for f in fails:
            print("needs: FAIL, " + f)
        if fails:
            failed += 1
        else:
            print("needs: every line under \"%s\" has its shape, source and date" % cfg["needs_heading"])
    else:
        print("now.md: not written yet")

    if os.path.isdir(os.path.join(root, "domains")):
        fails = domains(root)
        for f in fails:
            print("domains: FAIL, " + f)
        if fails:
            failed += 1
        else:
            print("domains: within %d notes and %d facts each; %s"
                  % (cfg["domain_max_notes"], cfg["domain_max_facts"], sensitive_line(root)))
    else:
        print("domains: " + sensitive_line(root))

    if root == life:
        print("\n== summary")
        p = subprocess.run([sys.executable, "-I", os.path.join(BIN, "summary"), "--no-words"],
                           capture_output=True, text=True, env=dict(os.environ, LIFE_FOLDER=root))
        print((p.stdout + p.stderr).rstrip())
        if p.returncode != 0:
            print("summary: FAIL, bin/summary could not read the markdown")
            failed += 1

    if failed:
        print("\n%d check(s) failed" % failed)
        return 1
    print("\nall checks passed")
    return 0


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