from odoo import models, fields, api
import base64
import xml.etree.ElementTree as ET
from html import unescape
import logging
from datetime import datetime
import re

_logger = logging.getLogger(__name__)

class CrmLead(models.Model):
    _inherit = "crm.lead"

    # Lead Classification Fields
    x_lead_type = fields.Selection([
        ('carfax', 'Carfax'),
        ('facebook', 'Facebook'),
        ('podium', 'Podium'),
        ('vehicle_inquiry', 'Vehicle Inquiry'),
        ('credit_app', 'Credit Application'),
        ('sms', 'SMS Lead'),
        ('contact_us', 'Contact Form')  # Added Contact Form type
    ], string="Lead Type", tracking=True)

    x_source_id = fields.Many2one(
        'crm.lead.source',
        string="Lead Source Reference",
        help="Reference to the source system that generated this lead."
    )

    # Customer Information Fields
    x_customer_name = fields.Char(string="Customer Name", tracking=True)
    x_preferred_contact = fields.Selection([
        ('voice', 'Phone Call'),
        ('text', 'Text Message'),
        ('email', 'Email'),
        ('unknown', 'Not Specified')
    ], string="Preferred Contact Method", default='unknown', tracking=True)

    # Vehicle Information Fields
    x_vehicle_year = fields.Char(string="Vehicle Year", tracking=True)
    x_vehicle_make = fields.Char(string="Make", tracking=True)
    x_vehicle_model = fields.Char(string="Model", tracking=True)
    x_vehicle_trim = fields.Char(string="Trim", tracking=True)
    x_vehicle_vin = fields.Char(string="VIN", tracking=True)
    x_vehicle_stock = fields.Char(string="Stock Number", tracking=True)
    x_vehicle_price = fields.Char(string="List Price", tracking=True)
    x_vehicle_mileage = fields.Char(string="Mileage", tracking=True)
    x_vehicle_color = fields.Char(string="Exterior Color", tracking=True)

    # Lead Metadata Fields
    x_request_date = fields.Datetime(string="Lead Request Date", tracking=True)
    x_lead_source = fields.Char(string="Original Lead Source", tracking=True)

    # =================================================================
    # CREDIT APPLICATION FIELDS
    # =================================================================
    
    # Applicant Personal Information
    x_first_name = fields.Char(string="First Name", tracking=True)
    x_last_name = fields.Char(string="Last Name", tracking=True) 
    x_ssn = fields.Char(string="SSN", groups="base.group_system", tracking=True)  # Restricted access
    x_date_of_birth = fields.Date(string="Date of Birth", tracking=True)
    x_dl_number = fields.Char(string="DL Number", tracking=True)
    x_dl_state = fields.Char(string="DL State", tracking=True)
    x_dl_exp_date = fields.Date(string="DL Expiration", tracking=True)
    
    # Applicant Address & Housing
    x_address = fields.Char(string="Address", tracking=True)
    x_city = fields.Char(string="City", tracking=True)
    x_state = fields.Char(string="State", tracking=True)
    x_zip = fields.Char(string="ZIP Code", tracking=True)
    x_residence_type = fields.Selection([
        ('rent', 'Rent'),
        ('own', 'Own'),
        ('other', 'Other')
    ], string="Residence Type", tracking=True)
    x_time_at_residence = fields.Char(string="Time at Residence", tracking=True)
    x_rent_mortgage = fields.Float(string="Rent/Mortgage Payment", tracking=True)
    x_landlord_name = fields.Char(string="Landlord Name", tracking=True)
    x_bank_name = fields.Char(string="Bank/Credit Union", tracking=True)
    
    # Applicant Contact
    x_home_phone = fields.Char(string="Home Phone", tracking=True)
    x_mobile_phone = fields.Char(string="Mobile Phone", tracking=True)
    x_email_address = fields.Char(string="Email Address", tracking=True)
    
    # Applicant Employment
    x_employer = fields.Char(string="Employer", tracking=True)
    x_employer_address = fields.Text(string="Employer Address", tracking=True)
    x_employer_city = fields.Char(string="Employer City", tracking=True)
    x_monthly_income = fields.Float(string="Stated Income", tracking=True)  # Renamed from Monthly Income
    x_hours_per_week = fields.Float(string="Hours per Week", tracking=True)
    x_hourly_pay = fields.Float(string="Hourly Pay", tracking=True)
    x_regular_income = fields.Float(string="Regular Income", compute="_compute_calculated_income", store=True, tracking=True)
    x_overtime_income = fields.Float(string="Overtime Income", compute="_compute_calculated_income", store=True, tracking=True)
    x_calculated_income = fields.Float(string="Total Calculated Income", compute="_compute_calculated_income", store=True, tracking=True)
    x_occupation = fields.Char(string="Occupation", tracking=True) 
    x_work_phone = fields.Char(string="Work Phone", tracking=True)
    x_time_on_job = fields.Char(string="Time on Job", tracking=True)
    
    # Co-Applicant Personal Information
    x_co_applicant_first_name = fields.Char(string="Co-Applicant First Name", tracking=True)
    x_co_applicant_middle_name = fields.Char(string="Co-Applicant Middle Name", tracking=True)
    x_co_applicant_last_name = fields.Char(string="Co-Applicant Last Name", tracking=True)
    x_co_applicant_ssn = fields.Char(string="Co-Applicant SSN", groups="base.group_system", tracking=True)
    x_co_applicant_dob = fields.Date(string="Co-Applicant DOB", tracking=True)
    x_co_applicant_dl_number = fields.Char(string="Co-Applicant DL Number", tracking=True)
    x_co_applicant_dl_state = fields.Char(string="Co-Applicant DL State", tracking=True)
    x_co_applicant_dl_exp_date = fields.Date(string="Co-Applicant DL Exp", tracking=True)
    
    # Co-Applicant Address & Housing  
    x_co_applicant_address = fields.Char(string="Co-Applicant Address", tracking=True)
    x_co_applicant_city = fields.Char(string="Co-Applicant City", tracking=True)
    x_co_applicant_state = fields.Char(string="Co-Applicant State", tracking=True)
    x_co_applicant_zip = fields.Char(string="Co-Applicant ZIP", tracking=True)
    x_co_applicant_residence_type = fields.Selection([
        ('rent', 'Rent'),
        ('own', 'Own'),
        ('other', 'Other')
    ], string="Co-Applicant Residence Type", tracking=True)
    x_co_applicant_time_at_residence = fields.Char(string="Co-Applicant Time at Residence", tracking=True)
    x_co_applicant_rent_mortgage = fields.Float(string="Co-Applicant Rent/Mortgage", tracking=True)
    
    # Co-Applicant Contact
    x_co_applicant_home_phone = fields.Char(string="Co-Applicant Home Phone", tracking=True)
    x_co_applicant_mobile_phone = fields.Char(string="Co-Applicant Mobile Phone", tracking=True)
    x_co_applicant_email = fields.Char(string="Co-Applicant Email", tracking=True)
    
    # Co-Applicant Employment
    x_co_applicant_employer = fields.Char(string="Co-Applicant Employer", tracking=True)
    x_co_applicant_employer_address = fields.Text(string="Co-Applicant Employer Address", tracking=True)
    x_co_applicant_employer_city = fields.Char(string="Co-Applicant Employer City", tracking=True)
    x_co_applicant_monthly_income = fields.Float(string="Co-Applicant Stated Income", tracking=True)  # Renamed
    x_co_applicant_hours_per_week = fields.Float(string="Co-Applicant Hours per Week", tracking=True)
    x_co_applicant_hourly_pay = fields.Float(string="Co-Applicant Hourly Pay", tracking=True)
    x_co_applicant_regular_income = fields.Float(string="Co-Applicant Regular Income", compute="_compute_co_calculated_income", store=True, tracking=True)
    x_co_applicant_overtime_income = fields.Float(string="Co-Applicant Overtime Income", compute="_compute_co_calculated_income", store=True, tracking=True)
    x_co_applicant_calculated_income = fields.Float(string="Co-Applicant Total Calculated Income", compute="_compute_co_calculated_income", store=True, tracking=True)
    x_co_applicant_occupation = fields.Char(string="Co-Applicant Occupation", tracking=True)
    x_co_applicant_work_phone = fields.Char(string="Co-Applicant Work Phone", tracking=True)
    x_co_applicant_time_on_job = fields.Char(string="Co-Applicant Time on Job", tracking=True)
    
    # Enhanced Vehicle Information (beyond existing fields)
    x_vehicle_sale_price = fields.Float(string="Vehicle Sale Price", tracking=True)  # vs list price
    x_vehicle_down_total = fields.Float(string="Down Payment Total", tracking=True)
    x_desired_down_payment = fields.Float(string="Desired Down Payment", tracking=True)
    
    # Additional Credit Application Metadata
    x_credit_app_processed = fields.Boolean(string="Credit App Processed", default=False, tracking=True)
    x_credit_app_date = fields.Datetime(string="Credit App Process Date", tracking=True)

    @api.depends('x_hours_per_week', 'x_hourly_pay')
    def _compute_calculated_income(self):
        """Calculate monthly income from hours per week and hourly pay with overtime"""
        for record in self:
            if record.x_hours_per_week and record.x_hourly_pay:
                # Regular hours (max 40) * 4.3 weeks per month * hourly rate
                regular_hours = min(record.x_hours_per_week, 40)
                record.x_regular_income = regular_hours * 4.3 * record.x_hourly_pay
                
                # Overtime hours (anything over 40) * 4.3 weeks per month * hourly rate * 1.5
                overtime_hours = max(record.x_hours_per_week - 40, 0)
                record.x_overtime_income = overtime_hours * 4.3 * record.x_hourly_pay * 1.5
                
                # Total = Regular + Overtime
                record.x_calculated_income = record.x_regular_income + record.x_overtime_income
            else:
                record.x_regular_income = 0.0
                record.x_overtime_income = 0.0
                record.x_calculated_income = 0.0

    @api.depends('x_co_applicant_hours_per_week', 'x_co_applicant_hourly_pay')
    def _compute_co_calculated_income(self):
        """Calculate co-applicant monthly income from hours per week and hourly pay with overtime"""
        for record in self:
            if record.x_co_applicant_hours_per_week and record.x_co_applicant_hourly_pay:
                # Regular hours (max 40) * 4.3 weeks per month * hourly rate
                regular_hours = min(record.x_co_applicant_hours_per_week, 40)
                record.x_co_applicant_regular_income = regular_hours * 4.3 * record.x_co_applicant_hourly_pay
                
                # Overtime hours (anything over 40) * 4.3 weeks per month * hourly rate * 1.5
                overtime_hours = max(record.x_co_applicant_hours_per_week - 40, 0)
                record.x_co_applicant_overtime_income = overtime_hours * 4.3 * record.x_co_applicant_hourly_pay * 1.5
                
                # Total = Regular + Overtime
                record.x_co_applicant_calculated_income = record.x_co_applicant_regular_income + record.x_co_applicant_overtime_income
            else:
                record.x_co_applicant_regular_income = 0.0
                record.x_co_applicant_overtime_income = 0.0
                record.x_co_applicant_calculated_income = 0.0

    def _clean_phone_number(self, phone):
        if not phone:
            return ''
        cleaned = re.sub(r'[^\d]', '', phone)
        if len(cleaned) > 10 and cleaned.startswith('1'):
            cleaned = cleaned[1:]
        if len(cleaned) >= 10:
            return f"{cleaned[:3]}-{cleaned[3:6]}-{cleaned[6:10]}"
        return cleaned

    def _extract_phone_from_comments(self, comments):
        if not comments:
            return None
        phone_pattern = r'\+?\d{0,1}[-\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}'
        found = re.findall(phone_pattern, comments)
        if found:
            return self._clean_phone_number(found[0])
        return None

    def _extract_pdf_credit_app_data(self, pdf_data):
        """Extract credit application data from PDF"""
        try:
            from PyPDF2 import PdfReader
            import io

            pdf_file = io.BytesIO(pdf_data)
            reader = PdfReader(pdf_file)
            text = ""
            for page in reader.pages:
                text += page.extract_text()

            # Initialize data dictionary
            data = {
                'customer': {
                    'first_name': '',
                    'last_name': '',
                    'phone': '',
                    'email': '',
                    'address': '',
                    'city': '',
                    'state': '',
                    'zip': ''
                },
                'vehicle': {
                    'year': '',
                    'make': '',
                    'model': '',
                    'vin': '',
                    'mileage': '',
                    'price': ''
                },
                'employment': {
                    'employer': '',
                    'position': '',
                    'income': '',
                    'length': ''
                }
            }

            # Extract data from text
            lines = text.split('\n')
            for i, line in enumerate(lines):
                if 'Last Name:' in line:
                    name_parts = line.split('First Name:')
                    if len(name_parts) > 1:
                        data['customer']['last_name'] = name_parts[0].replace('Last Name:', '').strip()
                        data['customer']['first_name'] = name_parts[1].split('MI:')[0].strip()
                elif 'Home Phone:' in line:
                    data['customer']['phone'] = line.split('Home Phone:')[1].strip()
                elif 'Email Address:' in line and 'Name of Bank' in line:
                    data['customer']['email'] = line.split('Email Address:')[1].split('Name of Bank')[0].strip()
                elif 'Current Address:' in line:
                    data['customer']['address'] = line.split('Current Address:')[1].split('Cell Phone:')[0].strip()
                elif 'City:' in line and 'State:' in line:
                    data['customer']['city'] = line.split('City:')[1].split('State:')[0].strip()
                    state_zip = line.split('State:')[1].split('ZIP Code:')
                    if len(state_zip) > 1:
                        data['customer']['state'] = state_zip[0].strip()
                        data['customer']['zip'] = state_zip[1].strip()

                # Vehicle information
                elif 'Year:' in line and 'Make:' in line and 'Model:' in line:
                    data['vehicle']['year'] = line.split('Year:')[1].split('Make:')[0].strip()
                    data['vehicle']['make'] = line.split('Make:')[1].split('Model:')[0].strip()
                    model_parts = line.split('Model:')[1].split('Mileage:')
                    if len(model_parts) > 1:
                        data['vehicle']['model'] = model_parts[0].strip()
                        data['vehicle']['mileage'] = model_parts[1].strip()
                elif 'VIN:' in line:
                    data['vehicle']['vin'] = line.split('VIN:')[1].strip()
                elif 'Cash Price' in line and '$' in line:
                    data['vehicle']['price'] = line.split('$')[1].strip()

                # Employment information
                elif 'Current employer:' in line:
                    data['employment']['employer'] = line.split('Current employer:')[1].split('How long?')[0].strip()
                elif 'Position:' in line and 'Income:' in line:
                    data['employment']['position'] = line.split('Position:')[1].split('Income:')[0].strip()
                    income_parts = line.split('Income:')[1].split('/Month')
                    if len(income_parts) > 0:
                        data['employment']['income'] = income_parts[0].replace('$', '').strip()

            return data
        except Exception as e:
            _logger.error(f"Error extracting PDF data: {str(e)}")
            return None

    def _process_pdf_credit_app(self, pdf_data):
        """Process PDF credit application and create lead"""
        try:
            data = self._extract_pdf_credit_app_data(pdf_data)
            if not data:
                return False
            
            # Format the lead name (vehicle only)
            lead_name = f"{data['vehicle']['year']} {data['vehicle']['make']} {data['vehicle']['model']}"
            
            # Create the lead values
            values = {
                'name': lead_name,
                'x_lead_type': 'credit_app',
                'contact_name': f"{data['customer']['first_name']} {data['customer']['last_name']}",
                'email_from': data['customer']['email'],
                'phone': self._clean_phone_number(data['customer']['phone']),
                'street': data['customer']['address'],
                'city': data['customer']['city'],
                'state_id': self.env['res.country.state'].search([
                    ('code', '=', data['customer']['state']),
                    ('country_id.code', '=', 'US')
                ], limit=1).id,
                'zip': data['customer']['zip'],
                'x_vehicle_year': data['vehicle']['year'],
                'x_vehicle_make': data['vehicle']['make'],
                'x_vehicle_model': data['vehicle']['model'],
                'x_vehicle_vin': data['vehicle']['vin'],
                'x_vehicle_mileage': data['vehicle']['mileage'],
                'x_vehicle_price': data['vehicle']['price'],
                'description': f"""
Credit Application Details:

Employment Information:
- Employer: {data['employment']['employer']}
- Position: {data['employment']['position']}
- Monthly Income: ${data['employment']['income']}

Vehicle Information:
- Year: {data['vehicle']['year']}
- Make: {data['vehicle']['make']}
- Model: {data['vehicle']['model']}
- VIN: {data['vehicle']['vin']}
- Mileage: {data['vehicle']['mileage']}
- Price: ${data['vehicle']['price']}
"""
            }
            
            # Get the Online Application tag
            tag = self.env['crm.tag'].search([('name', '=', 'Online App')], limit=1)
            if tag:
                values['tag_ids'] = [(4, tag.id)]
            
            return values
            
        except Exception as e:
            _logger.error(f"Error processing PDF credit application: {str(e)}")
            return None

    @api.model
    def message_new(self, msg_dict, custom_values=None):
        _logger.info("======= DEBUG =======")
        _logger.info(f"Message Keys: {msg_dict.keys()}")
        _logger.info(f"Message Type: {type(msg_dict)}")
        _logger.info(f"Custom Values: {custom_values}")
        _logger.info(f"Body present: {'body' in msg_dict}")
        _logger.info("====================")

        if custom_values is None:
            custom_values = {}

        # Check for PDF attachments
        attachments = msg_dict.get('attachments', [])
        for attachment in attachments:
            if attachment and len(attachment) == 2:
                filename, content = attachment
                if filename.lower().endswith('.pdf'):
                    try:
                        pdf_values = self._process_pdf_credit_app(content)
                        if pdf_values:
                            custom_values.update(pdf_values)
                            return super(CrmLead, self).message_new(msg_dict, custom_values)
                    except Exception as e:
                        _logger.error(f"Error processing PDF attachment: {str(e)}")

        # Create base lead
        lead = super(CrmLead, self).message_new(msg_dict, custom_values)

        try:
            email_body = msg_dict.get('body', '')
            _logger.info(f"Email body found: {bool(email_body)}")
            if email_body:
                decoded_body = unescape(email_body)
                adf_content = self._extract_adf_from_html(decoded_body)
                if adf_content:
                    self._process_adf_xml(adf_content.encode('utf-8'), lead.id)
        except Exception as e:
            _logger.error(f"Error in message_new: {str(e)}")
    
        return lead

    def _extract_adf_from_html(self, html_body):
        try:
            _logger.info("Attempting to extract ADF from HTML")
            start_index = html_body.lower().find('<?xml')
            if start_index == -1:
                start_index = html_body.lower().find('<?adf')
            if start_index == -1:
                return None

            end_index = html_body.lower().find('</adf>', start_index)
            if end_index == -1:
                return None

            adf_content = html_body[start_index:end_index + 6]
            _logger.info("Successfully extracted ADF content")
            return unescape(adf_content).strip()
        except Exception as e:
            _logger.error(f"Error extracting ADF from HTML: {str(e)}")
            return None

    def _determine_lead_type(self, service_name, provider_name=None):
        provider_name = (provider_name or '').lower()
        service_name = (service_name or '').lower()
    
        if 'carfax' in provider_name or 'carfax' in service_name:
            return 'carfax'
        elif 'apply for credit' in service_name:
            return 'credit_app'
        elif 'facebook messenger' in service_name:
            return 'facebook'
        elif provider_name and 'podium' in provider_name:
            return 'podium'
        elif 'price drop' in service_name.lower():
            return 'vehicle_inquiry'  # Classify Price Drop Form as vehicle inquiry
        elif 'contact us' in service_name.lower() or 'contact form' in service_name.lower():
            return 'contact_us'  # New classification for Contact Us forms
        elif 'vehicle inquiry' in service_name:
            return 'vehicle_inquiry'
        elif service_name == 'sms':  # Only classify as SMS when explicitly SMS
            return 'sms'
        elif provider_name and 'dealer car search' in provider_name:
            # For other Dealer Car Search leads, default to vehicle inquiry
            return 'vehicle_inquiry'
        elif 'carsforsale' in provider_name.replace(' ', '') or 'carsforsale' in service_name.replace(' ', ''):
            return 'vehicle_inquiry'
        return False

    def _format_name(self, first, last):
        first = first or ''
        last = last or ''
        return f"{first} {last}".strip() or "Unknown"

    def _format_vehicle_info(self, vehicle_data, include_options=False):
        info = []
        base_info = f"{vehicle_data['year']} {vehicle_data['make']} {vehicle_data['model']}"
        if vehicle_data.get('trim'):
            base_info += f" {vehicle_data['trim']}"
        info.append(base_info)
        
        if vehicle_data.get('mileage'):
            info.append(f"Mileage: {vehicle_data['mileage']}")
        if vehicle_data.get('price'):
            info.append(f"Price: ${vehicle_data['price']}")
        
        return "\n".join(info)

    def _extract_listing_url(self, comments):
        if not comments:
            return None
        
        if comments.startswith('<![CDATA['):
            comments = comments[9:-3]  # Strip CDATA tags
            
        for line in comments.split('\n'):
            line = line.strip()
            if 'carfax.com/vehicle/' in line:
                return line.strip()
            if line.startswith('Listing:'):
                return line.replace('Listing:', '').strip()
        return None

    def _format_contact_us_description(self, comments):
        """Format contact us form description in a more readable way"""
        sections = []
        
        # Replace HTML breaks with newlines and clean up
        comments = comments.replace('&lt;br&gt;', '\n').replace('<br>', '\n')
        
        # Add header
        sections.append("Contact Form Inquiry")
        
        # Extract contact info and comments sections if they exist
        contact_info = ""
        customer_message = ""
        
        if "Contact Information:" in comments and "Comments:" in comments:
            parts = comments.split("Comments:", 1)
            contact_info = parts[0].replace("Contact Information:", "").strip()
            customer_message = parts[1].strip()
        else:
            # If not structured, just use the whole thing as message
            customer_message = comments
        
        # Add cleaned up sections
        if contact_info:
            sections.append("Contact Information:")
            sections.append(contact_info)
        
        sections.append("Customer Message:")
        sections.append(customer_message)
        
        return "\n\n".join(sections)

    def _format_description(self, lead_type, comments, vehicle_info=None, listing_url=None, provider_name=None):
        sections = []
        
        # Handle CDATA sections in comments
        if comments and comments.startswith('<![CDATA['):
            comments = comments[9:-3].strip()
        
        if lead_type == 'facebook':
            if ')' in comments:
                name_part, message = comments.split(')', 1)
                sections.append("Facebook Messenger Inquiry:")
                sections.append(message.strip())
                
        elif lead_type == 'carfax':
            sections.append("Customer Inquiry")
            if comments:
                message_parts = comments.split('Listing:')
                customer_message = message_parts[0].strip()
                sections.append(f"Message: {customer_message}")
            
            if vehicle_info:
                sections.append("Vehicle Information:")
                sections.append(vehicle_info)
                
            # Extract and format listing URL more cleanly
            if comments and 'Listing:' in comments:
                listing_parts = comments.split('Listing:')
                if len(listing_parts) > 1:
                    listing_url = listing_parts[1].strip().split('\n')[0].strip()
                    sections.append("Carfax Listing:")
                    sections.append(listing_url)
            
            # Add lead source info if available
            if 'Lead provided by' in comments:
                source_parts = comments.split('Lead provided by')
                if len(source_parts) > 1:
                    lead_source = source_parts[1].strip()
                    sections.append(f"Source: {lead_source}")
                
        elif lead_type == 'credit_app':
            sections.append("Credit Application")
            if vehicle_info:
                sections.append("\nVehicle Information:")
                sections.append(vehicle_info)
            if comments:
                sections.append("\nCustomer Comments:")
                sections.append(comments)
                
        elif lead_type == 'podium':
            if 'Messages:' in comments:
                message = comments.split('Messages:', 1)[1].split('Link:', 1)[0].strip()
                sections.append("Customer Message:")
                sections.append(message)
        
        elif lead_type == 'contact_us':
            return self._format_contact_us_description(comments)
                
        elif lead_type == 'vehicle_inquiry':
            # Check the provider name instead of message content
            provider_display = 'Vehicle Inquiry'
            if provider_name and 'carsforsale' in provider_name.lower().replace(' ', ''):
                provider_display = 'Cars For Sale Inquiry'
            
            sections.append(f"{provider_display}:")
            if comments:
                sections.append(comments)
            if vehicle_info:
                sections.append("\nVehicle Information:")
                sections.append(vehicle_info)
            if listing_url:
                sections.append("\nListing URL:")
                sections.append(listing_url)
                
        elif lead_type == 'sms':
            sections.append("SMS Inquiry")
            if comments:
                sections.append(f"Message: {comments}")
            if vehicle_info:
                sections.append("Vehicle Information:")
                sections.append(vehicle_info)
            if listing_url:
                sections.append("Listing URL:")
                sections.append(listing_url)
                
        return "\n\n".join(sections)

    def _process_vehicle_data(self, vehicle):
        if vehicle is None:
            return {}
           
        data = {
            'year': vehicle.findtext('year', ''),
            'make': vehicle.findtext('make', ''),
            'model': vehicle.findtext('model', ''),
            'trim': vehicle.findtext('trim', ''),
            'vin': vehicle.findtext('vin', ''),
            'stock': vehicle.findtext('stock', ''),
            'price': '',
            'mileage': '',
            'color': ''
        }
        
        # Handle different formats for price
        price_elem = vehicle.find('.//price')
        if price_elem is not None and price_elem.text:
            data['price'] = price_elem.text.replace('$', '').replace(',', '')
        
        # Handle different formats for mileage
        odometer = vehicle.find('odometer')
        if odometer is not None:
            data['mileage'] = f"{odometer.text} {odometer.get('units', '')}"
        else:
            miles = vehicle.findtext('miles', '')
            if miles:
                data['mileage'] = f"{miles} mi"
        
        # Check for exterior color
        color_elem = vehicle.find('.//exteriorcolor')
        if color_elem is not None and color_elem.text:
            data['color'] = color_elem.text
                
        return data

    def _process_adf_xml(self, adf_data, lead_id):
        try:
            _logger.info(f"Starting ADF processing for lead ID: {lead_id}")

            if isinstance(adf_data, bytes):
                adf_data = adf_data.decode('utf-8', errors='ignore')
            
            if '<?ADF' in adf_data:
                adf_data = adf_data[adf_data.find('<?xml'):]

            root = ET.fromstring(adf_data.strip())
            
            request_date_str = root.findtext('.//requestdate')
            request_date = False
            if request_date_str:
                try:
                    dt = datetime.fromisoformat(request_date_str.replace('Z', '+00:00'))
                    request_date = dt.replace(tzinfo=None)
                except Exception as e:
                    _logger.error(f"Error parsing date: {e}")

            vehicle = root.find('.//vehicle')
            vehicle_data = self._process_vehicle_data(vehicle)
            
            customer = root.find('.//customer/contact')
            if customer is not None:
                _logger.info("==== Customer Debug ====")
                _logger.info(f"Raw customer data: {ET.tostring(customer)}")
                first_name = customer.find('name[@part="first"]').text if customer.find('name[@part="first"]') is not None else ''
                last_name = customer.find('name[@part="last"]').text if customer.find('name[@part="last"]') is not None else ''
                _logger.info(f"First: {first_name}, Last: {last_name}")
                customer_name = self._format_name(first_name, last_name)
                _logger.info(f"Formatted name: {customer_name}")
                email = customer.findtext('email', '')
                phone = customer.findtext('.//phone[@type="voice"]', '') or customer.findtext('.//phone', '')
                
                # Look for cellphone specifically for SMS leads
                if not phone or phone == 'N/A':
                    phone = customer.findtext('.//phone[@type="cellphone"]', '')
                    
                email = '' if not email or email == 'N/A' else email
                phone = self._clean_phone_number(phone) if phone and phone != 'N/A' else ''
                
                address = customer.find('address')
                if address is not None:
                    street = address.findtext('street', '')
                    city = address.findtext('city', '')
                    state_code = address.findtext('regioncode', '')
                    zip_code = address.findtext('postalcode', '')
                    state_id = False
                    if state_code:
                        state = self.env['res.country.state'].search([
                            ('code', '=', state_code),
                            ('country_id.code', '=', 'US')
                        ], limit=1)
                        if state:
                            state_id = state.id
            else:
                _logger.error("No customer contact found in ADF!")
                customer_name = "Unknown"
                email = ''
                phone = ''

            provider = root.find('.//provider')
            provider_name = provider.findtext('name') if provider is not None else ''
            service = root.find('.//provider/service')
            service_name = service.text if service is not None else ''
            if not service_name:
                lead_type_elem = root.find('.//leadtype')
                service_name = lead_type_elem.text if lead_type_elem is not None else ''

            lead_type = self._determine_lead_type(service_name, provider_name)
            _logger.info(f"Service name: '{service_name}', Provider name: '{provider_name}', Determined lead type: {lead_type}")

            comments = root.find('.//customer/comments')
            comments_text = comments.text.strip() if comments is not None and comments.text else ''
            
            if not phone and lead_type in ['facebook', 'podium', 'sms']:
                extracted_phone = self._extract_phone_from_comments(comments_text)
                if extracted_phone:
                    phone = extracted_phone

            # Generate lead name and get appropriate tag
            provider_display = ''
            if provider_name and 'carsforsale' in provider_name.replace(' ', '').lower():
                provider_display = 'Cars For Sale'
            elif provider_name:
                provider_display = provider_name
                
            if lead_type == 'facebook':
                lead_name = f"Facebook Messenger - {customer_name}"
                tag_name = 'Contact Request'
            elif lead_type == 'carfax':
                lead_name = f"{vehicle_data['year']} {vehicle_data['make']} {vehicle_data['model']} {vehicle_data.get('trim', '')}"
                tag_name = 'Vehicle Inquiry'
            elif lead_type == 'credit_app':
                lead_name = f"{vehicle_data['year']} {vehicle_data['make']} {vehicle_data['model']} {vehicle_data.get('trim', '')}"
                tag_name = 'Online App'
            elif lead_type == 'contact_us':
                # Special handling for Contact Us forms
                lead_name = f"Contact Form Inquiry - {customer_name}"
                tag_name = 'Contact Request'
            elif service_name and 'price drop' in service_name.lower():
                # Special handling for Price Drop forms
                stock_info = f" (Stock #{vehicle_data['stock']})" if vehicle_data.get('stock') else ""
                lead_name = f"Price Drop Notification - {vehicle_data['year']} {vehicle_data['make']} {vehicle_data['model']}"
                if vehicle_data.get('trim'):
                    lead_name += f" {vehicle_data['trim']}"
                lead_name += stock_info
                tag_name = 'Vehicle Inquiry'
            elif lead_type == 'sms':
                # Removed customer name from SMS lead titles
                lead_name = f"SMS - {vehicle_data['year']} {vehicle_data['make']} {vehicle_data['model']}"
                if vehicle_data.get('trim'):
                    lead_name += f" {vehicle_data['trim']}"
                tag_name = 'Vehicle Inquiry'
            elif lead_type == 'vehicle_inquiry':
                if provider_display:
                    lead_name = f"{provider_display} - {vehicle_data['year']} {vehicle_data['make']} {vehicle_data['model']}"
                else:
                    lead_name = f"Vehicle Inquiry - {vehicle_data['year']} {vehicle_data['make']} {vehicle_data['model']}"
                if vehicle_data.get('trim'):
                    lead_name += f" {vehicle_data['trim']}"
                tag_name = 'Vehicle Inquiry'
            else:
                lead_name = f"{service_name} - {vehicle_data['year']} {vehicle_data['make']} {vehicle_data['model']}"
                if vehicle_data.get('trim'):
                    lead_name += f" {vehicle_data['trim']}"
                tag_name = 'Vehicle Inquiry'

            tag = self.env['crm.tag'].search([('name', '=', tag_name)], limit=1)

            vehicle_info = self._format_vehicle_info(vehicle_data)
            listing_url = self._extract_listing_url(comments_text)
            formatted_description = self._format_description(lead_type, comments_text, vehicle_info, listing_url, provider_name)
            
            # Add color information to description if available
            if vehicle_data.get('color'):
                formatted_description += f"\n\nExterior Color: {vehicle_data.get('color')}"

            lead = self.env['crm.lead'].browse(lead_id)
            _logger.info(f"About to update lead with: name='{lead_name}', customer_name='{customer_name}', email='{email}', phone='{phone}'")
            values = {
                'name': lead_name,
                'x_customer_name': customer_name,
                'contact_name': customer_name,
                'partner_id': False,
                'email_from': email if email != 'N/A' else '',
                'phone': self._clean_phone_number(phone) if phone != 'N/A' else '',
                'description': formatted_description,
                'x_vehicle_year': vehicle_data.get('year'),
                'x_vehicle_make': vehicle_data.get('make'),
                'x_vehicle_model': vehicle_data.get('model'),
                'x_vehicle_trim': vehicle_data.get('trim'),
                'x_vehicle_vin': vehicle_data.get('vin'),
                'x_vehicle_stock': vehicle_data.get('stock'),
                'x_vehicle_price': vehicle_data.get('price'),
                'x_vehicle_mileage': vehicle_data.get('mileage'),
                'x_vehicle_color': vehicle_data.get('color', ''),
                'x_request_date': request_date,
                'x_lead_source': service_name,
                'x_lead_type': lead_type,
                'x_preferred_contact': 'text' if lead_type == 'sms' else 'voice',
                'medium_id': self.env.ref('utm.utm_medium_email').id,
                'source_id': self.env['utm.source'].search([('name', '=', service_name)], limit=1).id or self.env['utm.source'].create({'name': service_name}).id,
                'tag_ids': [(4, tag.id)] if tag else [],
            }

            if 'city' in locals():
                values.update({
                    'city': city,
                    'state_id': state_id,
                    'zip': zip_code,
                })

            lead.write(values)
            _logger.info(f"Successfully processed {lead_type} lead: {lead_name}")

        except Exception as e:
            _logger.error(f"Error processing ADF XML: {str(e)}")
            _logger.error(f"Raw ADF data: {adf_data}")


class CrmLeadSource(models.Model):
    _name = "crm.lead.source"
    _description = "Lead Source"

    name = fields.Char(string="Source Name", required=True)
    active = fields.Boolean(default=True)