# Copyright (c) 2025, JINGROW and contributors
# For license information, please see license.txt

"""
Local Job 相关白名单函数 - 转发到 SaaS 端
"""

import jingrow
import requests
import logging
from jingrow.config import Config
from jingrow.utils.auth import get_request_session_cookie

logger = logging.getLogger(__name__)


@jingrow.whitelist()
def get_local_job_count(**kwargs):
    """获取本地任务计数 - 转发到 SaaS 端"""
    url = f"{Config.jingrow_server_url}/api/action/jingrow.core.pagetype.local_job.local_job.get_local_job_count"
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    
    session_cookie = get_request_session_cookie()
    if session_cookie:
        headers["Cookie"] = f"sid={session_cookie}"
    
    resp = requests.get(url, headers=headers, params=kwargs, timeout=30)
    if resp.status_code == 200:
        return resp.json().get("message", 0)
    logger.error(f"[get_local_job_count] SaaS error: {resp.status_code}")
    return 0
