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

"""
Form load whitelist functions - Forward to SaaS using unified HTTP client
"""

import jingrow
import logging
import time
from jingrow.utils.auth import saas_post

logger = logging.getLogger(__name__)


@jingrow.whitelist()
def getdoc(**kwargs):
    """Get document - Forward to SaaS using unified HTTP client"""
    start_time = time.time()
    
    print(f"[getdoc] Requesting: pagetype={kwargs.get('pagetype')}, name={kwargs.get('name')}")
    
    resp = saas_post('/api/action/jingrow.desk.form.load.getdoc', json=kwargs)
    
    req_time = time.time() - start_time
    print(f"[getdoc] SaaS response: status={resp.status_code}, time={req_time:.3f}s")
    
    if resp.status_code == 200:
        result = resp.json()
        resp_size = len(resp.content) / 1024  # KB
        print(f"[getdoc] Total time: {time.time() - start_time:.3f}s, response size: {resp_size:.1f}KB")
        return result
    print(f"[getdoc] SaaS error: {resp.status_code} - {resp.text[:200]}")
    return {}
