import frappe
import json

def run():
    print("=== CRM Import Status Check ===")
    print("Customer Record DocType exists:", bool(frappe.db.exists("DocType", "Customer Record")))

    if not frappe.db.exists("DocType", "Customer Record"):
        print("Customer Record DocType does not exist! Creating it...")
        doctype_def = {
            "name": "Customer Record",
            "doctype": "DocType",
            "module": "Westech R2",
            "custom": 0,
            "autoname": "field:record_number",
            "naming_rule": "By fieldname",
            "engine": "InnoDB",
            "document_type": "Document",
            "fields": [
                {"fieldname": "record_number", "fieldtype": "Data", "label": "Record Number", "reqd": 1, "unique": 1, "in_list_view": 1},
                {"fieldname": "company_name", "fieldtype": "Data", "label": "Company Name", "in_list_view": 1},
                {"fieldname": "supplier", "fieldtype": "Link", "label": "Supplier", "options": "Supplier", "in_list_view": 1},
                {"fieldname": "customer_number", "fieldtype": "Data", "label": "Customer Number", "in_list_view": 1},
                {"fieldname": "contact_persons", "fieldtype": "Text", "label": "Contact Persons"},
                {"fieldname": "email_address", "fieldtype": "Text", "label": "Email Addresses"},
                {"fieldname": "phone_numbers", "fieldtype": "Text", "label": "Phone Numbers"},
                {"fieldname": "customer_address", "fieldtype": "Data", "label": "Address"},
                {"fieldname": "city", "fieldtype": "Data", "label": "City"},
                {"fieldname": "state", "fieldtype": "Data", "label": "State"},
                {"fieldname": "zip", "fieldtype": "Data", "label": "Zip"},
                {"fieldname": "date_created", "fieldtype": "Date", "label": "Date Created"},
                {"fieldname": "contacted_date", "fieldtype": "Date", "label": "Contacted Date"},
                {"fieldname": "follow_up_date", "fieldtype": "Date", "label": "Follow Up Date"},
                {"fieldname": "last_pu_date", "fieldtype": "Date", "label": "Last PU Date"},
                {"fieldname": "notes", "fieldtype": "Text", "label": "Notes"},
                {"fieldname": "comments", "fieldtype": "Text", "label": "Comments"},
                {"fieldname": "hours_operation", "fieldtype": "Data", "label": "Hours of Operation"},
            ],
            "permissions": [
                {"role": "System Manager", "read": 1, "write": 1, "create": 1, "delete": 1, "export": 1, "print": 1, "share": 1, "email": 1},
                {"role": "All", "read": 1, "write": 1, "create": 1, "delete": 0, "export": 0, "print": 1, "share": 0, "email": 0}
            ],
            "quick_entry": 0,
            "track_changes": 1,
        }
        doc = frappe.get_doc(doctype_def)
        doc.insert(ignore_permissions=True)
        frappe.db.commit()
        print("Customer Record DocType created!")
    
    # Check existing count
    existing = frappe.db.count("Customer Record")
    print(f"Records in Customer Record: {existing}")
    
    if existing > 0:
        print("Data already imported. Skipping.")
        return {"status": "already_done", "count": existing}
    
    # Load parsed data
    with open("/tmp/crm_records.json") as f:
        records = json.load(f)
    
    print(f"Parsed records from xlsx: {len(records)}")
    
    # Get supplier names
    supplier_names = set(frappe.get_all("Supplier", fields=["name"], pluck="name"))
    print(f"Total suppliers: {len(supplier_names)}")
    
    # Count matches
    matched = sum(1 for r in records if r["record_number"] in supplier_names)
    print(f"Records matching suppliers: {matched}")
    
    print(f"Importing {len(records)} records...")
    created = 0
    errors = []
    
    for rec in records:
        try:
            doc = frappe.new_doc("Customer Record")
            doc.record_number = rec["record_number"]
            doc.company_name = rec.get("company_name") or ""
            
            # Match supplier
            if rec["record_number"] in supplier_names:
                doc.supplier = rec["record_number"]
                doc.customer_number = rec["record_number"]
            
            doc.contact_persons = "\n".join(rec.get("contact_persons", []))
            doc.email_address = "\n".join(rec.get("emails", []))
            doc.phone_numbers = "\n".join(rec.get("phone_numbers", []))
            doc.customer_address = rec.get("address") or ""
            doc.city = rec.get("city") or ""
            doc.state = rec.get("state") or ""
            doc.zip = str(rec.get("zip") or "")
            
            if rec.get("date_created"):
                doc.date_created = rec["date_created"]
            if rec.get("contacted_date"):
                doc.contacted_date = rec["contacted_date"]
            if rec.get("follow_up_date"):
                doc.follow_up_date = rec["follow_up_date"]
            if rec.get("last_pu_date"):
                doc.last_pu_date = rec["last_pu_date"]
            
            doc.notes = rec.get("notes") or ""
            doc.comments = rec.get("comments") or ""
            doc.hours_operation = rec.get("hours_operation") or ""
            
            doc.save(ignore_permissions=True)
            created += 1
            
            if created % 500 == 0:
                print(f"  ... {created} records imported")
                frappe.db.commit()
                
        except Exception as e:
            errors.append(f"{rec['record_number']}: {str(e)}")
            if len(errors) > 10:
                print(f"Too many errors, stopping. Last error: {e}")
                break
    
    frappe.db.commit()
    print(f"\nImport complete: {created} records created")
    if errors:
        print(f"Errors ({len(errors)}):")
        for e in errors[:10]:
            print(f"  {e}")
    
    return {"status": "ok", "created": created, "errors": len(errors), "matched": matched}
