{% load i18n static i18n_urls core_tags %}
{% get_current_language as LANGUAGE_CODE %}
{% get_language_info for LANGUAGE_CODE as lang %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}" dir="{% if LANGUAGE_BIDI %}rtl{% else %}ltr{% endif %}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}masarX | {% trans "Small Shipments, Smart Delivery" %}{% endblock %}</title>
    
    {% if platform_profile and platform_profile.favicon %}
        <link rel="icon" href="{{ platform_profile.favicon.url }}">
    {% else %}
        <link rel="icon" href="{% static 'img/logo.jpg' %}">
    {% endif %}

    {% if project_description %}
    <meta name="description" content="{{ project_description }}">
    <meta property="og:description" content="{{ project_description }}">
    <meta property="twitter:description" content="{{ project_description }}">
    {% endif %}
    {% if project_image_url %}
    <meta property="og:image" content="{{ project_image_url }}">
    <meta property="twitter:image" content="{{ project_image_url }}">
    {% endif %}

    <!-- Bootstrap 5 -->
    {% if LANGUAGE_BIDI %}
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css">
    {% else %}
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    {% endif %}
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    
    <!-- Custom Styles -->
    <link rel="stylesheet" href="{% static 'css/custom.css' %}?v={{ deployment_timestamp }}">
    
    {% if LANGUAGE_BIDI %}
    <style>
        /* RTL Overrides */
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            text-align: right;
        }
        
        /* Ensure Navbar flips correctly */
        .navbar-nav {
            padding-right: 0; /* Reset default padding */
        }
        
        /* Fix alignment for form inputs in RTL */
        .form-control, .form-select {
            text-align: right;
        }
        /* Ensure labels are also right-aligned */
        .form-label {
            text-align: right;
            width: 100%;
        }
        
        .dropdown-menu-end {
            right: auto;
            left: 0;
        }
    </style>
    {% endif %}

    {% block head %}{% endblock %}
</head>
<body class="d-flex flex-column min-vh-100">
    <!-- Navbar -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark w-100 py-3" style="z-index: 1000;">
        <div class="container">
            <a class="navbar-brand d-flex align-items-center" href="{% url 'index' %}">
                {% if platform_profile and platform_profile.logo %}
                    <img src="{{ platform_profile.logo.url }}" alt="{{ platform_profile.name }}" height="40" class="me-2">
                {% else %}
                    <span class="fw-bold fs-3" style="font-family: 'Outfit', sans-serif;">masar<span style="color: var(--accent-orange)">X</span></span>
                {% endif %}
            </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 ms-auto align-items-center">
                    <li class="nav-item">
                        <a class="nav-link px-3" href="{% url 'track' %}">{% trans "Track" %}</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link px-3" href="{% url 'index' %}#how-it-works">{% trans "How it Works" %}</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link px-3" href="{% url 'contact' %}">{% trans "Contact" %}</a>
                    </li>
                    
                    {% if user.is_authenticated %}
                        <li class="nav-item">
                            <a class="nav-link px-3" href="{% url 'dashboard' %}">{% trans "Dashboard" %}</a>
                        </li>
                        
                        <!-- User Profile Dropdown -->
                        <li class="nav-item dropdown ms-lg-3">
                            <a class="nav-link dropdown-toggle d-flex align-items-center" href="#" id="userDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                {% if user_profile.profile_picture %}
                                    <img src="{{ user_profile.profile_picture.url }}" alt="Profile" class="rounded-circle" width="32" height="32" style="object-fit: cover;">
                                {% else %}
                                    <div class="rounded-circle bg-secondary d-flex justify-content-center align-items-center text-white" style="width: 32px; height: 32px;">
                                        <i class="bi bi-person-fill"></i>
                                    </div>
                                {% endif %}
                                <span class="ms-2 d-none d-lg-inline">{{ user.first_name|default:user.username }}</span>
                            </a>
                            <ul class="dropdown-menu dropdown-menu-end shadow-sm border-0" aria-labelledby="userDropdown">
                                <li>
                                    <div class="px-3 py-2 border-bottom">
                                        <div class="fw-bold text-dark">{{ user.first_name|default:user.username }}</div>
                                        <div class="small text-muted text-truncate" style="max-width: 150px;">{{ user.email }}</div>
                                    </div>
                                </li>
                                {% if user.is_staff %}
                                <li><a class="dropdown-item text-warning" href="{% url 'admin:index' %}"><i class="bi bi-shield-lock me-2"></i>{% trans "Admin Panel" %}</a></li>
                                <li><hr class="dropdown-divider"></li>
                                {% endif %}
                                <li><a class="dropdown-item" href="{% url 'profile' %}"><i class="bi bi-person me-2"></i>{% trans "My Profile" %}</a></li>
                                <li><a class="dropdown-item" href="{% url 'edit_profile' %}"><i class="bi bi-gear me-2"></i>{% trans "Edit Profile" %}</a></li>
                                <li><hr class="dropdown-divider"></li>
                                <li>
                                    <form action="{% url 'logout' %}" method="post" class="d-inline w-100">
                                        {% csrf_token %}
                                        <button type="submit" class="dropdown-item text-danger"><i class="bi bi-box-arrow-right me-2"></i>{% trans "Logout" %}</button>
                                    </form>
                                </li>
                            </ul>
                        </li>

                    {% else %}
                        <li class="nav-item ms-lg-2">
                            <a class="nav-link" href="{% url 'login' %}">{% trans "Login" %}</a>
                        </li>
                        <li class="nav-item ms-lg-2">
                            <a class="btn btn-outline-light btn-sm px-4 rounded-pill" href="{% url 'register' %}">{% trans "Register" %}</a>
                        </li>
                    {% endif %}
                    
                    <!-- Language Switcher -->
                    <li class="nav-item dropdown ms-lg-3">
                        <a class="nav-link dropdown-toggle text-white-50" href="#" id="langDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="bi bi-globe"></i> <span class="d-lg-none ms-2">{{ lang.name_local }}</span>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end shadow-sm border-0">
                            {% get_available_languages as LANGUAGES %}
                            {% for lang_code, lang_name in LANGUAGES %}
                                <li>
                                    {% translate_url lang_code as the_redirect_url %}
                                    <form action="{% url 'set_language' %}" method="post">
                                        {% csrf_token %}
                                        <input name="next" type="hidden" value="{{ the_redirect_url|default:'/' }}">
                                        <input name="language" type="hidden" value="{{ lang_code }}">
                                        <button type="submit" class="dropdown-item {% if LANGUAGE_CODE == lang_code %}active{% endif %}">
                                            {{ lang_name }}
                                        </button>
                                    </form>
                                </li>
                            {% endfor %}
                        </ul>
                    </li>

                    {% if not user.is_authenticated %}
                        <li class="nav-item ms-lg-3 d-none d-lg-block">
                             <a href="{% url 'shipment_request' %}" class="btn btn-masarx-primary btn-sm rounded-pill px-4">{% trans "Start Shipping" %}</a>
                        </li>
                    {% endif %}
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-3">
        {% if messages %}
            {% for message in messages %}
                <div class="alert alert-{{ message.tags }} alert-dismissible fade show shadow-sm border-0" role="alert">
                    {% if message.tags == 'success' %}<i class="bi bi-check-circle-fill me-2"></i>{% endif %}
                    {% if message.tags == 'error' %}<i class="bi bi-exclamation-triangle-fill me-2"></i>{% endif %}
                    {% if message.tags == 'warning' %}<i class="bi bi-exclamation-circle-fill me-2"></i>{% endif %}
                    {% if message.tags == 'info' %}<i class="bi bi-info-circle-fill me-2"></i>{% endif %}
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            {% endfor %}
        {% endif %}
    </div>

    <main class="flex-grow-1">
        {% block content %}{% endblock %}
    </main>

    <footer class="bg-dark text-white pt-5 pb-3 mt-5 border-top border-secondary">
        <div class="container">
            <div class="row">
                <div class="col-md-4 mb-4">
                    {% if platform_profile and platform_profile.logo %}
                        <img src="{{ platform_profile.logo.url }}" alt="{{ platform_profile.name }}" height="40" class="mb-3">
                    {% else %}
                        <h5 class="fw-bold text-white">masar<span style="color: var(--accent-orange)">X</span></h5>
                    {% endif %}
                    
                    {% if platform_profile.slogan %}
                        <p class="text-white-50">{{ platform_profile.slogan }}</p>
                    {% else %}
                        <p class="text-white-50">{% trans "Small Shipments, Smart Delivery" %}</p>
                    {% endif %}
                </div>
                
                <div class="col-md-4 mb-4">
                    <h5 class="text-white mb-3">{% trans "Contact Us" %}</h5>
                    <ul class="list-unstyled text-white-50">
                        {% if platform_profile.address %}
                            <li class="mb-2"><i class="bi bi-geo-alt me-2"></i>{{ platform_profile.address }}</li>
                        {% endif %}
                        {% if platform_profile.phone_number %}
                            <li class="mb-2"><i class="bi bi-telephone me-2"></i>{{ platform_profile.phone_number }}</li>
                        {% endif %}
                        {% if platform_profile.registration_number %}
                            <li class="mb-2">{% trans "Reg No:" %} {{ platform_profile.registration_number }}</li>
                        {% endif %}
                        {% if platform_profile.vat_number %}
                            <li class="mb-2">{% trans "VAT No:" %} {{ platform_profile.vat_number }}</li>
                        {% endif %}
                        <li class="mt-3">
                            <a href="{% url 'contact' %}" class="text-white text-decoration-underline">{% trans "Send us a message" %} <i class="bi bi-arrow-right-short"></i></a>
                        </li>
                    </ul>
                </div>
                
                <div class="col-md-4 mb-4">
                    <h5 class="text-white mb-3">{% trans "Legal" %}</h5>
                    <ul class="list-unstyled">
                        <li class="mb-2"><a href="{% url 'privacy_policy' %}" class="text-white-50 text-decoration-none">{% trans "Privacy Policy" %}</a></li>
                        <li class="mb-2"><a href="{% url 'terms_conditions' %}" class="text-white-50 text-decoration-none">{% trans "Terms & Conditions" %}</a></li>
                    </ul>
                </div>
            </div>
            
            <hr class="border-secondary my-4">
            
            <div class="text-center text-white-50">
                <p class="mb-0">&copy; {% now "Y" %} {{ platform_profile.name|default:"masarX" }}. {% trans "All rights reserved." %}</p>
            </div>
        </div>
    </footer>

    <!-- Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

    <!-- Chat Widget -->
    <button id="masar-chat-toggle" class="d-flex align-items-center justify-content-center">
        <i class="bi bi-chat-dots-fill fs-4"></i>
    </button>
    <div id="masar-chat-widget" class="d-none bg-white rounded-4 shadow overflow-hidden">
        <div class="bg-dark text-white p-3 d-flex justify-content-between align-items-center">
            <div class="d-flex align-items-center">
                <i class="bi bi-robot me-2 fs-5"></i>
                <h6 class="mb-0 fw-bold">MasarX AI</h6>
            </div>
            <div class="d-flex align-items-center">
                {% if platform_profile.phone_number %}
                    <a href="https://wa.me/{{ platform_profile.phone_number|whatsapp_url }}" target="_blank" class="btn btn-sm btn-success me-2 rounded-circle d-flex align-items-center justify-content-center" style="width: 32px; height: 32px;" title="{% trans 'Chat on WhatsApp' %}" data-bs-toggle="tooltip">
                        <i class="bi bi-whatsapp"></i>
                    </a>
                {% endif %}
                <button id="masar-chat-close" class="btn btn-sm btn-link text-white p-0">
                    <i class="bi bi-x-lg"></i>
                </button>
            </div>
        </div>
        <div id="masar-chat-messages" class="flex-grow-1 p-3 bg-light overflow-auto">
            <div class="d-flex mb-3 justify-content-start">
                <div class="p-3 rounded-3 shadow-sm bg-light text-dark" style="max-width: 80%;">
                    {% trans "Hello! How can I help you with your shipments today?" %}
                </div>
            </div>
        </div>
        <div class="p-3 border-top bg-white">
            <form id="masar-chat-form" class="d-flex">
                <input type="text" id="masar-chat-input" class="form-control me-2" placeholder="{% trans "Type a message..." %}" autocomplete="off">
                <button type="submit" class="btn btn-masarx-primary px-3">
                    <i class="bi bi-send-fill"></i>
                </button>
            </form>
        </div>
    </div>
    <script src="{% static "js/chat.js" %}?v={{ deployment_timestamp }}"></script>
    <script>
        // Initialize tooltips
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
        var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl)
        })
    </script>
</body>

</html>