#!/usr/bin/env python3
import argparse
import json
import os
import subprocess
import urllib.request
from datetime import datetime, timezone

NOTION_VERSION = "2025-09-03"
MC_DB = os.environ.get("MEMORY_CANDIDATES_DB_ID", "d40769f3-b824-432b-a8aa-0ee8a5d00dde")
MC_DS = os.environ.get("MEMORY_CANDIDATES_DS_ID", "c9d26639-dde4-41cd-8bc8-1a5623f6c4cf")


def run(cmd):
    return subprocess.check_output(cmd, text=True)


def notion_request(method, path, payload=None):
    key = open(os.path.expanduser("~/.config/notion/api_key"), "r", encoding="utf-8").read().strip()
    data = json.dumps(payload).encode("utf-8") if payload is not None else None
    req = urllib.request.Request(
        f"https://api.notion.com/v1{path}",
        data=data,
        method=method,
        headers={
            "Authorization": f"Bearer {key}",
            "Notion-Version": NOTION_VERSION,
            "Content-Type": "application/json",
        },
    )
    with urllib.request.urlopen(req, timeout=45) as r:
        return json.loads(r.read().decode("utf-8"))


def list_bee_facts(max_facts=500):
    facts, cursor = [], None
    while len(facts) < max_facts:
        cmd = ["bee", "facts", "list", "--limit", "100", "--json"]
        if cursor:
            cmd += ["--cursor", cursor]
        page = json.loads(run(cmd))
        batch = page.get("facts", [])
        facts.extend(batch)
        cursor = page.get("next_cursor")
        if not cursor or not batch:
            break
    return facts[:max_facts]


def classify(text: str):
    t = (text or "").lower()
    if any(k in t for k in ["prefers", "values", "believes", "philosoph", "leadership", "coaches", "communication style", "habit", "routine"]):
        return "Identity", "High"
    if any(k in t for k in ["diagnos", "troubleshoot", "setup", "laser", "pressure washer", "construction", "logistics", "technical", "workflow", "process"]):
        return "Capability", "High"
    if any(k in t for k in ["works at", "manages", "coordinates", "contractor", "client", "project"]):
        return "Role", "Medium"
    if any(k in t for k in ["today", "yesterday", "this meeting", "this morning", "tonight"]):
        return "Situational", "Low"
    return "Operational Pattern", "Medium"


def existing_by_dedupe():
    by_key = {}
    cursor = None
    while True:
        body = {"page_size": 100}
        if cursor:
            body["start_cursor"] = cursor
        page = notion_request("POST", f"/data_sources/{MC_DS}/query", body)
        for row in page.get("results", []):
            props = row.get("properties", {})
            key = "".join(x.get("plain_text", "") for x in props.get("Dedupe Key", {}).get("rich_text", [])).strip()
            if key:
                by_key[key] = row
        if not page.get("has_more"):
            break
        cursor = page.get("next_cursor")
    return by_key


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--max-facts", type=int, default=500)
    ap.add_argument("--apply", action="store_true")
    args = ap.parse_args()

    facts = list_bee_facts(args.max_facts)
    existing = existing_by_dedupe()

    created = updated = 0
    classes = {}

    for f in facts:
        fid = f.get("id")
        text = (f.get("text") or "").strip()
        if not text:
            continue
        dedupe = f"bee_fact:{fid}"
        cls, relevance = classify(text)
        classes[cls] = classes.get(cls, 0) + 1
        confirmed = bool(f.get("confirmed"))
        status = "Approved" if confirmed else "Candidate"
        confidence = "High" if confirmed else "Medium"

        note_line = f"BeeClass={cls}; CareerRelevance={relevance}; BeeConfirmed={str(confirmed).lower()}; bee_fact_id={fid}; tags={','.join(f.get('tags') or [])}"

        props = {
            "Name": {"title": [{"type": "text", "text": {"content": text[:1900]}}]},
            "Type": {"select": {"name": "Fact"}},
            "Status": {"select": {"name": status}},
            "Confidence": {"select": {"name": confidence}},
            "Source Excerpt": {"rich_text": [{"type": "text", "text": {"content": text[:1900]}}]},
            "Dedupe Key": {"rich_text": [{"type": "text", "text": {"content": dedupe}}]},
            "Notes": {"rich_text": [{"type": "text", "text": {"content": note_line[:1800]}}]},
        }

        if dedupe in existing:
            if args.apply:
                notion_request("PATCH", f"/pages/{existing[dedupe]['id']}", {"properties": props})
            updated += 1
        else:
            if args.apply:
                notion_request("POST", "/pages", {"parent": {"database_id": MC_DB}, "properties": props})
            created += 1

    print(json.dumps({
        "apply": args.apply,
        "fetched": len(facts),
        "created": created,
        "updated": updated,
        "class_distribution": classes,
        "timestamp": datetime.now(timezone.utc).isoformat(),
    }, indent=2))


if __name__ == "__main__":
    main()
