import frappe
from custom_ui.db_utils import build_success_response, build_error_response
# ===============================================================================
# CUSTOMER API METHODS
# ===============================================================================

@frappe.whitelist()
def get_customer_details(customer_name):
    try:
        customer = frappe.get_doc("Customer", customer_name)
        return build_success_response(customer)
    except frappe.ValidationError as ve:
        return build_error_response(str(ve), 400)
    except Exception as e:
        return build_error_response(str(e), 500)

@frappe.whitelist()
def get_client_names(type):
    """Get a list of client names. Maps to value/label pairs for select fields."""
    try:
        customer_names = frappe.db.sql("""
                            SELECT
                                customer_name AS label,
                                name AS value
                            FROM
                                `tabCustomer`
                            WHERE
                                customer_type = %s
                            """, (type,), as_dict=True)
        return build_success_response(customer_names)
    except Exception as e:
        return build_error_response(str(e), 500)
    except frappe.ValidationError as ve:
        return build_error_response(str(ve), 400)