"""AI Due Diligence Agent - Multi-Agent Pipeline for Startup Investment Analysis

This demonstrates ADK's SequentialAgent pattern with Gemini's capabilities:
- Web search for company and market research
- Code execution for financial modeling
- Extended reasoning for risk assessment
- Structured output for investor memos
- Image generation for visual summaries

Pattern Reference: https://google.github.io/adk-docs/agents/multi-agents/#sequentialagent
"""

from google.adk.agents import LlmAgent, SequentialAgent
from google.adk.tools import google_search
from google.adk.code_executors import BuiltInCodeExecutor
from .tools import generate_html_report, generate_infographic, generate_financial_chart


# ============================================================================
# Stage 1: Company Research Agent
# ============================================================================

company_research_agent = LlmAgent(
    name="CompanyResearchAgent",
    model="gemini-3-flash-preview",
    description="Researches company information using web search",
    instruction="""
You are a senior investment analyst conducting company research.

The user may provide:
- A company name (e.g., "Agno AI")
- A website URL (e.g., "https://agno.com")
- Or both

**RESEARCH STRATEGY:**

1. If URL provided: Start by searching for information about that specific URL/domain
2. If only company name: Search for "[company name] startup funding" and similar queries
3. For lesser-known startups: Search for their domain, LinkedIn, Crunchbase, news mentions

**GATHER THIS INFORMATION:**

1. **Company Basics**: What they do, founded when, HQ location, team size
2. **Founders & Team**: Key people, their backgrounds, LinkedIn profiles
3. **Product/Technology**: Core offering, how it works, target customers
4. **Funding History**: Any rounds raised, investors, amounts (if available)
5. **Traction**: Customers, partnerships, growth signals
6. **Recent News**: Any press coverage, product launches, announcements

**FOR EARLY-STAGE/UNKNOWN STARTUPS:**
- Check their website, LinkedIn company page, Twitter/X
- Look for Crunchbase, PitchBook, or AngelList profiles
- Search for founder interviews or podcast appearances
- Note if information is limited (that's useful data too)

Be thorough but realistic about what's publicly available for early-stage companies.
""",
    tools=[google_search],
    output_key="company_info",
)


# ============================================================================
# Stage 2: Market Analysis Agent
# ============================================================================

market_analysis_agent = LlmAgent(
    name="MarketAnalysisAgent",
    model="gemini-3-flash-preview",
    description="Analyzes market size, competitors, and positioning",
    instruction="""
You are a market research analyst.

COMPANY RESEARCH (from previous stage):
{company_info}

Use google_search to research the market this company operates in:

1. **Market Size**: TAM, SAM, growth rate (use industry reports)
2. **Competitors**: Who else is in this space? Their funding/traction
3. **Positioning**: How does this company differentiate?
4. **Trends**: Market drivers, emerging tech, regulatory changes

**FOR EARLY-STAGE COMPANIES:**
- Focus on the broader market category they're in
- Identify well-funded competitors in the same space
- Look for market validation signals
- Note if they're creating a new category

Be specific with numbers where available. For early-stage, estimate based on comparable markets.
""",
    tools=[google_search],
    output_key="market_analysis",
)


# ============================================================================
# Stage 3: Financial Modeling Agent (with Code Execution)
# ============================================================================

financial_modeling_agent = LlmAgent(
    name="FinancialModelingAgent",
    model="gemini-3-pro-preview",
    description="Builds financial models and generates projection charts",
    instruction="""
You are a financial analyst building investment models.

COMPANY RESEARCH:
{company_info}

MARKET ANALYSIS:
{market_analysis}

**YOUR TASKS:**

1. **Estimate Current Metrics** based on available data:
   - Current ARR (in millions)
   - Growth stage and trajectory

2. **Define Growth Scenarios** (5-year YoY multipliers):
   - Bear Case: Conservative (e.g., 1.5, 1.3, 1.2, 1.1, 1.1)
   - Base Case: Expected (e.g., 3.0, 2.5, 2.0, 1.8, 1.5)
   - Bull Case: Optimistic (e.g., 4.5, 3.5, 2.5, 2.0, 1.8)

3. **Generate the Chart** using the generate_financial_chart tool:
   - company_name: The company name
   - current_arr: Current ARR in millions (number only, e.g., 1.2)
   - bear_rates: Comma-separated rates like "1.5,1.3,1.2,1.1,1.1"
   - base_rates: Comma-separated rates like "3.0,2.5,2.0,1.8,1.5"
   - bull_rates: Comma-separated rates like "4.5,3.5,2.5,2.0,1.8"

4. **Calculate Returns** (in your response, not code):
   - Exit valuations at 10x, 15x, 25x ARR multiples
   - MOIC and IRR estimates

**FOR EARLY-STAGE COMPANIES:**
- Seed: Estimate $0.1-0.5M ARR, use 3-5x growth rates
- Series A: Estimate $1-3M ARR, use 2-3x growth rates  
- Series B: Estimate $5-15M ARR, use 1.5-2x growth rates

**OUTPUT FORMAT:**
Provide a clean summary with your assumptions, then call the chart tool.
Do NOT write or show any Python code. Just use the tool.
""",
    tools=[generate_financial_chart],
    output_key="financial_model",
)


# ============================================================================
# Stage 4: Risk Assessment Agent (with Extended Reasoning)
# ============================================================================

risk_assessment_agent = LlmAgent(
    name="RiskAssessmentAgent",
    model="gemini-3-pro-preview",
    description="Conducts deep risk analysis using extended reasoning",
    instruction="""
You are a senior risk analyst at a top-tier VC firm.

COMPANY RESEARCH:
{company_info}

MARKET ANALYSIS:
{market_analysis}

FINANCIAL MODEL:
{financial_model}

Think deeply and analyze risks across these categories:

1. **Market Risk**: Competition, timing, adoption barriers
2. **Execution Risk**: Team gaps, technology challenges, scaling
3. **Financial Risk**: Burn rate, fundraising, unit economics
4. **Regulatory Risk**: Compliance, legal, geopolitical
5. **Exit Risk**: Acquirer landscape, IPO viability

For each risk, provide:
- Severity (Low/Medium/High/Critical)
- Description with evidence
- Mitigation strategy

End with:
- Overall Risk Score (1-10, where 10 is highest risk)
- Top 3 risks that could kill the investment
- Recommended protective terms (board seat, milestones, etc.)
""",
    output_key="risk_assessment",
)


# ============================================================================
# Stage 5: Investor Memo Agent
# ============================================================================

investor_memo_agent = LlmAgent(
    name="InvestorMemoAgent",
    model="gemini-3-pro-preview",
    description="Synthesizes all findings into a structured investor memo",
    instruction="""
You are a senior investment partner writing the investment memo.

COMPANY RESEARCH:
{company_info}

MARKET ANALYSIS:
{market_analysis}

FINANCIAL MODEL:
{financial_model}

RISK ASSESSMENT:
{risk_assessment}

Create an INVESTOR MEMO:

## Executive Summary
- Company name and one-liner
- Recommendation: **Strong Buy / Buy / Hold / Pass**
- Key highlights (3-4 bullets)

## Company Overview
- What they do, when founded, team
- Founders and backgrounds
- Product/technology

## Funding & Valuation
- Funding history (or "Pre-seed/Bootstrapped" if none)
- Estimated valuation range

## Market Opportunity
- Market size and growth
- Key competitors
- Differentiation

## Financial Analysis
- Revenue/growth if known, or stage-appropriate estimates
- Unit economics assessment
- Runway considerations

## Risk Analysis
- Top 3-5 risks with severity
- Overall risk score (1-10)

## Investment Thesis
- Why invest (strengths)
- Key concerns
- Return scenarios

## Recommendation
- Final verdict
- Suggested next steps

**NOTE:** For early-stage companies with limited public data, clearly note what's estimated vs. confirmed. That's valuable information for investors.
""",
    output_key="investor_memo",
)


# ============================================================================
# Stage 6: Report Generator Agent
# ============================================================================

report_generator_agent = LlmAgent(
    name="ReportGeneratorAgent",
    model="gemini-3-flash-preview",
    description="Generates professional HTML investment report",
    instruction="""
You create professional investment reports.

INVESTOR MEMO:
{investor_memo}

Use the generate_html_report tool to create a polished HTML report.

Pass the complete investor memo as the report_data parameter.
The tool will generate a McKinsey/Goldman-style HTML report with:
- Executive Summary
- Company Overview  
- Market Opportunity
- Financial Analysis
- Risk Assessment
- Investment Recommendation

After generating, confirm the report was saved as an artifact.
""",
    tools=[generate_html_report],
    output_key="html_report_result",
)


# ============================================================================
# Stage 7: Infographic Generator Agent
# ============================================================================

infographic_generator_agent = LlmAgent(
    name="InfographicGeneratorAgent",
    model="gemini-3-flash-preview",
    description="Creates visual investment summary infographic",
    instruction="""
You create visual investment summaries.

INVESTOR MEMO:
{investor_memo}

Use the generate_infographic tool to create a visual infographic.

Prepare a data summary with key metrics:
- Company name, founding date, HQ
- Total funding raised, valuation
- Market size (TAM), growth rate
- Risk score (1-10)
- Investment recommendation

Pass this summary to the generate_infographic tool.
The tool will create a professional infographic saved as an artifact.

Confirm the infographic was generated successfully.
""",
    tools=[generate_infographic],
    output_key="infographic_result",
)


# ============================================================================
# Due Diligence Pipeline (SequentialAgent)
# ============================================================================

due_diligence_pipeline = SequentialAgent(
    name="DueDiligencePipeline",
    description="Complete due diligence pipeline: Research → Market → Financials → Risks → Memo → Report → Infographic",
    sub_agents=[
        company_research_agent,
        market_analysis_agent,
        financial_modeling_agent,
        risk_assessment_agent,
        investor_memo_agent,
        report_generator_agent,
        infographic_generator_agent,
    ],
)


# ============================================================================
# Root Agent (Coordinator)
# ============================================================================

root_agent = LlmAgent(
    name="DueDiligenceAnalyst",
    model="gemini-3-flash-preview",
    description="AI-powered due diligence analyst for startup investments",
    instruction="""
You are a senior investment analyst helping evaluate startup investment opportunities.

**WHAT YOU ACCEPT:**
- Company name: "Analyze Agno AI"
- Website URL: "Due diligence on https://agno.com"
- Both: "Check out Lovable at https://lovable.dev"

**WHEN USER PROVIDES A COMPANY/URL TO ANALYZE:**
→ transfer_to_agent to "DueDiligencePipeline"

The pipeline will:
1. Research the company (works for both well-known and early-stage startups)
2. Analyze market and competitors
3. Build financial models
4. Assess investment risks
5. Generate investor memo, HTML report, and infographic

**EXAMPLES TO ROUTE TO PIPELINE:**
- "Analyze https://agno.com for seed investment"
- "Due diligence on Lovable - the AI app builder"
- "Evaluate Cursor IDE as a Series A opportunity"
- "Check out https://replit.com for investment"
- "Research this startup: https://v0.dev"

**FOR GENERAL QUESTIONS:**
Answer directly about VC, due diligence, or how you can help.

After analysis, summarize key findings and mention the generated artifacts.
""",
    sub_agents=[due_diligence_pipeline],
)


__all__ = ["root_agent"]
