<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}{{ project_name }}{% endblock %}</title>
    {% if project_description %}<meta name="description" content="{{ project_description }}">{% endif %}
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Lexend:wght@400;600;700&display=swap" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    {% load static %}
    <link rel="stylesheet" href="{% static 'css/custom.css' %}?v={{ deployment_timestamp }}">
    
    <style>
        :root {
            --bg-dark: #0a0b0d;
            --card-bg: #1e2126;
            --accent-green: #00ffad;
            --accent-red: #ff4d4d;
            --text-main: #ffffff;
            --text-muted: #94a3b8;
            --border-color: #2d333b;
        }
        
        body {
            background-color: var(--bg-dark);
            color: var(--text-main);
            font-family: 'Inter', sans-serif;
            margin: 0;
            padding: 0;
        }
        
        h1, h2, h3, h4, .navbar-brand {
            font-family: 'Lexend', sans-serif;
        }

        .navbar {
            background-color: rgba(10, 11, 13, 0.8);
            backdrop-filter: blur(10px);
            border-bottom: 1px solid var(--border-color);
            padding: 1rem 2rem;
        }

        .navbar-brand {
            font-weight: 700;
            font-size: 1.5rem;
            color: var(--accent-green) !important;
        }

        .nav-link {
            color: var(--text-muted) !important;
            font-weight: 500;
            transition: color 0.3s ease;
        }

        .nav-link:hover, .nav-link.active {
            color: var(--text-main) !important;
        }

        .btn-primary-custom {
            background-color: var(--accent-green);
            color: var(--bg-dark);
            font-weight: 600;
            border: none;
            padding: 0.5rem 1.5rem;
            border-radius: 8px;
            transition: transform 0.2s ease, box-shadow 0.2s ease;
            text-decoration: none;
            display: inline-block;
        }

        .btn-primary-custom:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(0, 255, 173, 0.3);
            background-color: #00e69b;
            color: var(--bg-dark);
        }
    </style>
    {% block head %}{% endblock %}
</head>
<body>
    <nav class="navbar navbar-expand-lg sticky-top">
        <div class="container-fluid">
            <a class="navbar-brand" href="{% url 'home' %}">
                <i class="fa-solid fa-chart-line me-2"></i>MP TRADE TRACKER
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item"><a class="nav-link" href="{% url 'home' %}">Dashboard</a></li>
                    <li class="nav-item"><a class="nav-link" href="{% url 'mp_list' %}">MPs</a></li>
                    <li class="nav-item"><a class="nav-link" href="{% url 'ticker_list' %}">Tickers</a></li>
                    {% if user.is_authenticated %}
                        <li class="nav-item"><a class="nav-link" href="{% url 'profile' %}">Watchlist</a></li>
                    {% endif %}
                </ul>
                <div class="d-flex align-items-center">
                    {% if user.is_authenticated %}
                        <div class="dropdown me-3">
                            <button class="btn btn-link nav-link dropdown-toggle d-flex align-items-center" type="button" id="userDropdown" data-bs-toggle="dropdown">
                                <i class="bi bi-person-circle me-2"></i>{{ user.username }}
                            </button>
                            <ul class="dropdown-menu dropdown-menu-dark dropdown-menu-end shadow">
                                <li><a class="dropdown-item" href="{% url 'profile' %}"><i class="bi bi-person me-2"></i>Profile</a></li>
                                <li><hr class="dropdown-divider"></li>
                                <li>
                                    <form action="{% url 'logout' %}" method="post" class="dropdown-item p-0">
                                        {% csrf_token %}
                                        <button type="submit" class="btn btn-link dropdown-item py-2 px-3 m-0 border-0"><i class="bi bi-box-arrow-right me-2"></i>Logout</button>
                                    </form>
                                </li>
                            </ul>
                        </div>
                    {% else %}
                        <a href="{% url 'login' %}" class="nav-link me-3">Login</a>
                        <a href="{% url 'signup' %}" class="btn btn-primary-custom">Get Started</a>
                    {% endif %}
                </div>
            </div>
        </div>
    </nav>

    <main>
        {% block content %}{% endblock %}
    </main>

    <footer class="py-4 mt-5 border-top border-secondary">
        <div class="container text-center">
            <p class="text-muted small mb-0">&copy; 2024 Canada MP Trade Tracker. For reconnaissance purposes only.</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
        function getCookie(name) {
            let cookieValue = null;
            if (document.cookie && document.cookie !== '') {
                const cookies = document.cookie.split(';');
                for (let i = 0; i < cookies.length; i++) {
                    const cookie = cookies[i].trim();
                    if (cookie.substring(0, name.length + 1) === (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }

        const csrftoken = getCookie('csrftoken');

        document.addEventListener('click', function(e) {
            const btn = e.target.closest('.follow-btn');
            if (btn) {
                const mpId = btn.getAttribute('data-mp-id');
                const ticker = btn.getAttribute('data-ticker');
                
                {% if not user.is_authenticated %}
                window.location.href = "{% url 'login' %}";
                return;
                {% endif %}

                const formData = new FormData();
                if (mpId) formData.append('mp_id', mpId);
                if (ticker) formData.append('ticker', ticker);

                fetch("{% url 'toggle_follow' %}", {
                    method: 'POST',
                    headers: {
                        'X-CSRFToken': csrftoken,
                        'X-Requested-With': 'XMLHttpRequest'
                    },
                    body: formData
                })
                .then(response => response.json())
                .then(data => {
                    // Custom event for specific page handling
                    const event = new CustomEvent('followToggled', { detail: { data, btn } });
                    document.dispatchEvent(event);

                    if (data.status === 'followed') {
                        if (btn.classList.contains('btn-outline-light')) {
                            btn.innerText = 'Following';
                            btn.classList.remove('btn-outline-light');
                            btn.classList.add('btn-success');
                        }
                    } else if (data.status === 'unfollowed') {
                        if (btn.classList.contains('btn-success')) {
                            btn.innerText = 'Follow';
                            btn.classList.remove('btn-success');
                            btn.classList.add('btn-outline-light');
                        }
                    }
                })
                .catch(error => {
                    console.error('Error toggling follow:', error);
                });
            }
        });
    </script>
</body>
</html>
