<!-- 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="grid grid-cols-2 gap-x-2 gap-y-0 max-w-48">
            <!-- Row 1: Test -->
            <button
              onclick="testPrompt('{{ prompt.id }}')"
              class="col-span-2 flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md text-purple-600 hover:text-purple-900 hover:bg-purple-50 dark:text-purple-400 dark:hover:bg-purple-900/20 transition-colors"
              x-tooltip="'💡Test this Prompt with sample arguments'"
            >
              Test
            </button>

            <!-- Row 2: View | Edit -->
            <button onclick="viewPrompt('{{ prompt.id }}')" class="flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md text-indigo-600 hover:text-indigo-900 hover:bg-indigo-50 dark:text-indigo-400 dark:hover:bg-indigo-900/20 transition-colors">View</button>
            {% if can_modify %}
            <button onclick="editPrompt('{{ prompt.id }}')" class="flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md text-green-600 hover:text-green-900 hover:bg-green-50 dark:text-green-400 dark:hover:bg-green-900/20 transition-colors">Edit</button>
            {% endif %}

            {% if can_modify %}
            <!-- Row 3 & 4: Activate/Deactivate | Delete -->
            <div class="col-span-2 flex flex-col space-y-1">
              <form method="POST" action="{{ root_path }}/admin/prompts/{{ prompt.id }}/state" class="contents" onsubmit="return handleToggleSubmit(event, 'prompts')">
                <input type="hidden" name="activate" value="{{ 'false' if prompt.enabled else 'true' }}" />
                <button type="submit" class="flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md {% if prompt.enabled %}text-yellow-600 hover:text-yellow-900 hover:bg-yellow-50 dark:text-yellow-400 dark:hover:bg-yellow-900/20{% else %}text-green-600 hover:text-green-900 hover:bg-green-50 dark:text-green-400 dark:hover:bg-green-900/20{% endif %}">{% if prompt.enabled %}Deactivate{% else %}Activate{% endif %}</button>
              </form>
              <form method="POST" action="{{ root_path }}/admin/prompts/{{ prompt.id }}/delete" class="contents" onsubmit="return handleDeleteSubmit(event, 'prompt', '{{ prompt_label }}', 'prompts')">
                <button type="submit" class="flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md text-red-600 hover:text-red-900 hover:bg-red-50 dark:text-red-400 dark:hover:bg-red-900/20 transition-colors">Delete</button>
              </form>
            </div>
            {% endif %}
          </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>
        </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">{% 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_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>
