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

"""
Desktop 相关白名单函数 - 转发到 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_workspace_sidebar_items(**kwargs):
    """获取工作区侧边栏项目 - 转发到 SaaS 端"""
    url = f"{Config.jingrow_server_url}/api/action/jingrow.desk.desktop.get_workspace_sidebar_items"
    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.post(url, headers=headers, json=kwargs, timeout=30)
    if resp.status_code == 200:
        return resp.json().get("message", {})
    logger.error(f"[get_workspace_sidebar_items] SaaS error: {resp.status_code}")
    return {}


@jingrow.whitelist()
def get_desktop_page(**kwargs):
    """获取桌面页面 - 转发到 SaaS 端"""
    url = f"{Config.jingrow_server_url}/api/action/jingrow.desk.desktop.get_desktop_page"
    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.post(url, headers=headers, json=kwargs, timeout=30)
    if resp.status_code == 200:
        return resp.json().get("message", {})
    logger.error(f"[get_desktop_page] SaaS error: {resp.status_code}")
    return {}
