#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
services/mcp_context_service.py
MCP (Model Context Protocol) 橋接服務
此檔案旨在橋接 OpenClaw 戰略分析所需的外部情報與 MCP 集成層。
"""

import logging
from datetime import datetime
from services.mcp_collector_service import mcp_collector

logger = logging.getLogger(__name__)

class MCPRouter:
    """MCP 路由分發器（模擬或橋接至現有收集器）"""
    @staticmethod
    def get_context(topic: str) -> str:
        return mcp_collector.collect_topic(topic)

def build_mcp_context(query: str = "", topics: list = None) -> str:
    """構建完整 MCP 上下文供 AI 分析使用"""
    if topics is None:
        topics = ["market_trends", "holiday_calendar", "seasonal_insights"]
    
    results = []
    for topic in topics:
        content = mcp_collector.collect_topic(topic)
        if content:
             results.append(f"【{topic}】\n{content}")
    
    # 加入靜態輔助資訊
    results.append(mcp_collector.get_holiday_context())
    results.append(mcp_collector.get_seasonal_context())
    
    return "\n\n".join(results)

def query_mcp(question: str) -> str:
    """通用的 MCP 查詢入口"""
    # 這裡可以根據 question 決定回傳哪些主題內容
    return build_mcp_context(question)

def get_tw_media_news() -> str:
    """取得台灣媒體新聞情報"""
    return mcp_collector.collect_topic("market_trends")

def get_ecommerce_news() -> str:
    """取得台灣電商新聞行情"""
    return mcp_collector.collect_topic("competitor_intel")

def get_taiwan_trends() -> str:
    """取得台灣 Google 趨勢或熱搜"""
    return mcp_collector.collect_topic("market_trends")

def get_dcard_trends() -> str:
    """取得 Dcard 熱門討論洞察"""
    return mcp_collector.collect_topic("consumer_sentiment")

def get_youtube_trending() -> str:
    """取得 YouTube 台灣熱門趨勢"""
    return mcp_collector.collect_topic("market_trends")

def get_taiwan_weather() -> str:
    """取得台灣天氣概況（影響消費行為）"""
    # 簡易橋接，可用搜尋 Grounding 獲取
    return mcp_collector.collect_topic("台灣近日天氣與氣溫概況")

def get_twbank_exchange_rates() -> str:
    """取得台幣匯率概況（影響跨境採購）"""
    return mcp_collector.collect_topic("台幣匯率趨勢")

def get_upcoming_events() -> str:
    """取得近期重要行事曆事件"""
    return mcp_collector.get_holiday_context()
