<%- include("partials/shared_header", { title: "User Token Lookup" }) %>
<h1>User Token Lookup</h1>
<p>Provide your user token to check your usage and quota information.</p>
<form action="/user/lookup" method="post">
  <input type="hidden" name="_csrf" value="<%= csrfToken %>" />
  <label for="token">User Token</label>
  <input type="password" name="token" value="<%= user?.token %>" />
  <input type="submit" value="Lookup" />
</form>
<% if (user) { %>
<hr />
<% if (user.type === "temporary" && Boolean(user.disabledAt)) { %>
<%- include("partials/shared_flash", { flashData: {
      type: "info",
      message: "This temporary user token has expired and is no longer usable. These records will be deleted soon.",
    } }) %>
<% } else if (user.disabledAt) { %>
<%- include("partials/shared_flash", { flashData: {
      type: "warning",
      message: "This user token has been disabled." + (user.disabledReason ? ` Reason: ${user.disabledReason}` : ""),
    } }) %>
<% } %>
<table class="striped">
  <tbody>
    <tr>
      <th scope="row">User Token</th>
      <td colspan="2"><code> <%= "..." + user.token.slice(-5) %> </code></td>
    </tr>

    <tr>
      <th scope="row">Nickname</th>
      <td><%= user.nickname ?? "none" %></td>
      <td class="actions">
        <a title="Edit" id="edit-nickname" href="#" onclick="updateNickname()">✏️</a>
      </td>
    </tr>
    <tr>
      <th scope="row">Type</th>
      <td colspan="2"><%= user.type %></td>
    </tr>
    <tr>
      <th scope="row">Prompts</th>
      <td colspan="2"><%= user.promptCount %></td>
    </tr>
    <tr>
      <th scope="row">Created At</th>
      <td colspan="2"><%= user.createdAt %></td>
    </tr>
    <tr>
      <th scope="row">Last Used At</th>
      <td colspan="2"><%= user.lastUsedAt || "never" %></td>
    </tr>
    <tr>
      <th scope="row">IPs<%= ipLimit ? ` (max ${ipLimit})` : "" %></th>
      <td colspan="2"><%- include("partials/shared_user_ip_list", { user, shouldRedact: true }) %></td>
    </tr>
    <% if (user.type === "temporary") { %>
    <tr>
      <th scope="row">Expires At</th>
      <td colspan="2"><%= user.expiresAt %></td>
    </tr>
    <% } %>
  </tbody>
</table>

<h3>Quota Information</h3>
<%- include("partials/shared_quota-info", { quota, user, showRefreshEdit: false }) %>

<form id="edit-nickname-form" style="display: none" action="/user/edit-nickname" method="post">
  <input type="hidden" name="_csrf" value="<%= csrfToken %>" />
  <input type="hidden" name="nickname" value="<%= user.nickname %>" />
</form>

<script>
  function updateNickname() {
    const form = document.getElementById("edit-nickname-form");
    const existing = form.nickname.value;
    const value = prompt("Enter a new nickname", existing);
    if (value !== null) {
      form.nickname.value = value;
      form.submit();
    }
  }
</script>

<% } %> <%- include("partials/user_footer") %>
