function generateAppointmentSlots() {
    const result = {
        type: "SELF_SERVICE",
        slots: {}
    };
    
    // Get tomorrow's date
    const today = new Date();
    const tomorrow = new Date(today);
    tomorrow.setDate(today.getDate() + 1);
    
    // Generate dates for the next 45 days starting from tomorrow
    for (let i = 0; i < 63; i++) {
        const currentDate = new Date(tomorrow);
        currentDate.setDate(tomorrow.getDate() + i);
        
        // Format date as YYYY-MM-DD
        const dateString = currentDate.toISOString().split('T')[0];
        
        // Generate time slots from 07:00 to 20:00 with 30-minute intervals
        const timeSlots = [];
        
        for (let hour = 7; hour <= 20; hour++) {
            for (let minute = 0; minute < 60; minute += 15) {
                // Stop at 20:00 (don't include 20:30)
                if (hour === 20 && minute > 0) break;
                
                // Format time as HH:mm
                const timeString = `${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
                
                // Create time slot object
                const standardTimeSlot = {
                    [timeString]: [""]
                };
                const primeTimeSlot = {
                    [timeString]: ["pta"]
                };
                
                timeSlots.push(standardTimeSlot);
                timeSlots.push(primeTimeSlot);
            }
        }
        
        result.slots[dateString] = timeSlots;
    }
    
    return JSON.stringify(result);
}


(function() {
    console.log('TLSContact Interceptor script injected.');

    // Global flag to control interception
    window.tlsInterceptorEnabled = true;

    // Keep a reference to the original XMLHttpRequest
    const OriginalXMLHttpRequest = window.XMLHttpRequest;

    // Override the XMLHttpRequest
    window.XMLHttpRequest = function() {
        const xhr = new OriginalXMLHttpRequest();
        let url = '';
        let method = '';
        let shouldIntercept = false;

        // Store the URL and method when open is called
        const originalOpen = xhr.open;
        xhr.open = function(method_, url_) {
            url = url_;
            method = method_;
            // Check if interceptor is enabled AND the URL matches
            if (window.tlsInterceptorEnabled && 
                method.toUpperCase() === 'GET' && 
                (url.includes('bookings/available-slots') || url.includes('labels'))) {
                shouldIntercept = true;
                console.log('Intercepting request:', url);
            } else {
                shouldIntercept = false;
            }
            return originalOpen.apply(this, arguments);
        };

        // Intercept the send method if needed
        const originalSend = xhr.send;
        xhr.send = function() {
            if (shouldIntercept && window.tlsInterceptorEnabled) {
                console.log('Intercepted send for URL:', url);

                const jsonData = generateAppointmentSlots();

                // Manually trigger the onload event
                Object.defineProperty(xhr, 'responseText', { value: jsonData, writable: true });
                Object.defineProperty(xhr, 'response', { value: jsonData, writable: true });
                Object.defineProperty(xhr, 'status', { value: 200, writable: true });
                Object.defineProperty(xhr, 'readyState', { value: 4, writable: true });

                // Trigger the readystatechange and load events
                if (typeof xhr.onreadystatechange === 'function') {
                    xhr.onreadystatechange();
                }
                if (typeof xhr.onload === 'function') {
                    xhr.onload();
                }
                if (typeof xhr.onloadend === 'function') {
                    xhr.onloadend();
                }
            } else {
                // If not intercepting, proceed with the original send
                return originalSend.apply(this, arguments);
            }
        };

        return xhr;
    };

    // Function to disable interceptor
    window.disableTLSInterceptor = function() {
        window.tlsInterceptorEnabled = false;
        console.log('TLS Interceptor disabled');
    };

    // Function to enable interceptor
    window.enableTLSInterceptor = function() {
        window.tlsInterceptorEnabled = true;
        console.log('TLS Interceptor enabled');
    };
    // Listen for commands from content script
    document.addEventListener('tlsInterceptorCommand', function(event) {
        const command = event.detail.command;
        console.log('Received command:', command);
        
        if (command === 'enable') {
            window.enableTLSInterceptor();
        } else if (command === 'disable') {
            window.disableTLSInterceptor();
        }
    });

    // Send a signal that the interceptor is ready
    document.dispatchEvent(new CustomEvent('tlsInterceptorReady'));
    
    console.log('TLS Interceptor initialized and ready');
})();