<!-- templates/base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Meal Planner</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        .nav-tabs .nav-link.active {
            background-color: #0d6efd;
            color: white;
        }
        .nutrition-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
            gap: 10px;
            margin: 10px 0;
        }
        .nutrition-item {
            text-align: center;
            padding: 8px;
            background-color: #f8f9fa;
            border-radius: 4px;
        }
        .person-toggle {
            position: absolute;
            top: 10px;
            right: 20px;
        }
        .modal-lg {
            max-width: 800px;
        }
        .btn i {
            margin-right: 4px;
        }
        .table th, .table td {
            vertical-align: middle;
        }
        .badge {
            font-size: 0.8em;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="person-toggle">
            <select id="personSelect" class="form-select" onchange="switchPerson()">
                <option value="Sarah" {% if person == "Sarah" %}selected{% endif %}>Sarah</option>
                <option value="Stuart" {% if person == "Stuart" %}selected{% endif %}>Stuart</option>
            </select>
        </div>

        <!-- <h1 class="mt-3 mb-4">
            <i class="bi bi-calendar-check"></i> Meal Planner
        </h1> -->
        
        <ul class="nav nav-tabs" id="myTab" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link" onclick="location.href='/foods'">
                    <i class="bi bi-apple"></i> Foods
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" onclick="location.href='/meals'">
                    <i class="bi bi-egg-fried"></i> Meals
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" onclick="location.href='/plan'">
                    <i class="bi bi-calendar-week"></i> Plan
                </button>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/detailed">Detailed</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/templates">Templates</a>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" onclick="location.href='/weeklymenu'">
                    <i class="bi bi-calendar-week"></i> Weekly Menu
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" onclick="location.href='/tracker'">
                    <i class="bi bi-calendar-check"></i> Tracker
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" onclick="location.href='/charts'">
                    <i class="bi bi-graph-up"></i> Charts
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <a class="nav-link" href="/admin">
                    <i class="bi bi-gear"></i> Admin
                </a>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" onclick="location.href='/llm'">
                    <i class="bi bi-robot"></i> LLM Extract
                </button>
            </li>
        </ul>

        <div class="tab-content mt-3">
            {% block content %}{% endblock %}
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function switchPerson() {
            const person = document.getElementById('personSelect').value;
            localStorage.setItem('selectedPerson', person);
            const currentUrl = new URL(window.location);
            currentUrl.searchParams.set('person', person);
            window.location.href = currentUrl.toString();
        }

        // Set active tab based on current URL
        document.addEventListener('DOMContentLoaded', function() {
            // Set person selection from localStorage if available
            const savedPerson = localStorage.getItem('selectedPerson');
            if (savedPerson) {
                const personSelect = document.getElementById('personSelect');
                if (personSelect) {
                    personSelect.value = savedPerson;
                }
            }

            const currentPath = window.location.pathname;
            const tabs = document.querySelectorAll('.nav-link');

            tabs.forEach(tab => {
                const href = tab.getAttribute('onclick');
                if (href && href.includes(currentPath)) {
                    tab.classList.add('active');
                } else if (currentPath === '/tracker' && href && href.includes('/tracker')) {
                    // Special case: ensure Tracker tab is active when on /tracker
                    tab.classList.add('active');
                } else if (currentPath === '/weeklymenu' && href && href.includes('/weeklymenu')) {
                    // Special case: ensure Weekly Menu tab is active when on /weeklymenu
                    tab.classList.add('active');
                } else {
                    tab.classList.remove('active');
                }
            });
        });
    </script>
</body>
</html>