#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
任務執行器服務
封裝背景任務執行邏輯，避免 app.py 與路由模組的循環依賴
"""

import threading
from datetime import datetime, timezone, timedelta

from services.logger_manager import SystemLogger

# 時區設定
TAIPEI_TZ = timezone(timedelta(hours=8))

# Logger
sys_log = SystemLogger("TaskRunner").get_logger()


def run_momo_task_with_notification():
    """
    執行 MOMO 爬蟲任務並發送通知
    此函數會在背景執行緒中運行
    """
    import importlib

    timestamp = datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')
    sys_log.info(f"[TaskRunner] [{timestamp}] 啟動背景抓取執行緒...")

    def job():
        try:
            # 1. 執行爬蟲任務
            import scheduler
            importlib.reload(scheduler)
            scheduler.run_momo_task()

            # 2. 發送通知 (僅發送今日異動)
            try:
                import services.notification_manager
                importlib.reload(services.notification_manager)
                from services.notification_manager import NotificationManager
                from services.dashboard_service import get_dashboard_stats

                stats = get_dashboard_stats()

                # 只要有任何異動數據就發送通知
                if any(stats.values()):
                    screenshot_path = scheduler.capture_page_screenshot("http://127.0.0.1/", "momo_dashboard")
                    NotificationManager().send_momo_report(stats, screenshot_path)
            except Exception as e:
                sys_log.error(f"[TaskRunner] 發送通知失敗: {e}")

        except Exception as e:
            sys_log.error(f"[TaskRunner] 爬蟲任務執行失敗: {e}")

    threading.Thread(target=job, daemon=True).start()
