#!/usr/bin/env python3
"""QR7 Data Operations — run when daemons are stopped (pulse swarm stop).

Operations:
1. Episodic FTS backfill (195 entries)
2. Intents garbage purge (<40 chars + CONVERSATIONAL)
3. Anti-pattern garbage purge (low confidence CONVERSATIONAL/OBSERVED)
4. JSONL orphan backfill to SQLite
"""
import sqlite3
import json
import os
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
DB = ROOT / "Hippocampus" / "pulse_brain.db"


def main():
    conn = sqlite3.connect(str(DB), timeout=10)
    conn.execute("PRAGMA busy_timeout=10000;")
    conn.execute("PRAGMA journal_mode=WAL;")

    # 1. Episodic FTS backfill
    episodes = conn.execute("""
        SELECT id, summary, domain, agent FROM episodic
        WHERE id NOT IN (SELECT item_id FROM knowledge_fts WHERE table_name = 'episodic')
    """).fetchall()
    for ep in episodes:
        conn.execute(
            "INSERT OR IGNORE INTO knowledge_fts "
            "(item_id, content, title, domain, agent, table_name) "
            "VALUES (?, ?, ?, ?, ?, ?)",
            (ep[0], ep[1] or "", f"[EPISODE] {(ep[1] or '')[:60]}",
             ep[2] or "general", ep[3] or "unknown", "episodic"),
        )
    conn.commit()
    print(f"1. Episodic FTS: {len(episodes)} indexed")

    # 2. Intents purge
    b = conn.execute("SELECT COUNT(*) FROM intents").fetchone()[0]
    conn.execute("DELETE FROM intents WHERE LENGTH(content) < 40 AND source_type = 'CONVERSATIONAL'")
    conn.commit()
    a = conn.execute("SELECT COUNT(*) FROM intents").fetchone()[0]
    print(f"2. Intents: {b} -> {a} (purged {b - a})")

    # 3. Anti-pattern purge
    b2 = conn.execute("SELECT COUNT(*) FROM anti_patterns").fetchone()[0]
    conn.execute(
        "DELETE FROM anti_patterns WHERE source_type IN ('CONVERSATIONAL','OBSERVED') AND confidence < 0.60"
    )
    conn.commit()
    a2 = conn.execute("SELECT COUNT(*) FROM anti_patterns").fetchone()[0]
    print(f"3. Anti-patterns: {b2} -> {a2} (purged {b2 - a2})")

    # 4. JSONL orphan backfill
    jsonl_map = {
        ROOT / "Hippocampus/Lessons/auto_lessons.jsonl": "lessons",
        ROOT / "Hippocampus/Failures/auto_fails.jsonl": "failures",
        ROOT / "Hippocampus/Patterns/auto_patterns.jsonl": "patterns",
    }
    total_backfilled = 0
    for jpath, table in jsonl_map.items():
        if not jpath.exists():
            continue
        entries = []
        with open(jpath, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                try:
                    entries.append(json.loads(line))
                except json.JSONDecodeError:
                    continue
        missing = 0
        for entry in entries:
            fp = entry.get("fingerprint", "")
            if not fp:
                continue
            exists = conn.execute(
                f"SELECT COUNT(*) FROM {table} WHERE fingerprint = ?", (fp,)
            ).fetchone()[0]
            if not exists:
                content = entry.get("content", "")
                if not content or len(content) < 10:
                    continue
                try:
                    conn.execute(
                        f"INSERT OR IGNORE INTO {table} "
                        "(id, content, fingerprint, confidence, salience, source_type, "
                        "domain, agent, timestamp, tags, cross_refs, provenance, metadata, title) "
                        "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                        (
                            entry.get("id", f"BF-{fp[:8]}"),
                            content, fp,
                            float(entry.get("confidence", 0.5)),
                            float(entry.get("salience", 1.0)),
                            entry.get("source_type", "CONVERSATIONAL"),
                            entry.get("domain", "general"),
                            entry.get("agent", "unknown"),
                            entry.get("timestamp", ""),
                            json.dumps(entry.get("tags", [])),
                            json.dumps(entry.get("cross_refs", [])),
                            json.dumps(entry.get("_provenance", entry.get("provenance", {}))),
                            json.dumps(entry.get("metadata", {})),
                            entry.get("title", "")[:80],
                        ),
                    )
                    missing += 1
                except Exception:
                    pass
        conn.commit()
        if missing:
            print(f"4. {jpath.name}: {missing} backfilled")
            total_backfilled += missing

    if total_backfilled == 0:
        print("4. JSONL backfill: no orphans found")

    conn.close()
    print("\nAll QR7 data operations complete.")


if __name__ == "__main__":
    main()
