<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}LinkedIn Posts{% endblock %}</title>
    <link rel="icon" type="image/png" href="/static/favicon.png">
    <link rel="preload" href="/static/tailwind.css" as="style">
    <link rel="stylesheet" href="/static/tailwind.css">
    <style>
        body { background-color: #3d4848; }
        .nav-link.active { background-color: #ffc700; color: #2d3838; }
        .nav-link.active svg { stroke: #2d3838; }
        .post-content { white-space: pre-wrap; word-wrap: break-word; }
        .btn-primary { background-color: #ffc700; color: #2d3838; }
        .btn-primary:hover { background-color: #e6b300; }
        .sidebar-bg { background-color: #2d3838; }
        .card-bg { background-color: #4a5858; border-color: #5a6868; }
        .input-bg { background-color: #3d4848; border-color: #5a6868; }
        .input-bg:focus { border-color: #ffc700; outline: none; }
        ::-webkit-scrollbar { width: 8px; height: 8px; }
        ::-webkit-scrollbar-track { background: #3d4848; }
        ::-webkit-scrollbar-thumb { background: #5a6868; border-radius: 4px; }
        ::-webkit-scrollbar-thumb:hover { background: #6a7878; }

        /* Sidebar collapse styles */
        aside { transition: width 0.3s ease; }
        aside.collapsed { width: 4rem; }
        aside.collapsed .sidebar-text { display: none; }
        aside.collapsed .nav-link { justify-content: center; padding-left: 1rem; padding-right: 1rem; }
        aside.collapsed .logo-container { justify-content: center; }
        aside.collapsed .logo-full { display: none; }
        aside.collapsed .user-info { display: none; }
        /* Center profile avatar when collapsed */
        aside.collapsed .p-4.border-b > div { justify-content: center; }
        /* Center bottom links when collapsed */
        aside.collapsed .p-4.border-t a { justify-content: center; padding-left: 1rem; padding-right: 1rem; }
        main { transition: margin-left 0.3s ease; }
        main.sidebar-collapsed { margin-left: 4rem; }
        .toggle-btn { cursor: pointer; transition: transform 0.3s ease; }
        aside.collapsed .toggle-btn { transform: rotate(180deg); }
        /* Hide nav scrollbar when sidebar collapsed */
        aside.collapsed nav { overflow-y: hidden; overflow-x: hidden; }
        /* Subtle scrollbar inside nav */
        nav::-webkit-scrollbar { width: 4px; }
        nav::-webkit-scrollbar-track { background: transparent; }
        nav::-webkit-scrollbar-thumb { background: #4a5858; border-radius: 4px; }
        nav::-webkit-scrollbar-thumb:hover { background: #5a6868; }
    </style>

    <!-- Prevent sidebar flash on page load -->
    <script>
    (function() {
        const isCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';
        if (isCollapsed) {
            document.documentElement.classList.add('sidebar-loading-collapsed');
        }
    })();
    </script>
    <style>
        html.sidebar-loading-collapsed aside#sidebar { width: 4rem; }
        html.sidebar-loading-collapsed aside#sidebar .sidebar-text { display: none; }
        html.sidebar-loading-collapsed aside#sidebar .nav-link { justify-content: center; padding-left: 1rem; padding-right: 1rem; }
        html.sidebar-loading-collapsed aside#sidebar .logo-full { display: none; }
        html.sidebar-loading-collapsed aside#sidebar .user-info { display: none; }
        html.sidebar-loading-collapsed aside#sidebar .p-4.border-b > div { justify-content: center; }
        html.sidebar-loading-collapsed aside#sidebar .p-4.border-t a { justify-content: center; padding-left: 1rem; padding-right: 1rem; }
        html.sidebar-loading-collapsed main#mainContent { margin-left: 4rem; }
    </style>

    {% block head %}{% endblock %}
</head>
<body class="text-gray-100 min-h-screen flex {% if limit_reached %}pt-6{% endif %}">

    {% if limit_reached %}
    <!-- Token Limit Banner -->
    <div class="fixed top-0 left-0 right-0 z-[9999] bg-red-600 text-white text-xs font-medium flex items-center justify-center gap-2 px-4" style="height: 1.5rem;">
        <svg class="w-3.5 h-3.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/></svg>
        Token-Limit erreicht – keine KI-Aktionen mehr heute möglich. Morgen wird das Limit zurückgesetzt.
    </div>
    {% endif %}
    <!-- Sidebar -->
    <aside id="sidebar" class="w-64 sidebar-bg border-r border-gray-600 flex flex-col fixed h-full top-0" {% if limit_reached %}style="top: 1.5rem; height: calc(100vh - 1.5rem);"{% endif %}>
        <div class="p-4 border-b border-gray-600">
            <div class="flex items-center justify-between gap-3 logo-container">
                <div class="logo-full">
                    <img src="/static/logo.png" alt="Logo" class="h-15 w-auto">
                </div>
                <button onclick="toggleSidebar()" class="toggle-btn text-gray-400 hover:text-white">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
                    </svg>
                </button>
            </div>
        </div>

        <!-- User Profile -->
        {% if session %}
        <div class="p-4 border-b border-gray-600">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 rounded-full overflow-hidden bg-brand-highlight flex items-center justify-center flex-shrink-0">
                    {% if avatar_url or profile_picture or session.linkedin_picture %}
                    <img src="{{ avatar_url or profile_picture or session.linkedin_picture }}" alt="{{ session.display_name or session.linkedin_name }}" class="w-full h-full object-cover" referrerpolicy="no-referrer">
                    {% else %}
                    <span class="text-brand-bg-dark font-bold">{{ (session.display_name or session.linkedin_name or 'User')[0] | upper }}</span>
                    {% endif %}
                </div>
                <div class="flex-1 min-w-0 user-info">
                    <p class="text-white font-medium text-sm truncate">{{ session.display_name or session.linkedin_name or 'Benutzer' }}</p>
                    {% if session.account_type == 'employee' and session.company_name %}
                    <p class="text-gray-400 text-xs truncate">Mitarbeiter bei: {{ session.company_name }}</p>
                    {% else %}
                    <p class="text-gray-400 text-xs truncate">{{ session.email or '' }}</p>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endif %}

        <nav class="flex-1 p-4 space-y-2 overflow-y-auto min-h-0">
            <a href="/" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'home' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/></svg>
                <span class="sidebar-text">Dashboard</span>
            </a>
            <a href="/research" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'research' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/></svg>
                <span class="sidebar-text">Research Topics</span>
            </a>
            <a href="/create" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'create' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>
                <span class="sidebar-text">Post erstellen</span>
            </a>
            <a href="/posts" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'posts' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"/></svg>
                <span class="sidebar-text">Meine Posts</span>
            </a>
            <a href="/post-types" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'post_types' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"/></svg>
                <span class="sidebar-text">Post-Typen</span>
            </a>
            <a href="/calendar" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'calendar' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg>
                <span class="sidebar-text">Mein Kalender</span>
            </a>
            {% if session and session.account_type == 'company' %}
            <a href="/company/accounts" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'accounts' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/></svg>
                <span class="sidebar-text">Konten</span>
            </a>
            {% endif %}
            {% if session and session.account_type == 'employee' %}
            <a href="/employee/strategy" class="nav-link flex items-center gap-3 px-4 py-3 rounded-lg text-gray-300 hover:bg-brand-bg-light transition-colors {% if page == 'strategy' %}active{% endif %}">
                <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"/>
                </svg>
                <span class="sidebar-text">Strategie</span>
            </a>
            {% endif %}
        </nav>

        <div class="p-4 border-t border-gray-600 space-y-2">
            <a href="/settings" class="flex items-center gap-2 text-gray-400 hover:text-gray-200 text-sm transition-colors {% if page == 'settings' %}text-brand-highlight{% endif %}">
                <svg class="w-4 h-4 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
                <span class="sidebar-text">Einstellungen</span>
            </a>
            <a href="/logout" class="flex items-center gap-2 text-gray-400 hover:text-gray-200 text-sm transition-colors">
                <svg class="w-4 h-4 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"/></svg>
                <span class="sidebar-text">Logout</span>
            </a>
        </div>
    </aside>

    <!-- Main Content -->
    <main id="mainContent" class="flex-1 ml-64">
        <div class="p-8">
            {% block content %}{% endblock %}
        </div>
    </main>

    <!-- Toast Container -->
    <div id="toast-container" class="fixed bottom-4 right-4 z-50 space-y-2"></div>

    <!-- Background Jobs Script -->
    <script>
    (function() {
        let eventSource = null;

        function connectToJobUpdates() {
            if (eventSource) {
                eventSource.close();
            }

            eventSource = new EventSource('/api/job-updates');

            eventSource.onmessage = function(event) {
                const job = JSON.parse(event.data);
                showJobToast(job);
            };

            eventSource.onerror = function() {
                // Reconnect after 5 seconds
                setTimeout(connectToJobUpdates, 5000);
            };
        }

        function showJobToast(job) {
            const container = document.getElementById('toast-container');
            let toast = document.getElementById('toast-' + job.id);

            if (!toast) {
                toast = document.createElement('div');
                toast.id = 'toast-' + job.id;
                toast.className = 'bg-brand-bg-dark border border-gray-600 rounded-lg shadow-lg p-4 min-w-80 transform transition-all duration-300';
                container.appendChild(toast);
            }

            const statusColors = {
                'pending': 'text-gray-400',
                'running': 'text-brand-highlight',
                'completed': 'text-green-400',
                'failed': 'text-red-400'
            };

            const statusIcons = {
                'pending': '<svg class="w-5 h-5 animate-pulse" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>',
                'running': '<svg class="w-5 h-5 animate-spin" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/></svg>',
                'completed': '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>',
                'failed': '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>'
            };

            const jobTypeNames = {
                'profile_analysis': 'Profil-Analyse',
                'post_categorization': 'Kategorisierung',
                'post_type_analysis': 'Post-Typen-Analyse'
            };

            toast.innerHTML = `
                <div class="flex items-start gap-3">
                    <span class="${statusColors[job.status]}">${statusIcons[job.status]}</span>
                    <div class="flex-1">
                        <p class="font-medium text-white text-sm">${jobTypeNames[job.job_type] || job.job_type}</p>
                        <p class="text-gray-400 text-xs mt-1">${job.message || ''}</p>
                        ${job.status === 'running' ? `
                            <div class="mt-2 bg-gray-700 rounded-full h-1.5 overflow-hidden">
                                <div class="bg-brand-highlight h-full transition-all duration-300" style="width: ${job.progress}%"></div>
                            </div>
                        ` : ''}
                        ${job.error ? `<p class="text-red-400 text-xs mt-1">${job.error}</p>` : ''}
                    </div>
                    ${job.status === 'completed' || job.status === 'failed' ? `
                        <button onclick="this.parentElement.parentElement.remove()" class="text-gray-500 hover:text-gray-300">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
                        </button>
                    ` : ''}
                </div>
            `;

            // Auto-remove completed toasts after 10 seconds
            if (job.status === 'completed') {
                setTimeout(() => {
                    if (toast.parentElement) {
                        toast.style.opacity = '0';
                        setTimeout(() => toast.remove(), 300);
                    }
                }, 10000);
            }
        }

        // Connect when page loads
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', connectToJobUpdates);
        } else {
            connectToJobUpdates();
        }

        // Expose for manual toasts
        window.showToast = function(message, type = 'info') {
            const container = document.getElementById('toast-container');
            const toast = document.createElement('div');
            const colors = {
                'info': 'bg-brand-highlight text-brand-bg-dark',
                'success': 'bg-green-500 text-white',
                'error': 'bg-red-500 text-white'
            };
            toast.className = `${colors[type]} px-6 py-3 rounded-lg shadow-lg`;
            toast.textContent = message;
            container.appendChild(toast);
            setTimeout(() => {
                toast.style.opacity = '0';
                setTimeout(() => toast.remove(), 300);
            }, 5000);
        };
    })();
    </script>

    <!-- HTTPS Image Proxy for Supabase Storage -->
    <script>
    (function() {
        function proxySupabaseImages() {
            // Find all images with Supabase storage URLs
            document.querySelectorAll('img[src*="/storage/v1/object/public/"]').forEach(img => {
                const originalSrc = img.src;

                // Only proxy HTTP URLs (HTTPS is fine)
                if (originalSrc.startsWith('http://')) {
                    // Extract bucket and path from URL
                    // Format: http://.../storage/v1/object/public/{bucket}/{path}
                    const match = originalSrc.match(/\/storage\/v1\/object\/public\/([^\/]+)\/(.+)/);

                    if (match) {
                        const bucket = match[1];
                        const path = match[2];
                        const proxyUrl = `/proxy/image/${bucket}/${path}`;

                        img.src = proxyUrl;
                        console.log('Proxied image:', originalSrc, '->', proxyUrl);
                    }
                }
            });
        }

        // Run on page load
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', proxySupabaseImages);
        } else {
            proxySupabaseImages();
        }

        // Also run after dynamic content loads (e.g., AJAX)
        const observer = new MutationObserver(proxySupabaseImages);
        observer.observe(document.body, { childList: true, subtree: true });
    })();
    </script>

    <!-- Sidebar Toggle Script -->
    <script>
    function toggleSidebar() {
        const sidebar = document.getElementById('sidebar');
        const mainContent = document.getElementById('mainContent');

        sidebar.classList.toggle('collapsed');
        mainContent.classList.toggle('sidebar-collapsed');

        // Save state to localStorage
        const isCollapsed = sidebar.classList.contains('collapsed');
        localStorage.setItem('sidebarCollapsed', isCollapsed);
    }

    // Apply proper classes after DOM loads and remove loading class
    document.addEventListener('DOMContentLoaded', function() {
        const isCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';
        const sidebar = document.getElementById('sidebar');
        const mainContent = document.getElementById('mainContent');

        if (isCollapsed) {
            sidebar.classList.add('collapsed');
            mainContent.classList.add('sidebar-collapsed');
        }

        // Remove loading class
        document.documentElement.classList.remove('sidebar-loading-collapsed');
    });
    </script>

    {% block scripts %}{% endblock %}
    <script>
    if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
            navigator.serviceWorker.register('/sw.js').catch(function() {});
        });
    }
    </script>
</body>
</html>
