#!/usr/bin/env python3
"""Standings: one table over every project's standing brief.

    bin/standings            read every repo Argoni sees

The repos are found fresh on every run (lifecfg.repos()): every git repo
inside each folder in config.json's `repos_folders`, down to `repos_depth`
folders (3 unless set; a repo directly inside is one down), plus each entry
in `repos`. A repo is not looked inside; hidden folders, links, node_modules,
build output and the Argoni folder itself are skipped. A new repo in a listed
folder shows up with no edit; a folder taken off the list is no longer read.
A folder that is missing or cannot be read gives one line and is skipped.

For each repo the table shows: whether `docs/standing.md` exists and its
`updated:` date, how old that is in days, the newest decision's date, the
number of open briefs (a brief with no report at the same number), the next
brief number, the last commit date, and how many files are uncommitted.

Decisions are looked for in `docs/decisions/` first, then `decisions/` at the
root (the life folder's own layout). Briefs and reports pair by the number in front of
the filename, the way formwork's `work-paired` reads them.

Everything here is read-only: `git log` and `git status`, nothing that writes.

Exit status:
    0   every standing brief is fresh and ahead of its decisions
    1   at least one is older than 14 days, or older than its newest
        decision, or no repo was found; the reason is printed
    A folder skipped (missing, unreadable) is a line, not a failure.
"""
import datetime
import os
import re
import sys

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

LIFE = lifecfg.root()
NOW = os.path.join(LIFE, "now.md")
STALE_DAYS = 14
NOW_MAX_LINES = lifecfg.config(LIFE)["now_max_lines"]

UPDATED = re.compile(r"^\s*updated\s*:\s*[\"']?(\d{4})-(\d{2})-(\d{2})", re.M | re.I)
DATE = re.compile(r"^\s*(?:updated|date)\s*:\s*[\"']?(\d{4})-(\d{2})-(\d{2})", re.M | re.I)
NUMBERED = re.compile(r"^(\d{1,6})[-_]")
FENCE = re.compile(r"^[ \t]*(?:```|~~~).*?^[ \t]*(?:```|~~~)[ \t]*$", re.M | re.S)

TODAY = datetime.date.today()


def read(path):
    with open(path, encoding="utf-8", errors="replace") as fh:
        return fh.read()


def as_date(m):
    try:
        return datetime.date(int(m.group(1)), int(m.group(2)), int(m.group(3)))
    except ValueError:
        return None


def frontmatter_date(text, pattern):
    m = pattern.search(FENCE.sub("", text))
    return as_date(m) if m else None




UNSAFE = {}  # repo: why git was not run there, for one line in the table's notes


def git(repo, *args):
    code, out, err = lifecfg.git(repo, *args, timeout=30)
    if code == 3:
        UNSAFE[repo] = err
    return out if code == 0 else None


def newest_decision(repo):
    """(date, folder used) for the newest dated decision file, or (None, None)."""
    for sub in (os.path.join("docs", "decisions"), "decisions"):
        d = os.path.join(repo, sub)
        if not os.path.isdir(d):
            continue
        newest, seen = None, 0
        for base, dirs, files in os.walk(d):
            dirs.sort()
            for fn in files:
                if not fn.endswith(".md") or fn.upper().startswith("README"):
                    continue
                seen += 1
                when = frontmatter_date(read(os.path.join(base, fn)), DATE)
                if when and (newest is None or when > newest):
                    newest = when
        return newest, (sub if seen else None)
    return None, None


def numbered(repo, sub):
    """{number: filename} for docs/<sub>/NNNN-*.md."""
    d = os.path.join(repo, "docs", sub)
    out = {}
    if not os.path.isdir(d):
        return out
    for fn in sorted(os.listdir(d)):
        m = NUMBERED.match(fn)
        if fn.endswith(".md") and m:
            out[int(m.group(1))] = fn
    return out


CLOSED = re.compile(r"^---\n(?:.*\n)*?status\s*:\s*[\"']?\W*(done|dropped|parked|superseded|closed|cancell?ed|"
                    r"abandoned|rejected|withdrawn|retired|archived|merged|replaced|reported|complete|completed|finished)\b",
                    re.I)


def closed(path):
    try:
        head = read(path)[:4096].replace("\r\n", "\n")
    except OSError:
        return False
    m = CLOSED.match(head)
    return bool(m) and "\n---" not in head[3:m.start(1)]


def briefs(repo):
    """(open count, next number as text). Open means no report at that number and a status
    that does not say it is finished or given up (0477: dropped briefs were counted open)."""
    b = numbered(repo, "briefs")
    r = numbered(repo, "reports")
    open_count = sum(1 for n, fn in b.items() if n not in r and not closed(os.path.join(repo, "docs", "briefs", fn)))
    all_numbers = set(b) | set(r)
    if not all_numbers:
        return open_count, "0001"
    width = max(len(NUMBERED.match(fn).group(1)) for fn in list(b.values()) + list(r.values()))
    return open_count, str(max(all_numbers) + 1).zfill(width)


def local_row(repo):
    name = os.path.basename(repo.rstrip("/"))
    row = {"repo": name, "standing": "-", "age": "-", "decision": "-",
           "open": "-", "next": "-", "commit": "-", "dirty": "-"}
    problems = []
    if not os.path.isdir(repo):
        row["standing"] = "missing dir"
        problems.append("%s: %s is not a directory" % (name, repo))
        return row, problems

    updated = None
    standing = os.path.join(repo, "docs", "standing.md")
    if os.path.isfile(standing):
        updated = frontmatter_date(read(standing), UPDATED)
        if updated:
            age = (TODAY - updated).days
            row["standing"] = str(updated)
            row["age"] = "%dd" % age
            if age > STALE_DAYS:
                problems.append("%s: docs/standing.md is %d days old (limit %d)"
                                % (name, age, STALE_DAYS))
        else:
            row["standing"] = "no date"
            problems.append("%s: docs/standing.md has no 'updated:' line" % name)
    else:
        row["standing"] = "none"

    when, sub = newest_decision(repo)
    if when:
        row["decision"] = str(when) + ("" if sub.startswith("docs") else "*")
        if updated and when > updated:
            problems.append("%s: newest decision (%s) is newer than standing.md (%s)"
                            % (name, when, updated))
    elif sub:
        row["decision"] = "undated"

    if os.path.isdir(os.path.join(repo, "docs", "briefs")):
        n_open, nxt = briefs(repo)
        row["open"] = str(n_open)
        row["next"] = nxt

    out = git(repo, "log", "-1", "--format=%cs")
    row["commit"] = out.strip() if out else "no git"
    out = git(repo, "status", "--porcelain", "--ignore-submodules=all")
    if out is not None:
        row["dirty"] = str(len([l for l in out.splitlines() if l.strip()]))
    return row, problems


def table(rows):
    cols = [("repo", "repo"), ("standing", "standing"), ("age", "age"),
            ("decision", "newest decision"), ("open", "open"), ("next", "next"),
            ("commit", "last commit"), ("dirty", "dirty")]
    width = {k: max(len(h), *(len(r[k]) for r in rows)) for k, h in cols}
    line = "  ".join(h.ljust(width[k]) for k, h in cols)
    print(line.rstrip())
    print("  ".join("-" * width[k] for k, _ in cols))
    for r in rows:
        print("  ".join(r[k].ljust(width[k]) for k, _ in cols).rstrip())


def main(argv):
    repos, skipped = lifecfg.repos(LIFE)
    for line in skipped:
        print(line)
    if skipped == [lifecfg.NONE_CHOSEN]:
        return 0  # nothing chosen is not a problem; the line says so
    if not repos:
        print("no repos found in the folders Argoni sees (config.json repos_folders, repos), nothing to read")
        return 1
    if skipped:
        print()
    rows, problems = [], []
    for path in repos:
        row, p = local_row(path)
        rows.append(row)
        problems.extend(p)

    table(rows)
    if any(r["decision"].endswith("*") for r in rows):
        print("\n* decisions read from <repo>/decisions/, not docs/decisions/")
    for repo, why in sorted(UNSAFE.items()):
        print("\n%s: %s" % (os.path.basename(repo.rstrip("/")), why))

    if os.path.isfile(NOW):
        n = len(read(NOW).splitlines())
        flag = "  WARNING: over %d lines, the limit in config.json now_max_lines" % NOW_MAX_LINES if n > NOW_MAX_LINES else ""
        print("\nnow.md: %d lines%s" % (n, flag))
    else:
        print("\nnow.md: not written yet")

    if problems:
        print("\n%d problem(s):" % len(problems))
        for p in problems:
            print("  " + p)
        return 1
    print("\nevery standing brief read is fresh and ahead of its decisions")
    return 0


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