#!/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_path = os.path.expanduser("~/.config/notion/api_key")
    key = open(key_path, "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 resp:
        return json.loads(resp.read().decode("utf-8"))


def existing_dedupe_keys():
    keys = set()
    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", {})
            txt = "".join(
                x.get("plain_text", "")
                for x in props.get("Dedupe Key", {}).get("rich_text", [])
            ).strip()
            if txt:
                keys.add(txt)
        if not page.get("has_more"):
            break
        cursor = page.get("next_cursor")
    return keys


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


def is_identity_fact(text: str) -> bool:
    t = (text or "").lower()
    # prioritize durable self-profile/personality/role preferences
    strong = [
        "braden ", "he prefers", "he values", "leadership", "coaches", "family", "children",
        "communication", "style", "approach", "habit", "routine", "medication", "health", "belief",
    ]
    return any(s in t for s in strong)


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

    dedupe = existing_dedupe_keys()
    facts = fetch_bee_facts(args.max_facts)

    created = skipped_existing = skipped_non_identity = 0
    for f in facts:
        fid = f.get("id")
        text = (f.get("text") or "").strip()
        if not text:
            skipped_non_identity += 1
            continue
        if not is_identity_fact(text):
            skipped_non_identity += 1
            continue

        key = f"bee_fact:{fid}"
        if key in dedupe:
            skipped_existing += 1
            continue

        confidence = "High" if f.get("confirmed") else "Medium"
        payload = {
            "parent": {"database_id": MC_DB},
            "properties": {
                "Name": {"title": [{"type": "text", "text": {"content": text[:1900]}}]},
                "Type": {"select": {"name": "Fact"}},
                "Status": {"select": {"name": "Candidate"}},
                "Confidence": {"select": {"name": confidence}},
                "Source Excerpt": {"rich_text": [{"type": "text", "text": {"content": text[:1900]}}]},
                "Dedupe Key": {"rich_text": [{"type": "text", "text": {"content": key}}]},
                "Notes": {
                    "rich_text": [
                        {
                            "type": "text",
                            "text": {
                                "content": f"Bee identity-fact sync. bee_fact_id={fid}; confirmed={bool(f.get('confirmed'))}; tags={','.join(f.get('tags') or [])}"
                            },
                        }
                    ]
                },
            },
        }
        if args.apply:
            notion_request("POST", "/pages", payload)
        created += 1
        dedupe.add(key)

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


if __name__ == "__main__":
    main()
