// DOM Ready
document.addEventListener('DOMContentLoaded', () => {
    initWeatherWidget();
    initAccordion();
    initGoogleReviews();
});

// 1. Weather Widget
function initWeatherWidget() {
    const widget = document.querySelector('.weather-widget');
    if (!widget || !window.MKT_CONFIG.openweatherApiKey) return;

    widget.textContent = 'Loading weather...';
    widget.classList.add('weather-loading');

    const { openweatherApiKey, lat, lon, fallbackCity } = window.MKT_CONFIG;

    fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${openweatherApiKey}&units=metric`)
        .then(response => {
            if (!response.ok) throw new Error('API error');
            return response.json();
        })
        .then(data => {
            const temp = Math.round(data.main.temp);
            const desc = data.weather[0].description.charAt(0).toUpperCase() + data.weather[0].description.slice(1);
            widget.innerHTML = `
                <strong>Now in ${fallbackCity}:</strong> ${temp}°C &mdash; ${desc}
            `;
            widget.classList.remove('weather-loading');
        })
        .catch(err => {
            console.warn('Weather API failed, using fallback:', err);
            widget.textContent = `Current: Dry Season in ${fallbackCity} (Ideal for trekking!)`;
            widget.classList.remove('weather-loading');
        });
}

// 2. Accordion
function initAccordion() {
    document.querySelectorAll('.accordion-header').forEach(header => {
        header.addEventListener('click', () => {
            const content = header.nextElementSibling;
            const isOpen = content.classList.contains('open');
            
            // Close all
            document.querySelectorAll('.accordion-content').forEach(c => {
                c.classList.remove('open');
                c.style.maxHeight = null;
            });
            
            // Open clicked if was closed
            if (!isOpen) {
                content.classList.add('open');
            }
        });
    });
}

// 3. Google Reviews Carousel
function initGoogleReviews() {
    const container = document.querySelector('.reviews-section');
    if (!container) return;

    const dataEl = document.getElementById('google-reviews-data');
    if (!dataEl) return;

    let reviews = [];
    try {
        reviews = JSON.parse(dataEl.textContent);
    } catch (e) {
        console.error('Failed to parse reviews JSON');
        return;
    }

    if (!reviews.length) return;

    const carousel = document.createElement('div');
    carousel.className = 'review-carousel';
    container.appendChild(carousel);

    let currentIndex = 0;
    const renderReview = (index) => {
        const r = reviews[index];
        carousel.innerHTML = `
            <div class="review-card">
                <div class="review-stars">★★★★★</div>
                <div class="review-text">"${r.text}"</div>
                <div class="review-author">— ${r.author}</div>
            </div>
        `;
    };

    renderReview(currentIndex);

    // Auto-rotate every 6s
    setInterval(() => {
        currentIndex = (currentIndex + 1) % reviews.length;
        renderReview(currentIndex);
    }, 6000);
}