import frappe

@frappe.whitelist()
def get_records():
    # For now return Lead records mapped to form fields
    leads = frappe.db.sql("""
        SELECT name, company_name, email_id, mobile_no, phone, address_line1, city, state, pincode,
               title, status, industry, source
        FROM tabLead
        ORDER BY creation DESC
        LIMIT 1000
    """, as_dict=True)
    result = []
    for l in leads:
        result.append({
            "name": l.name,
            "additional_numbers": "",
            "field_star": l.status or "",
            "customer_address": l.address_line1 or "",
            "any_letter": l.title or "",
            "city": l.city or "",
            "field_e": l.email_id or "",
            "zip": l.pincode or "",
            "company_name": l.company_name or "",
            "contacted_date": "",
            "contact_persons": l.title or "",
            "follow_up_date": "",
            "email_address": l.email_id or "",
            "last_pu_date": "",
            "contact_numbers": (l.phone or "") + "\n" + (l.mobile_no or ""),
            "hours_operation": l.industry or "",
            "comments": l.source or ""
        })
    return result

@frappe.whitelist()
def save_record(data):
    data = frappe.parse_json(data)
    # For now just return OK - Lead update can be wired later
    return {"status": "ok", "message": "Saved " + (data.get("name") or "")}

@frappe.whitelist()
def delete_record(name):
    # For now return OK
    return {"status": "ok", "message": "Deleted " + name}

@frappe.whitelist()
def search_records(field, value):
    leads = frappe.db.sql("""
        SELECT name, company_name, email_id, mobile_no, phone, address_line1, city, state, pincode,
               title, status, industry, source
        FROM tabLead
        WHERE LOWER(company_name) LIKE %s
           OR LOWER(title) LIKE %s
           OR LOWER(address_line1) LIKE %s
           OR LOWER(city) LIKE %s
           OR LOWER(pincode) LIKE %s
           OR LOWER(email_id) LIKE %s
           OR LOWER(phone) LIKE %s
           OR LOWER(mobile_no) LIKE %s
        ORDER BY creation DESC
        LIMIT 100
    """, tuple(["%" + value + "%"] * 8), as_dict=True)
    result = []
    for l in leads:
        result.append({
            "name": l.name,
            "additional_numbers": "",
            "field_star": l.status or "",
            "customer_address": l.address_line1 or "",
            "any_letter": l.title or "",
            "city": l.city or "",
            "field_e": l.email_id or "",
            "zip": l.pincode or "",
            "company_name": l.company_name or "",
            "contacted_date": "",
            "contact_persons": l.title or "",
            "follow_up_date": "",
            "email_address": l.email_id or "",
            "last_pu_date": "",
            "contact_numbers": (l.phone or "") + "\n" + (l.mobile_no or ""),
            "hours_operation": l.industry or "",
            "comments": l.source or ""
        })
    return result
