#!/usr/bin/env python3
# Copyright (c) 2026 PULSE Contributors. Proprietary. All rights reserved.
# See LICENSE file for details.
"""
Brain Recommender: Synthesize search results into actionable recommendation (V43.12)

Takes brain search results and produces ranked recommendation with:
- Approach suggestion based on wins
- Warnings from anti-patterns and fails
- Confidence score from evidence depth
- Contradiction alerts

Usage:
    python .claude/automation/brain_recommender.py "want description"
    python .claude/automation/brain_recommender.py "want" --json
Dependencies: Python stdlib only (Law 4)
"""

import json
import sys
from datetime import datetime, timezone
from pathlib import Path

# Add Utilities to path for brain_utils

from pulse.core.brain_utils import load_config, load_json_dict, text_similarity, WANTS_FILE
from pulse.brain.brain_search import brain_search


def extract_warnings(search_results: dict) -> list:
    """Extract warnings from anti-patterns and fails."""
    warnings = []
    for hit in search_results.get("results", {}).get("anti_patterns", []):
        if hit.get("salience", 0) > 0.15:
            warnings.append({
                "type": "anti_pattern",
                "title": hit.get("title", "Unknown"),
                "text": hit.get("text", "")[:150],
                "salience": hit.get("salience", 0),
                "severity": "high"
            })
    for hit in search_results.get("results", {}).get("fails", []):
        if hit.get("salience", 0) > 0.15:
            warnings.append({
                "type": "past_failure",
                "title": hit.get("title", "Unknown"),
                "text": hit.get("text", "")[:150],
                "salience": hit.get("salience", 0),
                "severity": "medium"
            })
    return sorted(warnings, key=lambda x: -x["salience"])


def extract_approaches(search_results: dict) -> list:
    """Extract proven approaches from wins and evidence."""
    approaches = []
    for hit in search_results.get("results", {}).get("wins", []):
        if hit.get("salience", 0) > 0.1:
            approaches.append({
                "type": "proven_win",
                "title": hit.get("title", "Unknown"),
                "text": hit.get("text", "")[:200],
                "salience": hit.get("salience", 0),
                "confidence": 0.8
            })
    for hit in search_results.get("results", {}).get("evidence", []):
        if hit.get("salience", 0) > 0.15 and hit.get("type") == "win":
            approaches.append({
                "type": "evidence_win",
                "text": hit.get("text", "")[:200],
                "salience": hit.get("salience", 0),
                "confidence": hit.get("confidence", 0.5)
            })
    return sorted(approaches, key=lambda x: -x["salience"])


def extract_context(search_results: dict) -> list:
    """Extract domain context for enrichment."""
    contexts = []
    for hit in search_results.get("results", {}).get("domains", []):
        if hit.get("salience", 0) > 0.1:
            contexts.append({
                "file": hit.get("file", ""),
                "text": hit.get("text", "")[:200],
                "salience": hit.get("salience", 0)
            })
    return contexts


def detect_contradictions(warnings: list, approaches: list) -> list:
    """Find contradictions between recommended approaches and warnings."""
    contradictions = []
    for w in warnings:
        for a in approaches:
            sim = text_similarity(w.get("text", ""), a.get("text", ""))
            if sim > 0.3:
                contradictions.append({
                    "warning": w.get("title", w.get("text", ""))[:80],
                    "approach": a.get("title", a.get("text", ""))[:80],
                    "similarity": round(sim, 3),
                    "resolution": "Review carefully - past failure overlaps with suggested approach"
                })
    return contradictions


def compute_confidence(approaches: list, warnings: list, evidence_count: int) -> float:
    """Compute overall recommendation confidence (0.0-1.0)."""
    base = 0.5
    if approaches:
        avg_salience = sum(a["salience"] for a in approaches) / len(approaches)
        base += avg_salience * 0.3
    if warnings:
        penalty = min(len(warnings) * 0.1, 0.3)
        base -= penalty
    if evidence_count > 5:
        base += 0.1
    return round(max(0.1, min(1.0, base)), 2)

from .brain_recommender_main import generate_recommendation, main