<!-- htmlhint doctype-first:false -->
<table id="prompts-table" class="min-w-full divide-y divide-gray-200">
  <thead class="bg-gray-50 dark:bg-gray-700">
    <tr>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Actions</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-12">S. No.</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-36">Gateway Name</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Name</th>
      <th id="prompt-id-header" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-48">Prompt ID</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Description</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-20">Tags</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-20">Owner</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-20">Team</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-20">Status</th>
    </tr>
  </thead>
  <tbody id="prompts-table-body" class="bg-white divide-y divide-gray-200 dark:bg-gray-900 dark:divide-gray-700">
    {% for prompt in data %}
      {% set prompt_label = prompt.displayName or prompt.originalName or prompt.name or prompt.id %}
      {% set prompt_gateway = prompt.gatewaySlug or 'Local' %}
      {% set prompt_tech_name = prompt.name or prompt.originalName or prompt.id %}
      {% set can_modify = is_admin or prompt.owner_email == current_user_email or prompt.ownerEmail == current_user_email or (prompt.visibility == 'team' and prompt.teamId and user_team_roles.get(prompt.teamId|string) == 'owner') or (prompt.visibility == 'team' and prompt.team_id and user_team_roles.get(prompt.team_id|string) == 'owner') %}
      <tr data-enabled="{{ prompt.enabled|lower }}" data-owner-email="{{ prompt.ownerEmail or prompt.owner_email }}" data-team-id="{{ prompt.teamId or prompt.team_id or '' }}" data-visibility="{{ prompt.visibility }}">
        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <div class="relative"
                     x-data="window.Admin?.overflowMenu?.('prompts-table-wrapper')"
               @click.away="menuOpen = false"
               @keydown.escape="menuOpen = false; $refs.trigger.focus()">
                  <button
                    type="button"
                    x-ref="trigger"
                    @click="menuOpen ? (menuOpen = false) : openMenu()"
                    class="p-1.5 rounded-md text-gray-500 hover:text-gray-700 hover:bg-gray-100 dark:text-gray-400 dark:hover:text-gray-200 dark:hover:bg-gray-700 transition-colors"
                    :aria-expanded="menuOpen"
                    aria-haspopup="menu"
                    aria-label="Actions"
                    x-tooltip="'Actions'"
                  >
                    <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
                      <path d="M6 10a2 2 0 11-4 0 2 2 0 014 0zm6 0a2 2 0 11-4 0 2 2 0 014 0zm6 0a2 2 0 11-4 0 2 2 0 014 0z"/>
                    </svg>
                  </button>
                  <!-- Dropdown menu (fixed-positioned to escape overflow/scroll containers) -->
                  <div
                    x-ref="menu"
                    x-cloak
                    x-show="menuOpen"
                    x-transition:enter="transition ease-out duration-100"
                    x-transition:enter-start="opacity-0 scale-95"
                    x-transition:enter-end="opacity-100 scale-100"
                    x-transition:leave="transition ease-in duration-75"
                    x-transition:leave-start="opacity-100 scale-100"
                    x-transition:leave-end="opacity-0 scale-95"
                    class="fixed w-44 bg-white dark:bg-gray-800 rounded-lg shadow-xl border border-gray-200 dark:border-gray-700 z-50 py-1"
                    :style="`top: ${menuTop}px; left: ${menuLeft}px; display: ${menuOpen ? 'block' : 'none'}`"
                    role="menu"
                    aria-orientation="vertical"
                    @keydown.arrow-down.prevent="navigate(1)"
                    @keydown.arrow-up.prevent="navigate(-1)"
                  >              <button
                type="button"
                role="menuitem"
                tabindex="0"
                data-test-prompt-id="{{ prompt.id }}"
                @click="menuOpen = false; window.Admin?.testPrompt?.({{ prompt.id|tojson_attr }})"
                class="w-full flex items-center px-4 py-2 text-sm text-gray-700 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700"
                x-tooltip="'💡Test this Prompt with sample arguments'"
              >Test</button>
              <button
                type="button"
                role="menuitem"
                tabindex="0"
                @click="menuOpen = false; window.Admin?.viewPrompt?.({{ prompt.id|tojson_attr }})"
                class="w-full flex items-center px-4 py-2 text-sm text-gray-700 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700"
              >View</button>
              {% if can_modify %}
              <button
                type="button"
                role="menuitem"
                tabindex="0"
                @click="menuOpen = false; window.Admin?.editPrompt?.({{ prompt.id|tojson_attr }})"
                class="w-full flex items-center px-4 py-2 text-sm text-gray-700 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700"
              >Edit</button>
              <hr class="border-gray-200 dark:border-gray-600 my-1">
              <form method="POST" action="{{ root_path }}/admin/prompts/{{ prompt.id }}/state" class="contents" @submit="menuOpen = false" data-action-submit="handleToggleSubmit" data-arg0="prompts">
                <input type="hidden" name="activate" value="{{ 'false' if prompt.enabled else 'true' }}" />
                <button
                  type="submit"
                  role="menuitem"
                  tabindex="0"
                  class="w-full flex items-center px-4 py-2 text-sm {% if prompt.enabled %}text-yellow-600 dark:text-yellow-400{% else %}text-green-600 dark:text-green-400{% endif %} hover:bg-gray-100 dark:hover:bg-gray-700"
                >{% if prompt.enabled %}Deactivate{% else %}Activate{% endif %}</button>
              </form>
              <form method="POST" action="{{ root_path }}/admin/prompts/{{ prompt.id }}/delete" class="contents" @submit="menuOpen = false" data-action-submit="handleDeleteSubmit" data-arg0="prompt" data-arg1="{{ prompt_label|tojson_attr }}" data-arg2="prompts">
                <button
                  type="submit"
                  role="menuitem"
                  tabindex="0"
                  class="w-full flex items-center px-4 py-2 text-sm text-red-600 dark:text-red-400 hover:bg-gray-100 dark:hover:bg-gray-700"
                >Delete</button>
              </form>
              {% endif %}
            </div>
          </div>
        </td>
        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 w-12 dark:text-gray-100">{{ (pagination.page - 1) * pagination.per_page + loop.index }}</td>
        <td class="px-6 py-4 whitespace-normal break-words text-sm text-gray-500 dark:text-gray-300 w-36">{{ prompt_gateway }}</td>
        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-gray-100">
          <div>{{ prompt_label }}</div>
          <div class="text-xs text-gray-500 dark:text-gray-400 font-mono">{{ prompt_tech_name }}</div>
          {% if prompt.title %}
          <div class="text-xs text-gray-500 dark:text-gray-400 italic">{{ prompt.title }}</div>
          {% endif %}
        </td>
        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-300 w-48" aria-labelledby="prompt-id-header">
          <div class="font-mono text-xs">{{ prompt.id }}</div>
        </td>
        <td class="px-6 py-4 whitespace-normal break-words text-sm text-gray-500 dark:text-gray-300">{% set clean_desc = (prompt.description or "") | decode_html | replace('\n', ' ') | replace('\r', ' ') %} {% set refactor_desc = clean_desc | striptags | trim %} {% if refactor_desc | length is greaterthan 220 %} {{ refactor_desc[:400] + "..." }} {% else %} {{ refactor_desc }} {% endif %}</td>
        <td class="px-6 py-4 whitespace-nowrap">{% if prompt.tags %} {% for tag in prompt.tags %}<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800 mr-1 mb-1" data-tag="{% if tag is mapping %}{{ tag.id }}{% else %}{{ tag }}{% endif %}">{% if tag is mapping %}{{ tag.id }}{% else %}{{ tag }}{% endif %}</span>{% endfor %} {% else %}<span class="text-gray-500 dark:text-gray-300 text-xs">None</span>{% endif %}</td>
        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-300 w-20">{{ prompt.ownerEmail or prompt.owner_email }}</td>
        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-300 w-20">{% if prompt.team %}{{ prompt.team }}{% else %}<span class="text-gray-400 dark:text-gray-600 text-xs">None</span>{% endif %}</td>
        <td class="px-6 py-4 whitespace-nowrap text-sm w-20">
          <div class="flex flex-col gap-1">
            <!-- Visibility badge -->
            {% if prompt.visibility == 'public' %}
            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200 w-fit">🌍 Public</span>
            {% elif prompt.visibility == 'team' %}
            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-indigo-100 text-indigo-800 dark:bg-indigo-900 dark:text-indigo-200 w-fit">👥 Team</span>
            {% else %}
            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-200 w-fit">🔒 Private</span>
            {% endif %}
            <!-- Status badge -->
            {% set enabled = prompt.enabled %}
            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium w-fit {% if enabled %}bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200{% else %}bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200{% endif %}">
              {% if enabled %}● Active{% else %}● Inactive{% endif %}
            </span>
          </div>
        </td>
      </tr>
  {% endfor %}
  </tbody>
</table>

<!-- Out-of-band swap for pagination controls -->
<div id="prompts-pagination-controls" hx-swap-oob="true">
  {% set base_url = root_path + '/admin/prompts/partial' %}
  {% set hx_target = '#prompts-table' %}
  {% set hx_swap = 'outerHTML' %}
  {% set hx_indicator = '#prompts-loading' %}
  {% set table_name = 'prompts' %}
  {% set query_params = query_params if query_params is defined else {'include_inactive': include_inactive} %}
  {% include 'pagination_controls.html' %}
</div>
