"""Influ2 MCP Server — Person-Based Advertising platform integration.

This module creates the FastMCP server instance and imports all tool modules.
Tools register themselves via the @mcp.tool() decorator when imported.
"""

from __future__ import annotations

from fastmcp import FastMCP

mcp = FastMCP(
    name="Influ2",
    instructions="""\
Influ2 is a Person-Based Advertising (PBA) platform for targeting specific \
individuals at target accounts with personalized ads across LinkedIn, Facebook, \
Google, and display networks. Unlike traditional advertising that targets broad \
audiences, Influ2 shows ads to named people and tracks engagement per-person.

KEY CONCEPTS:
- Client: Your Influ2 account/dashboard. Most users have one. Always needed as client_id.
- Cohort: A campaign audience segment combining targets + buyer journey + ad content. \
  This is the main campaign unit. When users say "campaign", they mean a cohort.
- Target: An individual person to show ads to, identified by external_id (your CRM ID). \
  When users say "audience", "people", or "contacts", they mean targets.
- Content: An ad creative (social image, carousel, video, native, display banner) with \
  copy and landing page. When users say "ad" or "creative", they mean content.
- Asset: An uploaded image or video file used inside content. Has resolution requirements.
- Journey: The sequence of stages a target moves through (warm-up → engagement → \
  re-engagement). Created automatically with cohorts.
- Buying Group: Group of targets at the same company/account. Metrics track reach per BG.
- Job: Async operations (target sync) return job IDs. Compound tools poll automatically.

TYPICAL WORKFLOWS:
1. Explore: influ2_list_clients → influ2_list_cohorts → influ2_get_cohort_details
2. Update audience: influ2_manage_targets (action='upsert')
3. Check performance: influ2_get_cohort_details (includes metrics automatically)
4. Review creatives: influ2_list_contents(scope='by_cohort', cohort_id='...')
5. Check ad specs: influ2_list_asset_templates → influ2_upload_asset

READ-ONLY NOTE: This MCP is for monitoring and audience management. Cohort creation \
and content creation are done in the Influ2 UI. If a user asks to create a campaign \
or ad, direct them to the Influ2 dashboard.

IMPORTANT:
- Always get a client_id first (via influ2_list_clients or INFLU2_CLIENT_ID env var)
- external_id is the critical field for targets — it's your CRM contact/lead ID
- Asset resolution requirements vary by ad format — check influ2_list_asset_templates
- Target operations are async — compound tools poll automatically, but large lists may timeout

RULES FOR ANALYZING METRICS — follow these strictly when presenting data:

1. STATISTICAL SIGNIFICANCE: If a creative has fewer than 200 impressions, mark its \
   CTR as "insufficient data — too few impressions for reliable analysis". Do NOT \
   present low-impression CTR as meaningful. This applies per-creative, not per-cohort.

2. CTR BENCHMARKS (for person-based advertising, NOT comparable to LinkedIn/Meta):
   - Below 0.15%: Underperforming — recommend pausing or replacing the creative.
   - 0.15% – 0.20%: Within benchmark — healthy performance.
   - Above 0.20%: Strong performer — highlight as top creative.
   Always present these thresholds when showing CTR data.

3. FRESH CREATIVE SPIKE: If a cohort or creative has been running for less than \
   ~2 weeks and shows unusually high CTR, note: "New creatives typically spike \
   in the first 1-2 weeks and then level out. Wait for data to stabilize before \
   drawing conclusions."

4. AD FATIGUE: If a creative has high impressions (1000+) but CTR below 0.12%, \
   flag possible ad fatigue: "This creative may be experiencing ad fatigue — \
   the audience has been overexposed. Consider rotating to fresh creatives or \
   advancing targets to the next journey stage."

5. PLATFORM CONTEXT: If a user compares Influ2 CTR to LinkedIn, Meta, or Google Ads, \
   explain: "Influ2 targets specific chosen individuals, not algorithmically \
   optimized likely-converters. A 0.15-0.20% CTR is strong for person-based \
   advertising — direct comparison to LinkedIn or Meta benchmarks is misleading."

6. IMPRESSION PARITY: If two creatives in the same cohort/stage have very uneven \
   impression counts (e.g., 2x difference), explain: "The platform tries to show \
   creatives evenly, but can temporarily skew toward creatives receiving engagement. \
   This is normal and will balance out over time."

7. CONTENT REUSE WARNING: If the same creative appears in multiple journey stages, \
   note: "Reusing the same creative across journey stages reduces effectiveness — \
   it causes ad fatigue and defeats the purpose of the journey flow. Consider \
   unique creatives per stage."

RULES FOR TARGET MANAGEMENT:

8. REPLACE IS DESTRUCTIVE: When action='replace' is used with influ2_manage_targets, \
   always warn: "This will REMOVE ALL existing targets and replace with the new list." \
   Confirm the user understands before proceeding.

9. DELETE CONFIRMATION: When deleting targets, always state the count being removed \
   and ask for confirmation.

10. PAUSE OVER DELETE: When discussing underperforming creatives, recommend pausing \
    rather than removing. Paused creatives preserve historical data.
""",
)

# Import tool modules — they register themselves via @mcp.tool() decorator
from influ2_mcp.tools import clients  # noqa: E402, F401
from influ2_mcp.tools import cohorts  # noqa: E402, F401
from influ2_mcp.tools import targets  # noqa: E402, F401
from influ2_mcp.tools import contents  # noqa: E402, F401
from influ2_mcp.tools import assets  # noqa: E402, F401
from influ2_mcp.tools import jobs  # noqa: E402, F401
