{% load static i18n %}
{% get_current_language as CURRENT_LANG %}
<!DOCTYPE html>
<html lang="{{ CURRENT_LANG }}" {% if CURRENT_LANG == 'ar' %}dir="rtl"{% endif %}>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>{% block title %}{{ app_settings.app_name|default:"MASAR CARGO" }}{% 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">
  {% if CURRENT_LANG == 'ar' %}
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css">
  {% endif %}
  
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  
  <link rel="stylesheet" href="{% static 'css/custom.css' %}?v={{ deployment_timestamp }}">
  <!-- Chatbot Widget CSS -->
  <link rel="stylesheet" href="{% static 'css/chatbot.css' %}?v={{ deployment_timestamp }}">
  
  {% block head %}{% endblock %}
  <style>
    body { font-family: 'Outfit', sans-serif; background-color: #f8f9fa; }
    {% if CURRENT_LANG == 'ar' %}
    body { font-family: 'Cairo', sans-serif; }
    {% endif %}
    .navbar { background-color: #0A1D37; }
    .navbar-brand, .nav-link { color: #fff !important; }
    .btn-primary { background-color: #2196F3; border-color: #2196F3; }
    footer { background-color: #0A1D37; color: #fff; padding: 3rem 0; }
    .breadcrumb-container { background: #fff; border-bottom: 1px solid #eee; padding: 0.5rem 0; margin-bottom: 0; }
    .breadcrumb-item a { text-decoration: none; color: #6c757d; }
    .breadcrumb-item.active { color: #2196F3; font-weight: 600; }
    .nav-profile-pic { width: 30px; height: 30px; object-fit: cover; border-radius: 50%; }
  </style>
</head>

<body>
  <nav class="navbar navbar-expand-lg navbar-dark sticky-top">
    <div class="container">
      <a class="navbar-brand d-flex align-items-center" href="/">
        {% if app_settings.logo %}
          <img src="{{ app_settings.logo.url }}" alt="{{ app_settings.app_name }}" height="40" class="me-2">
        {% else %}
          <i class="fa-solid fa-truck-fast me-2 text-info"></i>
        {% endif %}
        <span>{{ app_settings.app_name|default:"MASAR CARGO" }}</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" href="/">{% trans "Home" %}</a>
          </li>
          {% if user.is_authenticated %}
            <li class="nav-item">
              <a class="nav-link" href="{% url 'dashboard' %}">{% trans "Dashboard" %}</a>
            </li>
            {% if user.profile.role == 'SHIPPER' %}
            <li class="nav-item">
              <a class="nav-link" href="{% url 'marketplace' %}">{% trans "Marketplace" %}</a>
            </li>
            {% endif %}
          {% endif %}
          <li class="nav-item">
            <a class="nav-link" href="{% url 'contact' %}">{% trans "Contact" %}</a>
          </li>
          
          <li class="nav-item dropdown ms-lg-3">
            <a class="nav-link dropdown-toggle" href="#" id="langDropdown" role="button" data-bs-toggle="dropdown">
              <i class="fa-solid fa-globe me-1"></i> {{ CURRENT_LANG|upper }}
            </a>
            <ul class="dropdown-menu">
              <li>
                <form action="{% url 'set_language' %}" method="post">
                  {% csrf_token %}
                  <input name="next" type="hidden" value="{{ request.get_full_path }}">
                  <input name="language" type="hidden" value="en">
                  <button type="submit" class="dropdown-item">English</button>
                </form>
              </li>
              <li>
                <form action="{% url 'set_language' %}" method="post">
                  {% csrf_token %}
                  <input name="next" type="hidden" value="{{ request.get_full_path }}">
                  <input name="language" type="hidden" value="ar">
                  <button type="submit" class="dropdown-item">العربية</button>
                </form>
              </li>
            </ul>
          </li>

          {% if user.is_authenticated %}
          <li class="nav-item dropdown ms-lg-3">
            <a class="nav-link dropdown-toggle d-flex align-items-center" href="#" data-bs-toggle="dropdown">
              {% if user.profile.profile_picture %}
                <img src="{{ user.profile.profile_picture.url }}" alt="{{ user.username }}" class="nav-profile-pic me-2">
              {% else %}
                <i class="fa-solid fa-user-circle me-1"></i>
              {% endif %}
              {{ user.username }}
            </a>
            <ul class="dropdown-menu">
              <li>
                <a class="dropdown-item" href="{% url 'profile' %}">
                  <i class="fa-solid fa-user-gear me-2"></i>{% trans "My Profile" %}
                </a>
              </li>
              {% if user.is_staff %}
              <li>
                <a class="dropdown-item" href="{% url 'admin:index' %}">
                  <i class="fa-solid fa-lock me-2"></i>{% trans "Admin Panel" %}
                </a>
              </li>
              {% endif %}
              <li><hr class="dropdown-divider"></li>
              <li>
                <form action="{% url 'logout' %}" method="post">
                  {% csrf_token %}
                  <button type="submit" class="dropdown-item">
                    <i class="fa-solid fa-right-from-bracket me-2"></i>{% trans "Logout" %}
                  </button>
                </form>
              </li>
            </ul>
          </li>
          {% else %}
          <li class="nav-item ms-lg-3">
            <a class="nav-link" href="{% url 'login' %}">{% trans "Login" %}</a>
          </li>
          <li class="nav-item">
            <a class="btn btn-primary" href="{% url 'register' %}">{% trans "Get Started" %}</a>
          </li>
          {% endif %}
        </ul>
      </div>
    </div>
  </nav>

  {% block breadcrumbs %}{% endblock %}

  {% if messages %}
  <div class="container mt-3">
    {% 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 %}
  </div>
  {% endif %}

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

  <footer>
    <div class="container">
      <div class="row">
        <div class="col-md-4 mb-4">
          <h4 class="fw-bold">{{ app_settings.app_name|default:"MASAR CARGO" }}</h4>
          {% if app_settings.slogan %}
            <p class="text-white-50">{{ app_settings.slogan }}</p>
          {% else %}
            <p class="text-white-50">{% trans "Empowering logistics with smart technology. Locally and abroad." %}</p>
          {% endif %}
        </div>
        <div class="col-md-4 mb-4">
          <h5 class="fw-bold">{% trans "Quick Links" %}</h5>
          <ul class="list-unstyled">
            <li><a href="{% url 'privacy_policy' %}" class="text-white-50 text-decoration-none">{% trans "Privacy Policy" %}</a></li>
            <li><a href="{% url 'terms_of_service' %}" class="text-white-50 text-decoration-none">{% trans "Terms of Service" %}</a></li>
            <li><a href="{% url 'contact' %}" class="text-white-50 text-decoration-none">{% trans "Contact Us" %}</a></li>
          </ul>
        </div>
        <div class="col-md-4 mb-4">
          <h5 class="fw-bold">{% trans "Contact" %}</h5>
          <p class="text-white-50">
            {% if app_settings.contact_phone %}
              <i class="fa-solid fa-phone me-2"></i> {{ app_settings.contact_phone }}<br>
            {% endif %}
            {% if app_settings.contact_email %}
              <i class="fa-solid fa-envelope me-2"></i> {{ app_settings.contact_email }}
            {% endif %}
          </p>
          {% if app_settings.registration_number %}
            <p class="text-white-50 small mb-1">{% trans "CR No:" %} {{ app_settings.registration_number }}</p>
          {% endif %}
          {% if app_settings.tax_number %}
            <p class="text-white-50 small mb-0">{% trans "VAT No:" %} {{ app_settings.tax_number }}</p>
          {% endif %}
        </div>
      </div>
      <hr class="bg-white-50">
      <p class="text-center text-white-50 mb-0">&copy; 2026 {{ app_settings.app_name|default:"MASAR CARGO" }}. All rights reserved.</p>
    </div>
  </footer>

  <!-- Chatbot Widget HTML -->
  <div id="chatbot-widget">
      <button id="chatbot-button" title="{% trans 'Chat with MASAR AI' %}">
          <i class="fa-solid fa-robot"></i>
      </button>
      
      <div id="chatbot-window">
          <div id="chatbot-header">
              <h5>MASAR AI</h5>
              <button id="chatbot-close">&times;</button>
          </div>
          <div id="chatbot-messages">
              <div class="chat-message bot">
                  {% trans "Hello! I am MASAR, your AI assistant. How can I help you today?" %}
              </div>
          </div>
          <div id="chatbot-input-container">
              <input type="text" id="chatbot-input" placeholder="{% trans 'Type a message...' %}">
              <button id="chatbot-send">
                  <i class="fa-solid fa-paper-plane"></i>
              </button>
          </div>
          <div class="px-3 pb-2 text-center">
              <a href="https://wa.me/{{ app_settings.contact_phone|default:'' }}" target="_blank" class="small text-muted text-decoration-none">
                  <i class="fa-brands fa-whatsapp text-success"></i> {% trans "Talk to a Human" %}
              </a>
          </div>
      </div>
  </div>

  <!-- Bootstrap 5 JS Bundle -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
  <!-- Chatbot Widget JS -->
  <script src="{% static 'js/chatbot.js' %}?v={{ deployment_timestamp }}"></script>
  
  {% block extra_js %}{% endblock %}
</body>

</html>