{% extends "framework_modern.html" %}

{% block content %}

  {% block action_modals %}
    {% include "modals/objects/edit_rule_modal.html" %}
    {% include "modals/objects/generic_set_aliases_modal.html" %}
    {% include "modals/objects/generic_set_scenario_modal.html" %}
    {% include "modals/objects/generic_copy_object_modal.html" %}
    {% include "modals/objects/generic_delete_modal.html" %}
  {% endblock action_modals %}

  <div class="space-y-2 p-4">
    <!-- Header Section -->
    <div class="card bg-base-100">
      <div class="card-body">
        <div class="flex items-center justify-between">
          <h2 class="card-title text-2xl">{{ rule.name }}</h2>
          <div id="actionsButton" class="dropdown dropdown-end hidden">
            <div tabindex="0" role="button" class="btn btn-ghost btn-sm">
              <svg
                xmlns="http://www.w3.org/2000/svg"
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                class="lucide lucide-wrench"
              >
                <path
                  d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"
                />
              </svg>
              Actions
            </div>
            <ul
              tabindex="-1"
              class="dropdown-content menu bg-base-100 rounded-box z-50 w-52 p-2"
            >
              {% block actions %}
                {% include "actions/objects/rule.html" %}
              {% endblock %}
            </ul>
          </div>
        </div>
        <p class="mt-2">{{ rule.description|safe }}</p>
      </div>
    </div>

    {% if rule.aliases %}
      <!-- Aliases -->
      <div class="collapse-arrow bg-base-200 collapse">
        <input type="checkbox" checked />
        <div class="collapse-title text-xl font-medium">Aliases</div>
        <div class="collapse-content">
          <ul class="menu bg-base-100 rounded-box">
            {% for alias in rule.aliases %}
              <li><a class="hover:bg-base-200">{{ alias }}</a></li>
            {% endfor %}
          </ul>
        </div>
      </div>
    {% endif %}

    <!-- Reaction Patterns -->
    <div class="collapse-arrow bg-base-200 collapse">
      <input type="checkbox" checked />
      <div class="collapse-title text-xl font-medium">Reaction Patterns</div>
      <div class="collapse-content">
        <div class="space-y-4">
          {% for r in rule.srs %}
            <div class="card bg-base-100">
              <div class="card-body">
                <a href="{{ r.url }}" class="link link-primary font-semibold"
                  >{{ r.name }}</a
                >
                <div class="mt-2 flex justify-center">{{ r.as_svg|safe }}</div>
              </div>
            </div>
          {% endfor %}
        </div>
      </div>
    </div>

    <!-- Scenarios -->
    {% if rule.scenarios.all %}
      <div class="collapse-arrow bg-base-200 collapse">
        <input type="checkbox" checked />
        <div class="collapse-title text-xl font-medium">Scenarios</div>
        <div class="collapse-content">
          <ul class="menu bg-base-100 rounded-box">
            {% for s in rule.scenarios.all %}
              <li>
                <a href="{{ s.url }}" class="hover:bg-base-200"
                  >{{ s.name }} <i>({{ s.package.name }})</i></a
                >
              </li>
            {% endfor %}
          </ul>
        </div>
      </div>
    {% endif %}

    {% if rule.enzymelinks %}
      <!-- EC Numbers -->
      <div class="collapse-arrow bg-base-200 collapse">
        <input type="checkbox" checked />
        <div class="collapse-title text-xl font-medium">EC Numbers</div>
        <div class="collapse-content">
          <div class="space-y-2">
            {% for k, v in rule.get_grouped_enzymelinks.items %}
              <div class="collapse-arrow bg-base-100 collapse">
                <input type="checkbox" checked />
                <div class="collapse-title text-lg font-medium">{{ k }}</div>
                <div class="collapse-content">
                  <ul class="menu bg-base-200 rounded-box">
                    {% for enzyme in v %}
                      <li>
                        <a href="{{ enzyme.url }}" class="hover:bg-base-300">
                          <div class="flex w-full items-center justify-between">
                            <span>{{ enzyme.ec_number }}</span>
                            <span class="text-sm opacity-70"
                              >{{ enzyme.linking_method }}</span
                            >
                          </div>
                          <div class="text-sm opacity-60">
                            {{ enzyme.name }}
                          </div>
                        </a>
                      </li>
                    {% endfor %}
                  </ul>
                </div>
              </div>
            {% endfor %}
          </div>
        </div>
      </div>
    {% endif %}
  </div>

  <script>
    // Show actions button if there are actions
    document.addEventListener("DOMContentLoaded", function () {
      const actionsButton = document.getElementById("actionsButton");
      const actionsList = actionsButton?.querySelector("ul");
      if (actionsList && actionsList.children.length > 0) {
        actionsButton?.classList.remove("hidden");
      }
    });
  </script>
{% endblock content %}
