#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查北京展览通知标记
如果存在则返回消息内容，由调用方通过 OpenClaw message 工具发送
"""

import json
import sys
from pathlib import Path

BASE_DIR = Path(__file__).parent
NOTIFICATION_FILE = BASE_DIR.parent / "notification_pending.json"


def check_notification():
    """检查通知标记"""
    if not NOTIFICATION_FILE.exists():
        return None, "没有待发送的通知"
    
    try:
        with open(NOTIFICATION_FILE, "r", encoding="utf-8") as f:
            notification = json.load(f)
        
        # 验证通知类型
        if notification.get("type") != "beijing_exhibitions":
            return None, f"通知类型不匹配：{notification.get('type')}"
        
        # 获取消息内容
        message = notification.get("message")
        if not message:
            return None, "消息内容为空"
        
        # 返回消息内容（替换 \n 为实际换行）
        message = message.replace("\\n", "\n")
        
        return message, "OK"
    
    except Exception as e:
        return None, f"处理通知失败：{e}"


def clear_notification():
    """清除通知标记"""
    if NOTIFICATION_FILE.exists():
        try:
            NOTIFICATION_FILE.unlink()
            return True, "通知标记已清除"
        except Exception as e:
            return False, f"清除失败：{e}"
    return True, "没有标记文件"


if __name__ == "__main__":
    message, status = check_notification()
    
    if message:
        # 输出 JSON 格式供调用方解析
        result = {
            "has_notification": True,
            "message": message,
            "status": status
        }
    else:
        result = {
            "has_notification": False,
            "message": None,
            "status": status
        }
    
    print(json.dumps(result, ensure_ascii=False, indent=2))
    sys.exit(0)
