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

"""
Client whitelist functions - Forward to SaaS using unified HTTP client
"""

import jingrow
import logging
from jingrow.utils.auth import saas_get, saas_post

logger = logging.getLogger(__name__)


@jingrow.whitelist()
def get_list(**kwargs):
    """Get list - Forward to SaaS"""
    resp = saas_post('/api/action/jingrow.client.get_list', json=kwargs)
    if resp.status_code == 200:
        return resp.json().get("message", [])
    logger.error(f"[get_list] SaaS error: {resp.status_code} - {resp.text[:200]}")
    return []


@jingrow.whitelist()
def get_value(**kwargs):
    """Get value - Forward to SaaS"""
    resp = saas_post('/api/action/jingrow.client.get_value', json=kwargs)
    if resp.status_code == 200:
        return resp.json().get("message")
    logger.error(f"[get_value] SaaS error: {resp.status_code} - {resp.text[:200]}")
    return None


@jingrow.whitelist()
def get_count(**kwargs):
    """Get count - Forward to SaaS"""
    resp = saas_get('/api/action/jingrow.client.get_count', params=kwargs)
    if resp.status_code == 200:
        return resp.json().get("message", 0)
    logger.error(f"[get_count] SaaS error: {resp.status_code} - {resp.text[:200]}")
    return 0
