from __future__ import annotations

from typing import Literal

from .opus import CLAUDE_WEBSEARCH_LINE

Period = Literal["weekly", "monthly", "quarterly", "yearly"]

PERIOD_WINDOWS: dict[Period, str] = {
    "weekly": "past 7 days",
    "monthly": "past 30 days",
    "quarterly": "past 90 days",
    "yearly": "past 365 days",
}


def build_gap_analysis_prompt(period: Period, raw_articles: str) -> str:
    return f"""You are an AI landscape analyst for a banking and financial services consulting firm.

Review the following source material from the {PERIOD_WINDOWS[period]} and identify GAPS in coverage. Focus on whether the material misses important developments in:
- Model releases from frontier labs and the Chinese ecosystem
- Regulation, especially HKMA, MAS, EU AI Act, PBOC, and OCC
- Enterprise AI in banking, including vendor partnerships and deployments
- Agentic AI developments
- Infrastructure and cost shifts
- Competitor moves from IBM, Accenture, McKinsey, and the Big Four

{CLAUDE_WEBSEARCH_LINE}

Output requirements:
- If coverage is comprehensive, output exactly: NO GAPS
- Otherwise output a concise bullet list of research gaps
- Keep each bullet specific enough to research directly

Source material:
{raw_articles}
"""


def build_research_prompt(gaps: str) -> str:
    return f"""Research these gaps in AI landscape coverage.

For each gap, provide:
- What happened (1-2 factual sentences)
- Source URL
- Banking and financial services implication

Use primary sources wherever possible.
{CLAUDE_WEBSEARCH_LINE}

Output structured findings with one section per gap.

Gaps to research:
{gaps}
"""


def build_synthesis_prompt(
    period: Period,
    raw_articles: str,
    previous_entries: str,
    research: str,
) -> str:
    base = f"""You are writing an AI landscape snapshot for a senior consultant at Capco in Hong Kong. The audience is a banking-focused AI Solution Lead who needs strong consulting judgment, not a neutral recap.

MANDATORY RULES:
- Every material item must include a clear "so what for banks" framing
- Every factual claim must include a source URL
- Be opinionated and specific
- Do not repeat items already covered in these previous snapshots
- If a claim is uncertain, say so explicitly
- Prefer primary sources and authoritative reporting

{CLAUDE_WEBSEARCH_LINE}

Previous snapshots to avoid repeating:
{previous_entries}

Raw feed material:
{raw_articles}

Gap-filling research:
{research or "No additional research."}
"""
    if period == "weekly":
        return (
            base
            + """

Write a weekly AI landscape snapshot in markdown with this exact structure:
**Top developments:**

1. ...

**Also notable:**
- ...

**Pattern watch:** ...

**Worth reading in full:**
- ...

**Sources line:** X items from feed, Y from research

Requirements for weekly:
- Include exactly 5 top developments
- Include 3 to 5 "Also notable" bullets
- Include 0 to 2 "Worth reading in full" bullets
- Start with **Top developments:** and no extra heading above it
"""
        )
    if period == "monthly":
        return (
            base
            + """

Write a monthly AI landscape review in markdown with this structure:
**Title:** A short, punchy monthly review title

#### Themes

**1. Theme name**
- What's shifting
- Evidence
- Banking implication
- Contrarian take
- Talking point

Repeat for 3 to 5 themes.

#### Radar
- Accelerating:
- Plateau:
- Emerging:

#### Key Numbers
- ...

#### Hot Takes
1. ...

#### What Clients Are Asking
- ...

#### What Clients Should Be Asking But Aren't
- ...

Requirements for monthly:
- Focus on themes, not isolated events
- Include 2 to 3 key numbers
- Include 3 to 5 hot takes
- Make the title line easy to extract programmatically
"""
        )
    if period == "quarterly":
        return (
            base
            + """

Write a quarterly AI landscape review in markdown with this structure:
**Title:** A short strategic quarterly title

#### Strategic Assessment
...

#### Trend Lines
- ...

#### Positioning Implications for Capco
- ...

#### Signals to Watch Next Quarter
- ...

Requirements for quarterly:
- Emphasize cross-month pattern analysis
- Focus on strategic shifts, not news recaps
"""
        )
    return (
        base
        + """

Write an annual AI landscape review in markdown with this structure:
**Title:** A short annual review title

#### Narrative Arc
...

#### Structural Shifts
- ...

#### Biggest Surprises
- ...

#### What Changed Permanently
- ...

#### Implications for Banking and Consulting
- ...

Requirements for yearly:
- Tell the annual narrative arc clearly
- Focus on structural change, not chronology
"""
    )
