"""
CloudSearch Search Enricher v1.0.0
搜索结果增强：TMDB匹配 + 过期检测 + 内容去重
"""

import time
import logging
from typing import List, Dict, Any, Optional
from tmdb_enricher import TMDBEnricher

logger = logging.getLogger("enricher")


class SearchEnricher:
    """搜索结果增强器"""

    def __init__(self, tmdb_api_key: str = "", cache_ttl: int = 86400):
        self.tmdb = TMDBEnricher(tmdb_api_key, cache_ttl=cache_ttl) if tmdb_api_key else None

    def enrich_results(self, results: List[Dict], keyword: str = "") -> List[Dict]:
        """批量增强搜索结果"""
        if not results:
            return results

        enriched = []
        titles_to_lookup = []

        # 收集需要查 TMDB 的标题
        for r in results:
            title = r.get("title", "")
            if title and self.tmdb:
                titles_to_lookup.append(title)

        # 批量查询 TMDB
        tmdb_results = {}
        if titles_to_lookup and self.tmdb:
            tmdb_results = self.tmdb.enrich_batch(titles_to_lookup[:20], max_concurrent=5)

        # 应用增强
        for r in results:
            title = r.get("title", "")
            media = tmdb_results.get(title)

            enriched_item = dict(r)
            if media:
                enriched_item.update({
                    "tmdb_id": media.tmdb_id,
                    "tmdb_url": media.tmdb_url,
                    "poster": media.poster_url,
                    "backdrop": media.backdrop_url,
                    "rating": media.rating,
                    "rating_count": media.rating_count,
                    "year": media.year,
                    "genres": media.genres,
                    "description": media.description,
                    "media_type": media.media_type,
                    "directors": media.directors,
                    "actors": media.actors[:5],
                    "enriched": True,
                })

                # 自动生成更好的标题
                if media.year and media.rating:
                    enriched_item["display_title"] = (
                        f"{title} ({media.year}) ⭐{media.rating}"
                    )

            enriched.append(enriched_item)

        return enriched

    def enrich_single(self, title: str, keyword: str = "") -> Optional[Dict]:
        """增强单个标题"""
        if not self.tmdb:
            return None
        media = self.tmdb.enrich(title)
        if not media:
            return None
        return {
            "title": title,
            "tmdb_id": media.tmdb_id,
            "poster": media.poster_url,
            "rating": media.rating,
            "year": media.year,
            "genres": media.genres,
            "description": media.description,
            "media_type": media.media_type,
        }


# Flask API wrapper
def create_enricher_api(tmdb_key: str = ""):
    from flask import Flask, request, jsonify
    app = Flask(__name__)
    enricher = SearchEnricher(tmdb_key)

    @app.route("/health", methods=["GET"])
    def health():
        return jsonify({"status": "ok", "version": "1.0.0"})

    @app.route("/enrich", methods=["POST"])
    def enrich():
        data = request.get_json() or {}
        results = data.get("results", [])
        keyword = data.get("keyword", "")

        if not results:
            return jsonify({"error": "results required"}), 400

        enriched = enricher.enrich_results(results, keyword)
        return jsonify({"results": enriched, "count": len(enriched)})

    @app.route("/lookup", methods=["POST"])
    def lookup():
        data = request.get_json() or {}
        title = data.get("title", "")
        if not title:
            return jsonify({"error": "title required"}), 400

        info = enricher.enrich_single(title)
        return jsonify(info or {})

    return app


if __name__ == "__main__":
    import os
    api_key = os.getenv("TMDB_API_KEY", "")
    port = int(os.getenv("PORT", "9530"))
    app = create_enricher_api(api_key)
    logger.info(f"Enricher API on port {port}")
    app.run(host="0.0.0.0", port=port)
