import frappe
from custom_ui.services import ContactService, AddressService, ClientService, DbService

class ServiceAppointmentService:
    
    @staticmethod
    def create(data):
        """Create a new Service Appointment document."""
        print("DEBUG: Creating Service Appointment with data:", data)
        service_appointment_doc = frappe.get_doc({
            "doctype": "Service Address 2",
            **data
        })
        service_appointment_doc.insert()
        print("DEBUG: Created Service Appointment with name:", service_appointment_doc.name)
        return service_appointment_doc
    
    @staticmethod
    def get_full_dict(service_appointment_name: str) -> dict:
        """Retrieve a Service Appointment document as a full dictionary."""
        print(f"DEBUG: Retrieving Service Appointment document with name: {service_appointment_name}")
        service_appointment = frappe.get_doc("Service Address 2", service_appointment_name).as_dict()
        service_appointment["service_address"] = AddressService.get_or_throw(service_appointment["service_address"]).as_dict()
        service_appointment["customer"] = ClientService.get_client_or_throw(service_appointment["customer"]).as_dict()
        service_appointment["project"] = DbService.get_or_throw("Project", service_appointment["project"]).as_dict()
        
        return service_appointment
    
    @staticmethod
    def update_scheduled_dates(service_appointment_name: str, crew_lead_name: str,start_date, end_date, start_time=None, end_time=None):
        """Update the scheduled start and end dates of a Service Appointment."""
        print(f"DEBUG: Updating scheduled dates for Service Appointment {service_appointment_name} to start: {start_date}, end: {end_date}")
        service_appointment = DbService.get_or_throw("Service Address 2", service_appointment_name)
        service_appointment.expected_start_date = start_date
        service_appointment.expected_end_date = end_date
        service_appointment.foreman = crew_lead_name
        if start_time:
            service_appointment.expected_start_time = start_time
        if end_time:
            service_appointment.expected_end_time = end_time
        service_appointment.save()
        print(f"DEBUG: Updated scheduled dates for Service Appointment {service_appointment_name}")
        return service_appointment
    
    @staticmethod
    def update_field(service_appointment_name: str, updates: list[tuple[str, any]]):
        """Update specific fields of a Service Appointment."""
        print(f"DEBUG: Updating fields for Service Appointment {service_appointment_name} with updates: {updates}")
        service_appointment = DbService.get_or_throw("Service Address 2", service_appointment_name)
        for field, value in updates:
            setattr(service_appointment, field, value)
        service_appointment.save()
        print(f"DEBUG: Updated fields for Service Appointment {service_appointment_name}")
        return service_appointment
    
    @staticmethod
    def update_status(service_appointment_name: str, new_status: str):
        """Update the status of a Service Appointment."""
        print(f"DEBUG: Updating status for Service Appointment {service_appointment_name} to {new_status}")
        service_appointment = DbService.get_or_throw("Service Address 2", service_appointment_name)
        service_appointment.status = new_status
        service_appointment.save()
        print(f"DEBUG: Updated status for Service Appointment {service_appointment_name} to {new_status}")
        return service_appointment