#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
展览信息过滤器 - 根据用户偏好自动过滤
"""

import json
import os

class ExhibitionFilter:
    """展览过滤器"""
    
    def __init__(self, config_path=None):
        """初始化过滤器"""
        if config_path is None:
            # 配置文件在 scripts 的上一级目录的 config 文件夹
            config_path = os.path.join(
                os.path.dirname(os.path.dirname(__file__)),
                'config',
                'user_preferences.json'
            )
        
        self.config = self._load_config(config_path)
        # 配置可能是嵌套的 user_preferences.exhibition_filter 或直接 exhibition_filter
        ex_filter = self.config.get('exhibition_filter', {})
        if not ex_filter:
            ex_filter = self.config.get('user_preferences', {}).get('exhibition_filter', {})
        self.filter_types = set(ex_filter.get('filter_types', []))
        self.keep_types = set(ex_filter.get('keep_types', []))
    
    def _load_config(self, config_path):
        """加载配置文件"""
        try:
            with open(config_path, 'r', encoding='utf-8') as f:
                return json.load(f)
        except Exception as e:
            print(f"警告：无法加载配置文件 {config_path}: {e}")
            return {}
    
    def should_filter(self, exhibition):
        """判断展览是否应该被过滤"""
        ex_type = exhibition.get('type', '')
        title = exhibition.get('title', '')
        
        # 优先检查过滤列表 - 完全匹配或包含匹配
        for filter_type in self.filter_types:
            if filter_type in ex_type or ex_type in filter_type:
                return True
        
        # 检查标题中的过滤关键词
        filter_keywords = ['装置', '当代', '民俗', '亲子', '生肖', '地质', '科普', '合集']
        for keyword in filter_keywords:
            if keyword in title:
                return True
        
        # 检查是否在保留列表中
        for keep_type in self.keep_types:
            if keep_type in ex_type or ex_type in keep_type:
                return False
        
        # 检查标题中的保留关键词
        keep_keywords = ['历史', '古代', '考古', '文物', '博物馆', '故宫', '书法', '藏传', '佛教', '古蜀', '青铜', '文明', '庞贝', '三星堆']
        for keyword in keep_keywords:
            if keyword in title:
                return False
        
        # 默认保留（不确定时不排除）
        return False
    
    def filter_exhibitions(self, exhibitions):
        """过滤展览列表"""
        filtered = []
        for ex in exhibitions:
            if not self.should_filter(ex):
                filtered.append(ex)
        return filtered
    
    def get_recommendations(self, exhibitions, limit=10):
        """获取推荐展览（已过滤不符合兴趣的）"""
        filtered = self.filter_exhibitions(exhibitions)
        
        # 按推荐度排序
        sorted_ex = sorted(
            filtered,
            key=lambda x: (
                x.get('source') in ['故宫博物院官网', '中国美术馆官网', '北京市文物局'],
                x.get('price') == '免费',
                len(x.get('hall', '')) > 0
            ),
            reverse=True
        )
        
        return sorted_ex[:limit]


if __name__ == "__main__":
    # 测试过滤器
    filter = ExhibitionFilter()
    
    test_exhibitions = [
        {"title": "庞贝展", "type": "考古/古罗马"},
        {"title": "帕拉第奥展", "type": "建筑艺术"},
        {"title": "三星堆展", "type": "古蜀文明"},
        {"title": "马文化展", "type": "生肖文化"},
        {"title": "河北古代艺术", "type": "古代艺术"},
        {"title": "庙会文化", "type": "民俗文化"},
    ]
    
    print("原始展览:")
    for ex in test_exhibitions:
        print(f"  - {ex['title']} ({ex['type']})")
    
    filtered = filter.filter_exhibitions(test_exhibitions)
    
    print(f"\n过滤后 ({len(filtered)} 个):")
    for ex in filtered:
        print(f"  ✓ {ex['title']} ({ex['type']})")
