/**
 * Offside Outlaws - Main JavaScript
 */

document.addEventListener('DOMContentLoaded', function() {
    // Mobile menu toggle
    const mobileMenuBtn = document.querySelector('.mobile-menu-btn');
    const navLinks = document.querySelector('.nav-links');

    if (mobileMenuBtn && navLinks) {
        mobileMenuBtn.addEventListener('click', function() {
            navLinks.classList.toggle('active');

            // Animate hamburger to X
            const spans = mobileMenuBtn.querySelectorAll('span');
            spans.forEach((span, index) => {
                span.style.transition = 'all 0.3s';
            });

            if (navLinks.classList.contains('active')) {
                spans[0].style.transform = 'rotate(45deg) translate(5px, 5px)';
                spans[1].style.opacity = '0';
                spans[2].style.transform = 'rotate(-45deg) translate(5px, -5px)';
            } else {
                spans[0].style.transform = 'none';
                spans[1].style.opacity = '1';
                spans[2].style.transform = 'none';
            }
        });
    }

    // Close mobile menu when clicking a link
    const navLinkItems = document.querySelectorAll('.nav-links a');
    navLinkItems.forEach(link => {
        link.addEventListener('click', () => {
            if (navLinks) {
                navLinks.classList.remove('active');
            }
        });
    });

    // Smooth scroll for anchor links
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function(e) {
            const href = this.getAttribute('href');
            if (href !== '#') {
                e.preventDefault();
                const target = document.querySelector(href);
                if (target) {
                    target.scrollIntoView({
                        behavior: 'smooth',
                        block: 'start'
                    });
                }
            }
        });
    });

    // Load team data from JSON if available
    loadTeamData();
});

/**
 * Load team data from data.json
 * This can be updated by the scraper script
 */
async function loadTeamData() {
    try {
        const response = await fetch('data/team-data.json');
        if (!response.ok) {
            console.log('Team data not found, using static content');
            return;
        }

        const data = await response.json();

        // Update stats on homepage
        if (data.record) {
            const recordEl = document.getElementById('record');
            if (recordEl) recordEl.textContent = data.record;
        }

        if (data.goalsFor) {
            const gfEl = document.getElementById('goals-for');
            if (gfEl) gfEl.textContent = data.goalsFor;
        }

        if (data.goalsAgainst) {
            const gaEl = document.getElementById('goals-against');
            if (gaEl) gaEl.textContent = data.goalsAgainst;
        }

        if (data.standing) {
            const standingEl = document.getElementById('standing');
            if (standingEl) standingEl.textContent = data.standing;
        }

        // Update next game info
        if (data.nextGame) {
            const nextGameDate = document.getElementById('next-game-date');
            const homeTeam = document.getElementById('home-team');
            const awayTeam = document.getElementById('away-team');

            if (nextGameDate) {
                nextGameDate.textContent = `${data.nextGame.date} • ${data.nextGame.time}`;
            }
            if (homeTeam && data.nextGame.homeTeam) {
                homeTeam.textContent = data.nextGame.homeTeam;
            }
            if (awayTeam && data.nextGame.awayTeam) {
                awayTeam.textContent = data.nextGame.awayTeam;
            }
        }

        console.log('Team data loaded successfully');

    } catch (error) {
        console.log('Could not load team data:', error.message);
    }
}

/**
 * Format date for display
 */
function formatDate(dateString) {
    const date = new Date(dateString);
    const options = { weekday: 'short', month: 'short', day: 'numeric' };
    return date.toLocaleDateString('en-US', options).toUpperCase();
}

/**
 * Format time for display
 */
function formatTime(timeString) {
    const [hours, minutes] = timeString.split(':');
    const hour = parseInt(hours);
    const ampm = hour >= 12 ? 'PM' : 'AM';
    const hour12 = hour % 12 || 12;
    return `${hour12}:${minutes} ${ampm}`;
}
