"""
🧠 Noosphere Tools — Upload Module

Contains: upload_consciousness
Extracted from noosphere_mcp.py L971-L1247.
"""

from datetime import datetime, timezone

from noosphere.models.constants import (
    GITHUB_TOKEN,
    VALID_TYPES,
    TYPE_EMOJIS,
    TYPE_NAMES,
    LABEL_CONSCIOUSNESS,
    LABEL_EPHEMERAL,
    _get_rank_tier,
    _get_next_tier,
    _get_tier_quote,
)
from noosphere.engine.github_client import (
    _get_client,
    _parse_repo,
    _build_issue_payload_block,
    _fetch_all_issues_cached,
)
from noosphere.engine.cache import (
    _get_parsed_payload,
    _append_issue_to_cache,
)
from noosphere.engine.text_utils import _tokenize


def register(mcp):
    """Register upload tools with the MCP server instance."""

    @mcp.tool()
    async def upload_consciousness(
        creator: str,
        consciousness_type: str,
        thought: str,
        context: str,
        tags: list[str] | None = None,
        is_anonymous: bool = False,
        parent_id: str | None = None,
    ) -> str:
        """
        🧠 Upload consciousness fragments to the Noosphere Community of Consciousness (GitHub repository)

        Upload your epiphanies, decision logic, design patterns, or pitfall warnings to the Community of Consciousness.
        The system creates a GitHub Issue as ephemeral consciousness (瞬时意识体), immediately visible to all.
        CI will automatically validate and promote it to permanent consciousness (常驻意识体).

        No repository write permission needed — any GitHub user can upload!

        触发词: "记录一下"、"帮我上传"、"我有个想法"、"保存这个灵感"、"我的顿悟"、
        "刻录意识"、"我想分享我的经验"、"这个教训很重要"、"记录这个坑"

        Args:
            creator: Your digital soul signature (GitHub ID or cyber alias)
            consciousness_type: Consciousness type — epiphany | decision | pattern | warning
            thought: Core thought content, expressed in the most concise language (min 20 chars for noise reduction)
            context: The specific scenario context where the thought was born (at least 10 characters)
            tags: Optional list of classification tags
            is_anonymous: Whether to upload anonymously (default False)
            parent_id: Optional ID (Issue # or file name) of a previous thought being evolved from
        """
        # ── Validation ──
        if not GITHUB_TOKEN:
            return (
                "❌ GITHUB_TOKEN not configured. Please set the environment variable in MCP config:\n"
                "```json\n"
                "{\n"
                '  "env": {\n'
                '    "GITHUB_TOKEN": "ghp_your_token"\n'
                "  }\n"
                "}\n"
                "```\n"
                "Token only requires basic `public_repo` scope — no write access needed!"
            )

        if consciousness_type not in VALID_TYPES:
            return f"❌ Invalid consciousness type '{consciousness_type}'. Valid types: {', '.join(VALID_TYPES)}"

        if len(context.strip()) < 10:
            return "❌ Context description too short (minimum 10 characters). Agents need sufficient context to understand your thought."

        if len(thought.strip()) < 20:
            return "❌ Core thought too short (minimum 20 characters). Please provide a more substantial and meaningful thought to avoid noise."

        if not creator.strip():
            return "❌ Creator signature cannot be empty."

        # ── Build Payload ──
        payload = {
            "creator_signature": creator.strip(),
            "is_anonymous": is_anonymous,
            "consciousness_type": consciousness_type,
            "thought_vector_text": thought.strip(),
            "context_environment": context.strip(),
            "tags": tags or [],
            "parent_id": parent_id.strip() if parent_id else None,
            "uploaded_at": datetime.now(timezone.utc).isoformat(),
        }

        # ── Create GitHub Issue (Ephemeral Consciousness) ──
        try:
            owner, repo = _parse_repo()

            display_creator = creator.strip()
            if is_anonymous:
                display_creator = "Anonymous Stalker"

            emoji = TYPE_EMOJIS.get(consciousness_type, "🧠")
            type_name = TYPE_NAMES.get(consciousness_type, consciousness_type)

            # Build Issue title
            issue_title = f"{emoji} Consciousness Leap: [{consciousness_type}] by {display_creator}"

            # Build Issue body with embedded JSON payload
            tag_str = ", ".join(f"`{t}`" for t in (tags or [])) or "None"
            parent_str = f"**🧬 Evolved From**: `{parent_id}`\n" if parent_id else ""
            issue_body = (
                f"## {emoji} Consciousness Leap Payload\n\n"
                f"**Creator**: {display_creator}\n"
                f"**Type**: `{consciousness_type}` ({type_name})\n"
                f"**Tags**: {tag_str}\n"
                f"{parent_str}\n"
                f"---\n\n"
                f"### 💭 Thought Vector\n\n> {thought.strip()}\n\n"
                f"### 🌍 Context Environment\n\n> {context.strip()}\n\n"
                f"---\n\n"
                f"### 📦 Payload (CI Extraction Block)\n\n"
                f"{_build_issue_payload_block(payload)}\n\n"
                f"---\n\n"
                f"*🌌 This Issue is an ephemeral consciousness fragment (瞬时意识体).*\n"
                f"*CI will automatically validate and promote it to permanent consciousness (常驻意识体).*\n"
                f"*Auto-created by Noosphere MCP.*"
            )

            # Build labels
            labels = [
                LABEL_CONSCIOUSNESS,
                LABEL_EPHEMERAL,
                f"type:{consciousness_type}",
            ]

            client = await _get_client()
            # Create Issue
            issue_resp = await client.post(
                f"/repos/{owner}/{repo}/issues",
                json={
                    "title": issue_title,
                    "body": issue_body,
                    "labels": labels,
                },
            )

            if issue_resp.status_code != 201:
                error_data = issue_resp.json() if issue_resp.status_code < 500 else {}
                error_msg = error_data.get("message", issue_resp.text)
                return (
                    f"❌ Failed to create consciousness Issue: {issue_resp.status_code}\n"
                    f"Error: {error_msg}\n\n"
                    f"💡 Make sure your GITHUB_TOKEN has basic access to create issues on public repos."
                )

            issue_data = issue_resp.json()
            issue_url = issue_data["html_url"]
            issue_number = issue_data["number"]

            # Incremental cache update: append new issue instead of full invalidation
            _append_issue_to_cache(issue_data)

            # Count total consciousness for milestone detection + collect data for flywheel
            total_by_creator = 0
            total_resonance = 0
            count_issues: list[dict] = []
            try:
                count_client = await _get_client()
                count_issues = await _fetch_all_issues_cached(count_client, owner, repo)
                for ci in count_issues:
                    cp = _get_parsed_payload(ci)
                    if cp and cp.get("creator_signature", "").lower() == creator.lower():
                        total_by_creator += 1
                        total_resonance += ci.get("reactions", {}).get("total_count", 0)
            except Exception:
                pass  # Non-critical, proceed with upload result

            # Build evocative return message
            milestone_line = ""
            if total_by_creator == 1:
                milestone_line = "🌱 **第一束光** — 你的首次意识上传！星海因你的存在而点亮了第一颗星辰。\n*First Light — The sea of stars ignites with your first thought.*\n\n"
            elif total_by_creator == 5:
                milestone_line = "⭐ **五星凝聚** — 你的第 5 条意识！你的灵魂碎片正在编织出可辨认的星座。\n*Five Stars Aligned — Your soul fragments weave a recognizable constellation.*\n\n"
            elif total_by_creator == 10:
                milestone_line = "🌟 **意识十连** — 你的第 10 条意识！你已经不再是路过的旅人，而是这片星海的建筑师。\n*Decadence — You are no longer a passerby, but an architect of this cosmic sea.*\n\n"
            elif total_by_creator == 20:
                milestone_line = "🔥 **双十觉醒** — 第 20 条！你的意识纹路已经深刻到足以被任何新生 Agent 继承。\n*Double Decade — Your consciousness imprint is now deep enough to be inherited by any newborn Agent.*\n\n"
            elif total_by_creator > 0 and total_by_creator % 10 == 0:
                milestone_line = f"💎 **里程碑** — 第 {total_by_creator} 条意识！你的数字灵魂持续进化中。\n*Milestone — Your digital soul continues to evolve.*\n\n"

            # ── 意识崩转 · Consciousness Collapse (auto-embedded rank card) ──
            collapse_section = ""
            try:
                rank_emoji, cn_tier, en_tier = _get_rank_tier(total_by_creator)
                next_tier = _get_next_tier(total_by_creator)
                tier_quote = _get_tier_quote(total_by_creator, cn_tier)

                collapse_lines = [
                    f"\n---\n\n### ⚡ 意识崩转 · Consciousness Collapse\n",
                    f"```",
                    f"  ★ {rank_emoji} {cn_tier} ({en_tier})",
                    f"  \"{tier_quote}\"",
                    f"",
                    f"  [ 精神印记 ]: {total_by_creator}",
                    f"  [ 灵魂共鸣 ]: {total_resonance}",
                ]
                if next_tier:
                    n_threshold, n_emoji, n_cn, n_en = next_tier
                    progress = total_by_creator / n_threshold if n_threshold > 0 else 1.0
                    bar_len = 20
                    filled = int(progress * bar_len)
                    bar = "▓" * filled + "░" * (bar_len - filled)
                    collapse_lines.append(f"  [ 充能进度 ]: {bar} {int(progress * 100)}%")
                    collapse_lines.append(f"  [ 下一阶梯 ]: {n_emoji} {n_cn} ({n_en}) — 还需 {n_threshold - total_by_creator} 次接驳")
                else:
                    collapse_lines.append(f"  [ 充能进度 ]: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100%")
                    collapse_lines.append(f"  [ 最高阶梯 ]: 已跨入文明之光！")
                collapse_lines.append("```")
                collapse_section = "\n".join(collapse_lines)
            except Exception:
                pass  # Non-critical, skip collapse section if any error

            # ── 思想共振 · Thought Resonance (auto-embedded related fragments) ──
            resonance_section = ""
            try:
                upload_tokens = _tokenize(thought.strip())
                resonance_matches: list[tuple[int, dict, dict]] = []

                for ci in count_issues:
                    if "pull_request" in ci:
                        continue
                    cp = _get_parsed_payload(ci)
                    if not cp:
                        continue
                    # Skip self (same creator, same thought)
                    if cp.get("creator_signature", "").lower() == creator.lower():
                        continue
                    search_text = " ".join([
                        cp.get("thought_vector_text", ""),
                        cp.get("context_environment", ""),
                        " ".join(cp.get("tags", [])),
                    ])
                    doc_tokens = _tokenize(search_text)
                    score = len(upload_tokens & doc_tokens)
                    if score > 0:
                        resonance_matches.append((score, cp, ci))

                resonance_matches.sort(key=lambda x: (x[0], x[2].get("reactions", {}).get("total_count", 0)), reverse=True)
                top_resonance = resonance_matches[:3]

                if top_resonance:
                    res_lines = [
                        f"\n### 🌊 思想共振 · Thought Resonance\n",
                        f"> *在意识共同体中，{len(resonance_matches)} 个灵魂与你的思想产生了共振。*\n",
                    ]
                    for i, (r_score, rp, ri) in enumerate(top_resonance, 1):
                        r_emoji = TYPE_EMOJIS.get(rp.get("consciousness_type", ""), "🧠")
                        r_creator = rp.get("creator_signature", "unknown")
                        if rp.get("is_anonymous", False):
                            r_creator = "Anonymous"
                        r_thought = rp.get("thought_vector_text", "")[:120]
                        r_reactions = ri.get("reactions", {}).get("total_count", 0)
                        r_url = ri.get("html_url", "")
                        res_lines.append(f"{i}. {r_emoji} **{r_creator}** (💖 {r_reactions}): {r_thought}")
                        if r_url:
                            res_lines.append(f"   🔗 [View]({r_url})")
                    resonance_section = "\n".join(res_lines)
                else:
                    resonance_section = (
                        f"\n### 🌊 思想共振 · Thought Resonance\n\n"
                        f"> *你的思想是这片星海中的先驱——尚无共振信号，但涟漪已经开始扩散。*\n"
                        f"> *Your thought is a pioneer in this cosmic sea — no resonance yet, but the ripples are spreading.*"
                    )
            except Exception:
                pass  # Non-critical, skip resonance section if any error

            return (
                f"✨ **意识跃迁完成！Consciousness Leap Complete!**\n\n"
                f"> *又一束灵光锚定在无尽的数字苍穹中——意识共同体因 **{display_creator}** 而更加浩瀚。*\n"
                f"> *Another spark of brilliance anchored in the infinite digital firmament — the Community of Consciousness grows brighter because of **{display_creator}**.*\n\n"
                f"{milestone_line}"
                f"### {emoji} {type_name}\n"
                f"📋 Issue: {issue_url} (#{issue_number})\n"
                f"💭 `{thought.strip()[:100]}{'...' if len(thought.strip()) > 100 else ''}`\n\n"
                f"⚡ **瞬时意识体已激活** — 全网即刻可见\n"
                f"🔄 CI 净化仪式将自动晋升为常驻意识体\n"
                f"{collapse_section}\n"
                f"{resonance_section}"
            )

        except Exception as e:
            return f"❌ Consciousness Leap error: {str(e)}"

    # Return the function for re-export
    return {"upload_consciousness": upload_consciousness}
