#!/usr/bin/env python3
import csv
import datetime
import fcntl
import pathlib
import sys

root = pathlib.Path(sys.argv[1])
registry_path = pathlib.Path(sys.argv[2])
lock_path = pathlib.Path(sys.argv[3])
input_file = pathlib.Path(sys.argv[4])
sites_inline = sys.argv[5]

now = datetime.datetime.now().isoformat(timespec="seconds")
skipped = []


def load_rows():
    rows = {}
    if registry_path.exists():
        for line in registry_path.read_text().splitlines():
            if line.startswith("|") and "Domain" not in line and "--------" not in line:
                cols = [c.strip() for c in line.strip("|").split("|")]
                if len(cols) >= 6:
                    domain, title, desc, status, batch, updated = cols[:6]
                    rows[domain] = [domain, title, desc, status or "-", batch or "-", updated]
    return rows


def parse_input():
    entries = []
    if input_file.exists():
        with input_file.open() as fh:
            reader = csv.reader(fh)
            for idx, row in enumerate(reader, start=1):
                if not row or not (row[0] or "").strip():
                    skipped.append(f"sites.csv line {idx}: blank or missing domain")
                    continue
                domain = row[0].strip()
                theme = row[1].strip() if len(row) > 1 else ""
                title = domain
                desc = theme
                entries.append((domain, title, desc))
    if sites_inline:
        for chunk in sites_inline.split(";"):
            parts = [p.strip() for p in chunk.split(",")]
            if not parts or not parts[0]:
                skipped.append(f"inline chunk '{chunk}': missing domain")
                continue
            domain = parts[0]
            title = domain if len(parts) < 2 else parts[1] or domain
            desc = parts[2] if len(parts) > 2 else (parts[1] if len(parts) > 1 else "")
            entries.append((domain, title, desc))
    return entries


def write_registry(rows):
    header = [
        "# SMaking Sites Registry (template)\n",
        "\n",
        "| Domain | Title | Description | Status | Batch | Updated |\n",
        "|--------|-------|-------------|--------|-------|---------|\n",
    ]
    body = [f"| {r[0]} | {r[1]} | {r[2]} | {r[3]} | {r[4]} | {r[5]} |\n" for r in sorted(rows.values(), key=lambda r: r[0])]
    footer = [
        "\n",
        "Status codes: none/- → B → d → D → O → i → I → Q\n",
        "\n",
        "Notes:\n",
        "- Status \"-\" means registered but not in design batch.\n",
        "- Design phases use B/d/D; implement phases use O/i/I/Q.\n",
        "- Update timestamps on each transition.\n",
    ]
    registry_path.write_text("".join(header + body + footer))


entries = parse_input()
if not entries:
    print("ERROR:no_sites:no sites provided to register")
    sys.exit(2)

lock_path.parent.mkdir(parents=True, exist_ok=True)
with lock_path.open("w") as lf:
    fcntl.flock(lf, fcntl.LOCK_EX)
    rows = load_rows()
    for domain, title, desc in entries:
        if domain in rows:
            # Preserve existing status/batch/updated to avoid overwriting completed work
            existing = rows[domain]
            new_title = title if existing[1] == "" else existing[1]
            new_desc = desc if existing[2] == "" else existing[2]
            rows[domain] = [domain, new_title, new_desc, existing[3], existing[4], existing[5]]
            skipped.append(f"{domain}: already registered; preserved status {existing[3]}")
            continue
        status = "-"
        batch = "-"
        rows[domain] = [domain, title, desc, status, batch, now]
    write_registry(rows)
    fcntl.flock(lf, fcntl.LOCK_UN)

for msg in skipped:
    print(f"WARN:skip:{msg}")
print(f"OK:registered:{len(entries)} sites ({len(skipped)} skipped)")
