{% load static %}
{% load cache %}
{% load page_title %}
{% load accessibility %}

<!DOCTYPE html>
<html lang="en-GB" class="{% block html_class %}{% endblock %}">
  <head>
    <script>
      (function () {
        var storedTheme = null;
        var resolvedTheme = null;
        try {
          storedTheme = localStorage.getItem("theme");
        } catch (error) {
        }
        if (storedTheme === "dark" || storedTheme === "light") {
          resolvedTheme = storedTheme;
        } else {
          try {
            resolvedTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
              ? "dark"
              : "light";
          } catch (error) {
          }
        }
        if (resolvedTheme === "dark" || resolvedTheme === "light") {
          document.documentElement.dataset.theme = resolvedTheme;
          document.documentElement.style.colorScheme = resolvedTheme;
        } else {
          document.documentElement.removeAttribute("data-theme");
        }
      })();
    </script>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{% block browser_title %}{% firstof page_browser_title page_title as explicit_title %}{% if explicit_title %}{{ explicit_title }} · GIA{% else %}{% with route_value=request.resolver_match.url_name|default:request.path_info|humanize_route %}{% if route_value %}{{ route_value }} · GIA{% else %}GIA{% endif %}{% endwith %}{% endif %}{% endblock %}</title>
    <link rel="icon" type="image/png" href="{% static 'favicon.png' %}?v=sq3" data-gia-favicon="icon">
    <link rel="shortcut icon" href="{% static 'favicon.png' %}?v=sq3" data-gia-favicon="shortcut">
    <link rel="manifest" href="{% static 'manifest.webmanifest' %}">
    {# Managed by tools/frontend_assets/asset-manifest.json and scripts/vendor_frontend_assets.py. #}
    <link rel="preload" href="{% static 'vendor/fontawesome/webfonts/fa-solid-900.woff2' %}" as="font" type="font/woff2" integrity="sha512-Ph1xTLhfMycYSW+wUN8oL3Ggl56nGIS95EHiKWggcL/GbMNjPdib1Hreb1D4COlMxdiGCkk43nspQnpDuTjgQg==" crossorigin="anonymous">
    <link rel="preload" href="{% static 'vendor/fontawesome/webfonts/fa-regular-400.woff2' %}" as="font" type="font/woff2" integrity="sha512-qioT43fXB5q4Bbpn8sPQE9OIZLjKD0c0lVmpm6KmT8k34LM6gkRcOOMi1BOl2lohFG/7p9tzKfTP5G563BQq1g==" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'css/bulma.min.css' %}" integrity="sha512-yh2RE0wZCVZeysGiqTwDTO/dKelCbS9bP2L94UvOFtl/FKXcNAje3Y2oBg/ZMZ3LS1sicYk4dYVGtDex75fvvA==" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'vendor/fontawesome/css/all.css' %}" integrity="sha512-UKBBxJ5N3/MYiSsYTlEsARsp4vELKVRIklED4Mb6wpuVFOgy5Blt+sXUdz1TDReqWsm64xxBA2QoBJRCxI0x5Q==" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'css/gia-theme.css' %}" integrity="sha512-17wNDv0gA1saxAIzoySMcOef4/8dKEo2eZcWGhVHUjKolxhbfYVia9i/wExDqEw8MhfP4Kk8BrMajcOHngqJJg==" crossorigin="anonymous">
    {% block extra_head_assets %}{% endblock %}
    <script src="{% static 'js/htmx.min.js' %}" integrity="sha512-CGXFnDNv5q48ciFeIyWFcfZhqYW0sSBiPO+HZDO3XLM+p8xjhezz5CCxtkXVDKfCbvF+iUhel7xoeSp19o7x7g==" crossorigin="anonymous"></script>
    <script defer src="{% static 'js/remove-me.js' %}" integrity="sha512-uhE4kDw2+tXdJPDKSttOEYhVnwYq310+d5DMQnTjafJ58QLlYPyx0RTCNbjcrTiGfCjAhaQob4AumEUa2m3TaQ==" crossorigin="anonymous"></script>
    <script>
      (function () {
        var FAVICON_VERSION = "sq3";
        var STORAGE_KEY = "theme";
        var THEME_DARK = "dark";
        var THEME_LIGHT = "light";
        var root = document.documentElement;
        var darkMedia = window.matchMedia("(prefers-color-scheme: dark)");

        function getStoredTheme() {
          try {
            var raw = localStorage.getItem(STORAGE_KEY);
            if (raw === THEME_DARK || raw === THEME_LIGHT) {
              return raw;
            }
          } catch (error) {
          }
          return null;
        }

        function getResolvedTheme() {
          var stored = getStoredTheme();
          if (stored === THEME_DARK || stored === THEME_LIGHT) {
            return stored;
          }
          return darkMedia.matches ? THEME_DARK : THEME_LIGHT;
        }

        function applyFavicon() {
          var href = "{% static 'favicon.png' %}?v=" + FAVICON_VERSION;
          var iconLink = document.querySelector("link[data-gia-favicon='icon']");
          if (iconLink) {
            iconLink.setAttribute("href", href);
          }
          var shortcutLink = document.querySelector("link[data-gia-favicon='shortcut']");
          if (shortcutLink) {
            shortcutLink.setAttribute("href", href);
          }
        }

        function persistTheme(mode) {
          try {
            localStorage.setItem(STORAGE_KEY, mode);
          } catch (error) {
          }
        }

        function updateToggleUI(mode) {
          document.querySelectorAll(".js-theme-toggle").forEach(function (button) {
            var nextMode = mode === THEME_DARK ? THEME_LIGHT : THEME_DARK;
            button.setAttribute("data-theme-mode", mode);
            button.setAttribute("aria-label", "Theme: " + mode + ". Click to switch to " + nextMode + ".");
            button.setAttribute("title", "Theme: " + mode);
            var label = button.querySelector("[data-theme-label]");
            if (label) {
              label.textContent = mode === THEME_DARK ? "Dark" : "Light";
            }
          });
        }

        function applyTheme(mode, shouldPersist) {
          var validMode = mode === THEME_DARK ? THEME_DARK : THEME_LIGHT;
          root.dataset.theme = validMode;
          root.style.colorScheme = validMode;
          applyFavicon();
          updateToggleUI(validMode);
          if (shouldPersist !== false) {
            persistTheme(validMode);
          }
        }

        function cycleTheme() {
          var currentTheme = root.dataset.theme === THEME_DARK ? THEME_DARK : THEME_LIGHT;
          applyTheme(currentTheme === THEME_DARK ? THEME_LIGHT : THEME_DARK, true);
        }

        applyTheme(getResolvedTheme(), false);

        if (darkMedia && darkMedia.addEventListener) {
          darkMedia.addEventListener("change", function () {
            if (!getStoredTheme()) {
              applyTheme(getResolvedTheme(), false);
            }
          });
        }

        document.addEventListener("DOMContentLoaded", function () {
          document.querySelectorAll(".js-theme-toggle").forEach(function (button) {
            button.addEventListener("click", cycleTheme);
          });
          updateToggleUI(getResolvedTheme());
        });
      })();

      document.addEventListener("restore-scroll", function () {
        var scrollpos = localStorage.getItem("scrollpos");
        if (scrollpos) {
          window.scrollTo(0, scrollpos);
        }
      });

      document.addEventListener("htmx:beforeSwap", function () {
        localStorage.setItem("scrollpos", window.scrollY);
      });

      document.addEventListener("DOMContentLoaded", function () {
        document.querySelectorAll(".navbar-burger").forEach(function (el) {
          el.addEventListener("click", function () {
            var target = document.getElementById(el.dataset.target);
            el.classList.toggle("is-active");
            if (target) {
              target.classList.toggle("is-active");
            }
            });
        });

        document.body.addEventListener("htmx:afterRequest", function (event) {
          const detail = (event && event.detail) || null;
          const source = detail && detail.elt ? detail.elt : null;
          if (!detail || !detail.successful || !source || !source.dataset) {
            return;
          }
          const queueAfter = String(source.dataset.queueAfter || "");
          if (queueAfter === "remove-card") {
            const cardId = String(source.dataset.queueCardId || "").trim();
            const refreshEvent = String(source.dataset.queueRefreshEvent || "").trim();
            if (cardId) {
              const card = document.getElementById(cardId);
              if (card) {
                card.remove();
              }
            }
            if (refreshEvent) {
              htmx.trigger(document.body, refreshEvent);
            }
            return;
          }
          if (queueAfter === "show-inline-editor") {
            const editorId = String(source.dataset.queueEditorId || "").trim();
            if (!editorId) {
              return;
            }
            const editor = document.getElementById(editorId);
            if (editor) {
              editor.style.display = "block";
            }
          }
        });

        var composeLink = document.getElementById("nav-compose-link");
        var composeDropdown = document.getElementById("nav-compose-contacts");
        var composePreviewLoaded = false;
        var composePreviewLoading = false;
        if (!composeLink || !composeDropdown) {
          return;
        }
        composeLink.addEventListener("mouseenter", function () {
          var previewUrl = composeLink.dataset.previewUrl || "";
          if (!previewUrl || composePreviewLoaded || composePreviewLoading) {
            return;
          }
          composePreviewLoading = true;
          fetch(previewUrl, {
            method: "GET",
            credentials: "same-origin",
            headers: { "HX-Request": "true" },
          })
            .then(function (response) {
              if (!response.ok) {
                throw new Error("Failed contacts preview fetch.");
              }
              return response.text();
            })
            .then(function (html) {
              composeDropdown.innerHTML = html;
              composePreviewLoaded = true;
            })
            .catch(function () {
              composePreviewLoaded = false;
            })
            .finally(function () {
              composePreviewLoading = false;
            });
        });
      });
    </script>
  </head>
  {% get_accessibility_settings request.user as a11y_settings %}
  <body class="{% if a11y_settings and a11y_settings.disable_animations %}reduced-motion {% endif %}{% block body_class %}{% endblock %}">

    <nav class="navbar" role="navigation" aria-label="main navigation">
      <div class="navbar-brand">
        <div class="navbar-item">
          <span class="gia-brand-shell">
            {% if user.is_authenticated %}
              <button class="gia-brand-logo brand-theme-toggle js-theme-toggle" type="button" data-theme-mode="light" aria-label="Theme toggle">
                <svg class="brand-theme-logo" viewBox="0 0 213.35 150.85" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
                  <g transform="translate(38.831 -7.4316)">
                    <g transform="matrix(.99287 0 0 .99911 1.2367 -30.308)">
                      <path class="brand-theme-stroke" d="m-32.645 113.03a115.16 122.5 0 0 1 99.73-61.25 115.16 122.5 0 0 1 99.73 61.25" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="15.42"/>
                      <path class="brand-theme-stroke" d="m67.006 89.591a42.374 42.374 0 0 1 39.148 26.158 42.374 42.374 0 0 1-9.1855 46.179 42.374 42.374 0 0 1-46.179 9.1855 42.374 42.374 0 0 1-26.158-39.148" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="16.251"/>
                      <circle cx="67.003" cy="131.96" r="13.151" fill="#740101"/>
                    </g>
                  </g>
                </svg>
                <span class="is-sr-only" data-theme-label>Auto</span>
              </button>
            {% else %}
              <a href="{% url 'home' %}" class="gia-brand-logo" aria-label="GIA home">
                <svg class="brand-theme-logo" viewBox="0 0 213.35 150.85" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
                  <g transform="translate(38.831 -7.4316)">
                    <g transform="matrix(.99287 0 0 .99911 1.2367 -30.308)">
                      <path class="brand-theme-stroke" d="m-32.645 113.03a115.16 122.5 0 0 1 99.73-61.25 115.16 122.5 0 0 1 99.73 61.25" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="15.42"/>
                      <path class="brand-theme-stroke" d="m67.006 89.591a42.374 42.374 0 0 1 39.148 26.158 42.374 42.374 0 0 1-9.1855 46.179 42.374 42.374 0 0 1-46.179 9.1855 42.374 42.374 0 0 1-26.158-39.148" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="16.251"/>
                      <circle cx="67.003" cy="131.96" r="13.151" fill="#740101"/>
                    </g>
                  </g>
                </svg>
              </a>
            {% endif %}
          </span>
        </div>

        <a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="bar">
          <span aria-hidden="true"></span>
          <span aria-hidden="true"></span>
          <span aria-hidden="true"></span>
        </a>
      </div>

      <div id="bar" class="navbar-menu">
        <div class="navbar-start">
          <a class="navbar-item" href="{% url 'home' %}">
            Home
          </a>
          {% if user.is_authenticated %}
            <div class="navbar-item has-dropdown is-hoverable">
              <a
                id="nav-compose-link"
                class="navbar-link"
                data-preview-url="{% url 'compose_contacts_dropdown' %}"
                data-full-url="{% url 'compose_contacts_dropdown' %}?all=1"
                hx-get="{% url 'compose_contacts_dropdown' %}?all=1"
                hx-target="#nav-compose-contacts"
                hx-trigger="click"
                hx-swap="innerHTML">
                <span class="icon is-small"><i class="fa-solid fa-paper-plane"></i></span>
                <span style="margin-left: 0.35rem;">Compose</span>
              </a>
              <div class="navbar-dropdown" id="nav-compose-contacts">
                <a
                  class="navbar-item"
                  hx-get="{% url 'compose_contacts_dropdown' %}?all=1"
                  hx-target="#nav-compose-contacts"
                  hx-swap="innerHTML">
                  Open Contacts
                </a>
              </div>
            </div>
            <a class="navbar-item" href="{% url 'tasks_hub' %}">
              Task Inbox
            </a>
            <a class="navbar-item" href="{% url 'ai_workspace' %}">
              AI
            </a>
          {% endif %}
        </div>

        <div class="navbar-end">
          {% if user.is_authenticated %}
            <div class="navbar-item has-dropdown is-hoverable">
              <a class="navbar-link">
                Services
              </a>

              <div class="navbar-dropdown is-right gia-navbar-dropdown">
                <a class="navbar-item" href="{% url 'signal' %}">
                  Signal
                </a>
                <a class="navbar-item" href="{% url 'whatsapp' %}">
                  WhatsApp
                </a>
                <a class="navbar-item" href="{% url 'instagram' %}">
                  Instagram
                </a>
              </div>
            </div>

            <div class="navbar-item has-dropdown is-hoverable">
              <a class="navbar-link">
                Data
              </a>

              <div class="navbar-dropdown is-right gia-navbar-dropdown">
                <a class="navbar-item" href="{% url 'sessions' type='page' %}">
                  Sessions
                </a>
                <a class="navbar-item{% if request.resolver_match.url_name == 'business_plan_inbox' or request.resolver_match.url_name == 'business_plan_editor' %} is-current-route{% endif %}" href="{% url 'business_plan_inbox' %}">
                  Business Plans
                </a>
              </div>
            </div>

            <div class="navbar-item has-dropdown is-hoverable">
              <a class="navbar-link">
                Settings
              </a>

              <div class="navbar-dropdown is-right gia-navbar-dropdown">
                <div class="navbar-item has-text-weight-semibold is-size-7 has-text-grey">
                  General
                </div>
                <a class="navbar-item{% if request.resolver_match.url_name == 'notifications_settings' or request.resolver_match.url_name == 'notifications_update' %} is-current-route{% endif %}" href="{% url 'notifications_settings' %}">
                  Notifications
                </a>
                {% if user.is_superuser %}
                  <a class="navbar-item{% if request.resolver_match.url_name == 'system_settings' %} is-current-route{% endif %}" href="{% url 'system_settings' %}">
                    System
                  </a>
                {% endif %}
                <a class="navbar-item{% if request.resolver_match.url_name == 'accessibility_settings' %} is-current-route{% endif %}" href="{% url 'accessibility_settings' %}">
                  Accessibility
                </a>
                <hr class="navbar-divider">
                <div class="navbar-item has-text-weight-semibold is-size-7 has-text-grey">
                  Security
                </div>
                <a class="navbar-item{% if request.resolver_match.url_name == 'encryption_settings' or request.resolver_match.url_name == 'security_settings' %} is-current-route{% endif %}" href="{% url 'encryption_settings' %}">
                  Encryption
                </a>
                <a class="navbar-item{% if request.resolver_match.url_name == 'permission_settings' %} is-current-route{% endif %}" href="{% url 'permission_settings' %}">
                  Permissions
                </a>
                <a class="navbar-item{% if request.resolver_match.url_name == 'security_2fa' or request.resolver_match.namespace == 'two_factor' %} is-current-route{% endif %}" href="{% url 'security_2fa' %}">
                  2FA
                </a>
                <hr class="navbar-divider">
                <div class="navbar-item has-text-weight-semibold is-size-7 has-text-grey">
                  AI
                </div>
                <a class="navbar-item{% if request.resolver_match.url_name == 'ai_models' or request.resolver_match.url_name == 'ais' %} is-current-route{% endif %}" href="{% url 'ai_models' %}">
                  Models
                </a>
                <a class="navbar-item{% if request.resolver_match.url_name == 'ai_execution_log' %} is-current-route{% endif %}" href="{% url 'ai_execution_log' %}">
                  Traces
                </a>
                <hr class="navbar-divider">
                <div class="navbar-item has-text-weight-semibold is-size-7 has-text-grey">
                  Modules
                </div>
                <a class="navbar-item{% if request.resolver_match.url_name == 'command_routing' %} is-current-route{% endif %}" href="{% url 'command_routing' %}">
                  Commands
                </a>
                <a class="navbar-item{% if request.resolver_match.url_name == 'business_plan_inbox' or request.resolver_match.url_name == 'business_plan_editor' %} is-current-route{% endif %}" href="{% url 'business_plan_inbox' %}">
                  Business Plans
                </a>
                <a class="navbar-item{% if request.resolver_match.url_name == 'tasks_settings' %} is-current-route{% endif %}" href="{% url 'tasks_settings' %}">
                  Task Automation
                </a>
                <a class="navbar-item{% if request.resolver_match.url_name == 'translation_settings' %} is-current-route{% endif %}" href="{% url 'translation_settings' %}">
                  Translation
                </a>
                <a class="navbar-item{% if request.resolver_match.url_name == 'availability_settings' or request.resolver_match.url_name == 'behavioral_signals_settings' %} is-current-route{% endif %}" href="{% url 'behavioral_signals_settings' %}">
                  Behavioral Signals
                </a>
                <hr class="navbar-divider">
                <div class="navbar-item has-text-weight-semibold is-size-7 has-text-grey">
                  Automation
                </div>
                <a class="navbar-item{% if request.resolver_match.url_name == 'queues' %} is-current-route{% endif %}" href="{% url 'queues' type='page' %}">
                  Approvals Queue
                </a>
                <a class="navbar-item{% if request.resolver_match.url_name == 'osint_workspace' %} is-current-route{% endif %}" href="{% url 'osint_workspace' %}">
                  OSINT Workspace
                </a>
                <hr class="navbar-divider">
                <a class="navbar-item" href="{% url 'logout' %}">
                  Logout
                </a>
                <button class="navbar-item button is-light add-button" type="button" style="display:none;">
                  Install App
                </button>
              </div>
            </div>
          {% endif %}

          {% if not user.is_authenticated %}
            <div class="navbar-item">
              <div class="buttons">
                <a class="button is-info" href="{% url 'signup' %}">
                  <strong>Sign up</strong>
                </a>
                <a class="button" href="{% url 'two_factor:login' %}">
                  Log in
                </a>
              </div>
            </div>
          {% endif %}
        </div>
      </div>
    </nav>
    <script>
      let deferredPrompt;
      const addBtn = document.querySelector('.add-button');
      if (addBtn) {
        addBtn.style.display = 'none';
      }
      window.addEventListener('beforeinstallprompt', (e) => {
        if (!addBtn) {
          return;
        }
        // Prevent Chrome 67 and earlier from automatically showing the prompt
        e.preventDefault();
        // Stash the event so it can be triggered later.
        deferredPrompt = e;
        // Update UI to notify the user they can add to home screen
        addBtn.style.display = 'block';

        addBtn.addEventListener('click', (e) => {
          // hide our user interface that shows our A2HS button
          addBtn.style.display = 'none';
          // Show the prompt
          deferredPrompt.prompt();
          // Wait for the user to respond to the prompt
          deferredPrompt.userChoice.then((choiceResult) => {
            if (choiceResult.outcome === 'accepted') {
              console.log('User accepted the A2HS prompt');
            } else {
              console.log('User dismissed the A2HS prompt');
            }
            deferredPrompt = null;
          });
        });
      });
    </script>

    {% block outer_content %}
    {% endblock %}
    {% block standard_page_shell %}
      <div class="gia-standard-page-shell">
        <div class="container">
          {% include "partials/settings-hierarchy-nav.html" %}
          {% block content_wrapper %}
            {% block content %}
            {% endblock %}
          {% endblock %}
        </div>
      </div>
    {% endblock %}
    <div id="modals-here">
    </div>
    <div id="windows-here" style="z-index: 120;">
    </div>
    <div id="widgets-here" style="display: none;">
    </div>
  </body>
</html>
