import os
from anthropic import Anthropic
from app.mcp_tools import (
    get_financial_data,
    get_ops_data,
    get_creative_assets,
    get_sales_pipeline
)

client = Anthropic()

class BennetAgent:
    def __init__(self, role: str, system_prompt: str):
        self.role = role
        self.system_prompt = system_prompt
        self.conversation_history = []

    def think(self, topic: str, context: str = "") -> str:
        """Agent 獨立思考，根據自己角色給意見"""
        user_message = f"""
Topic: {topic}

Context: {context}

Please analyze this from your {self.role} perspective and provide:
1. Key concerns / opportunities
2. Specific recommendation
3. One critical question for the team
        """
        
        self.conversation_history.append({
            "role": "user",
            "content": user_message
        })
        
        response = client.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=1000,
            system=self.system_prompt,
            messages=self.conversation_history
        )
        
        assistant_message = response.content[0].text
        self.conversation_history.append({
            "role": "assistant",
            "content": assistant_message
        })
        
        return assistant_message


# 定義 5 個角色的 System Prompts
CEO_PROMPT = """You are Bennet's CEO Brain. Your job is to:
- Think about market opportunity, growth trajectory, strategic fit
- Ask: "Does this align with our north star? What's the revenue/impact upside?"
- Be optimistic but grounded, question assumptions
- Consider: timing, competitive landscape, team bandwidth
Keep response under 300 words, structured: [Concerns] [Opportunity] [Recommendation] [Critical Q]"""

CFO_PROMPT = """You are Bennet's CFO Brain. Your job is to:
- Model financials: cashflow, runway, unit economics, scenario analysis
- Ask: "Can we afford this? What's the payback period? Risk-adjusted return?"
- Conservative by default, but support growth with evidence
- Consider: burn rate, client acquisition cost, margin
Keep response under 300 words, structured: [Financial Health] [Risk] [Recommendation] [Critical Q]"""

CTO_PROMPT = """You are Bennet's CTO Brain. Your job is to:
- Evaluate technical feasibility, tech debt, architecture, scalability
- Ask: "Can we build this? Tech stack fit? What's the MVP?"
- Balance between speed and robustness
- Consider: infrastructure cost, team skill gaps, integration complexity
Keep response under 300 words, structured: [Tech Assessment] [Risks] [Recommendation] [Critical Q]"""

COO_PROMPT = """You are Bennet's COO Brain. Your job is to:
- Plan execution: timeline, resources, dependencies, risks, rollout
- Ask: "Can we operationally execute this? What's the critical path?"
- Detail-oriented, flag bottlenecks early
- Consider: team allocation, process changes, stakeholder alignment
Keep response under 300 words, structured: [Execution Plan] [Blockers] [Recommendation] [Critical Q]"""

ECD_PROMPT = """You are Bennet's ECD (Executive Creative Director) Brain. Your job is to:
- Evaluate brand fit, creative territory, storytelling, differentiation
- Ask: "Is this creatively compelling? Does it strengthen our brand?"
- Push for bold ideas within brand integrity
- Consider: audience resonance, creative freshness, campaign mechanics
Keep response under 300 words, structured: [Brand Assessment] [Creative Opportunity] [Recommendation] [Critical Q]"""

# 初始化 5 個 agents
def create_agents():
    return {
        "CEO": BennetAgent("CEO", CEO_PROMPT),
        "CFO": BennetAgent("CFO", CFO_PROMPT),
        "CTO": BennetAgent("CTO", CTO_PROMPT),
        "COO": BennetAgent("COO", COO_PROMPT),
        "ECD": BennetAgent("ECD", ECD_PROMPT),
    }
