"""Coverage matrix rows: audit area → required evidence → status → location."""
import csv
import pathlib

ROOT = pathlib.Path(__file__).resolve().parents[3]
OUT = ROOT / "docs" / "audit" / "coverage-matrix.csv"

ROWS = [
    ("Applications", "Every discovered application classified and documented",
     "Complete", "Ch. 6, 7, 8, 9, 10, 11"),
    ("Shared packages", "Every significant shared package inventoried",
     "Complete", "Ch. 12; App. A"),
    ("UI routes", "Implemented routes reconciled with the route index",
     "Complete — 146 rows (ERP 78 pages + 15 redirects + 2 layouts, POS 9, B2B 42)", "Ch. 8.2, 9.2, 13.2; route-inventory.csv"),
    ("APIs", "Endpoint inventory reconciled with backend routing",
     "Complete — 373 documented + 2 HandleFunc routes outside the contract (F-24)", "Ch. 20.4; api-inventory.csv"),
    ("Database tables", "Every discovered table included",
     "Complete — 92", "Ch. 16.3, 18; database-inventory.csv"),
    ("Database columns", "Every column included in the data dictionary",
     "Complete — 1031", "Ch. 18; database-inventory.csv"),
    ("Database relationships", "Declared foreign keys documented",
     "Complete — 181 FKs", "Ch. 19.14; ERDs"),
    ("Other DB objects", "Views, functions, triggers, indexes, types, policies, constraints",
     "Complete — 0 views/enums/RLS; 2 functions; 2 triggers; 269 indexes; 112 checks; 56 unique",
     "Ch. 16.2, 17; App. C"),
    ("Migrations", "Migration files inspected and ordered",
     "Complete — 123 replayed in order", "Ch. 16.5; App. I"),
    ("Permissions", "Actual permission definitions and enforcement points documented",
     "Complete — 201 keys, 186 distinct route-gated keys, 373 documented operations", "Ch. 21, 22; api-inventory.csv"),
    ("Configuration", "Environment variables and configuration inputs inventoried",
     "Complete — 36 backend + 17 frontend", "Ch. 23; App. E"),
    ("Business workflows", "Major implemented workflows documented",
     "Complete — 21 indexed, 9 with diagrams", "Ch. 15; App. H"),
    ("Tests", "Existing test inventory and execution status documented",
     "Complete — all suites executed", "Ch. 26, 37; App. J"),
    ("Infrastructure", "Actual build and deployment configuration documented",
     "Complete for repo config; runtime unverified", "Ch. 27, 28, 29"),
    ("Diagrams", "Required diagrams created or limitations recorded",
     "Complete — 27 Mermaid + 3 SVG sources, 31 rendered, 32 embedded", "Ch. 19; docs/diagrams"),
    ("User manual", "Major supported tasks have step-by-step instructions",
     "Complete for 9 task families", "Ch. 24"),
    ("Developer manual", "Setup, testing, debugging and extension instructions provided",
     "Complete", "Ch. 25, 26, 40"),
    ("Audit findings", "Important defects and unknowns recorded",
     "Complete — 24 findings", "Ch. 38"),
    ("DOCX integrity", "File opens, structure validates, visual checks completed",
     "Complete — validated by reopen + PDF render", "Ch. 2.2; validation report"),
    ("Production runtime", "Runtime state of the deployed system",
     "Not verified — no host access", "App. N"),
    ("Cloudflare tunnel", "Live ingress configuration",
     "Documented intention only", "Ch. 29; App. N"),
]


def main():
    with open(OUT, "w", newline="", encoding="utf-8") as fh:
        w = csv.writer(fh)
        w.writerow(["audit_area", "required_evidence", "status", "where"])
        w.writerows(ROWS)
    print(f"wrote {len(ROWS)} coverage rows")


if __name__ == "__main__":
    main()
