{% load i18n static i18n_urls %}
{% 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 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%;
        }

        /* Float helpers if needed (though flex is preferred) */
        .float-end { float: left !important; }
        .float-start { float: right !important; }
    </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" style="z-index: 1000;">
        <div class="container">
            <a class="navbar-brand fw-bold fs-3" href="{% url 'index' %}" style="font-family: 'Outfit', sans-serif;">masar<span style="color: var(--accent-orange)">X</span></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 '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>
                        {% if user.is_staff %}
                        <li class="nav-item">
                            <a class="nav-link px-3 text-warning" href="/admin/">{% trans "Admin" %}</a>
                        </li>
                        {% endif %}
                        <li class="nav-item">
                            <a href="{% url 'profile' %}" class="nav-link text-white-50">{% trans "Hello" %}, {{ user.username }}</a>
                        </li>
                        <li class="nav-item">
                            <form action="{% url 'logout' %}" method="post" class="d-inline">
                                {% csrf_token %}
                                <button type="submit" class="btn btn-link nav-link">{% trans "Logout" %}</button>
                            </form>
                        </li>
                    {% else %}
                        <li class="nav-item">
                            <a class="nav-link px-3" href="{% url 'login' %}">{% trans "Login" %}</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link px-3" href="{% url 'register' %}">{% trans "Register" %}</a>
                        </li>
                    {% endif %}
                    
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="langDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="bi bi-globe"></i> {{ lang.name_local }}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            {% 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>

                    <li class="nav-item ms-lg-3">
                        {% if user.is_authenticated and user_profile.role == 'car_owner' %}
                            <a href="{% url 'dashboard' %}" class="btn btn-masarx-primary btn-sm">{% trans "Find Loads" %}</a>
                        {% else %}
                            <a href="{% url 'shipment_request' %}" class="btn btn-masarx-primary btn-sm">{% trans "Start Shipping" %}</a>
                        {% endif %}
                    </li>
                </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" role="alert">
                    {{ 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">
        <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>
</body>
</html>