import frappe

class OnSiteMeetingService:
    
    @staticmethod
    def exists(onsite_meeting_name: str) -> bool:
        """Check if an OnSite Meeting document exists by name."""
        result = frappe.db.exists("OnSite Meeting", onsite_meeting_name) is not None
        print(f"DEBUG: OnSite Meeting existence for {onsite_meeting_name}: {result}")
        return result
    
    @staticmethod
    def get(onsite_meeting_name: str) -> frappe._dict:
        """Retrieve an OnSite Meeting document by name. Returns None if not found."""
        print(f"DEBUG: Retrieving OnSite Meeting document with name: {onsite_meeting_name}")
        if OnSiteMeetingService.exists(onsite_meeting_name):
            onsite_meeting_doc = frappe.get_doc("OnSite Meeting", onsite_meeting_name)
            print("DEBUG: OnSite Meeting document found.")
            return onsite_meeting_doc
        print("DEBUG: OnSite Meeting document not found.")
        return None
    
    @staticmethod
    def get_or_throw(onsite_meeting_name: str) -> frappe._dict:
        """Retrieve an OnSite Meeting document or throw an error if not found."""
        onsite_meeting_doc = OnSiteMeetingService.get(onsite_meeting_name)
        if not onsite_meeting_doc:
            raise ValueError(f"OnSite Meeting with name {onsite_meeting_name} does not exist.")
        return onsite_meeting_doc
    
    @staticmethod
    def link_onsite_meeting_to_customer(onsite_meeting_doc, customer_type, customer_name):
        """Link an onsite meeting to a customer or lead."""
        print(f"DEBUG: Linking Onsite Meeting {onsite_meeting_doc.name} to {customer_type} {customer_name}")
        onsite_meeting_doc.party_type = customer_type
        onsite_meeting_doc.party_name = customer_name
        onsite_meeting_doc.save(ignore_permissions=True)
        print(f"DEBUG: Linked Onsite Meeting {onsite_meeting_doc.name} to {customer_type} {customer_name}")