"""Migration 010: Expand knowledge categories and domains.

Adds new categories for dual-use (personal + solo developer/creative):
    meeting_intel     — 14 days (meetings, commitments, action items)
    runbook           — 14 days (operational procedures, recurring tasks)
    design_decision   — 30 days (UI/UX, brand, aesthetic direction)
    business_decision — 60 days (pricing, positioning, launch timing)
    product_insight   — 60 days (user feedback, market signals, competitive intel)
    preference        — 90 days (working style, tool preferences)

Adds new domains:
    dashboard, mcp, devops, business, product, finance, productivity, music

Reclassifies existing entries with the expanded category set.
"""
from . import register


@register(10, "Expand knowledge categories + domains for dual-use system")
def migrate(db) -> None:
    """Reclassify existing entries with expanded category set."""
    # Reclassify all active entries using the updated categorizer
    try:
        from greymatter_core.knowledge_categorizer import classify_entry

        entries = db.execute(
            "SELECT id, entry_type, domain, title, content "
            "FROM knowledge_entries WHERE pruned_at IS NULL"
        )

        reclassified = 0
        for entry in entries:
            category, ttl = classify_entry(entry)
            old_cat = entry.get("knowledge_category", "domain_expertise")
            if category != old_cat:
                db.execute(
                    "UPDATE knowledge_entries "
                    "SET knowledge_category = ?, ttl_days = ? "
                    "WHERE id = ?",
                    (category, ttl, entry["id"]),
                )
                reclassified += 1
    except Exception:
        pass

    # Update schema metadata with full category list
    all_categories = (
        "infrastructure,meeting_intel,architecture,configuration,runbook,"
        "design_decision,correction,process,business_decision,"
        "product_insight,pattern,preference,domain_expertise"
    )
    db.execute(
        "INSERT INTO _schema_metadata (key, value) VALUES (?, ?) "
        "ON CONFLICT(key) DO UPDATE SET value = ?, updated_at = datetime('now')",
        ("knowledge_categories", all_categories, all_categories),
    )
