{% extends "base.html" %}
{% block content %}
  <section class="section">
    <div class="container gia-page-shell">
      <div class="gia-page-header">
        <div>
          <h1 class="title is-4">Task Inbox</h1>
          <p class="subtitle is-6">Canonical tasks live in GIA. Chats, XMPP, web UI, and agent tooling all operate on the same records.</p>
        </div>
      </div>
      <div class="buttons" style="margin-bottom: 0.75rem;">
        <a class="button is-small is-link is-light" href="{% url 'tasks_settings' %}{% if scope.person_id or scope.service or scope.identifier %}?{% if scope.person_id %}person={{ scope.person_id|urlencode }}{% endif %}{% if scope.service %}{% if scope.person_id %}&{% endif %}service={{ scope.service|urlencode }}{% endif %}{% if scope.identifier %}{% if scope.person_id or scope.service %}&{% endif %}identifier={{ scope.identifier|urlencode }}{% endif %}{% endif %}">Task Automation</a>
      </div>
      <div class="columns is-variable is-5">
        <div class="column is-4">
          <article class="box">
            <div class="is-flex is-justify-content-space-between is-align-items-center" style="gap: 0.5rem; margin-bottom: 0.6rem;">
              <h2 class="title is-6" style="margin: 0;">Projects</h2>
              <span class="tag task-ui-badge">{{ projects|length }}</span>
            </div>
            <p class="help" style="margin-bottom: 0.45rem;">Projects are created automatically from chat usage. Use this panel for manual cleanup and mapping.</p>
            <div class="buttons" style="margin-bottom:0.55rem;">
              {% if show_empty_projects %}
                <a class="button is-small is-light" href="{% url 'tasks_hub' %}">Hide empty projects</a>
              {% else %}
                <a class="button is-small is-light" href="{% url 'tasks_hub' %}?show_empty=1">Show empty projects</a>
              {% endif %}
            </div>
            <form method="post" style="margin-bottom: 0.75rem;">
              {% csrf_token %}
              <input type="hidden" name="action" value="project_create">
              <input type="hidden" name="person" value="{{ scope.person_id }}">
              <input type="hidden" name="service" value="{{ scope.service }}">
              <input type="hidden" name="identifier" value="{{ scope.identifier }}">
              <div class="field has-addons">
                <div class="control is-expanded">
                  <input class="input is-small" name="name" placeholder="New project name">
                </div>
                <div class="control">
                  <button class="button is-small is-link is-light" type="submit">Add</button>
                </div>
              </div>
            </form>

            {% if scope.person %}
              <article class="message is-light" style="margin-bottom: 0.75rem;">
                <div class="message-body is-size-7">
                  Setup scope: <strong>{{ scope.person.name }}</strong>
                  {% if scope.service and scope.identifier %}
                    · {{ scope.service }} · {{ scope.identifier }}
                  {% endif %}
                </div>
              </article>
              <div style="margin-bottom: 0.75rem;">
                <label class="label is-size-7">Map Linked Identifiers To Project</label>
                <form method="get">
                  <input type="hidden" name="person" value="{{ scope.person_id }}">
                  <input type="hidden" name="service" value="{{ scope.service }}">
                  <input type="hidden" name="identifier" value="{{ scope.identifier }}">
                  <div class="field has-addons">
                    <div class="control is-expanded">
                      <div class="select is-small is-fullwidth">
                        <select name="project">
                          <option value="">Select project</option>
                          {% for project in project_choices %}
                            <option value="{{ project.id }}" {% if selected_project and selected_project.id == project.id %}selected{% endif %}>{{ project.name }}</option>
                          {% endfor %}
                        </select>
                      </div>
                    </div>
                    <div class="control">
                      <button class="button is-small is-light" type="submit">Select</button>
                    </div>
                  </div>
                </form>
              </div>
              <table class="table is-fullwidth is-striped is-size-7" style="margin-bottom:0.9rem;">
                <thead><tr><th>Identifier</th><th>Service</th><th></th></tr></thead>
                <tbody>
                  {% for row in person_identifier_rows %}
                    <tr>
                      <td><code>{{ row.identifier }}</code></td>
                      <td>{{ row.service }}</td>
                      <td class="has-text-right">
                        {% if selected_project %}
                          {% if row.mapped %}
                            <span class="tag task-ui-badge">Linked</span>
                          {% else %}
                            <form method="post">
                              {% csrf_token %}
                              <input type="hidden" name="action" value="project_map_identifier">
                              <input type="hidden" name="project_id" value="{{ selected_project.id }}">
                              <input type="hidden" name="person_identifier_id" value="{{ row.id }}">
                              <input type="hidden" name="person" value="{{ scope.person_id }}">
                              <input type="hidden" name="service" value="{{ scope.service }}">
                              <input type="hidden" name="identifier" value="{{ scope.identifier }}">
                              <button class="button is-small is-light" type="submit">Link</button>
                            </form>
                          {% endif %}
                        {% else %}
                          <span class="has-text-grey">Select project</span>
                        {% endif %}
                      </td>
                    </tr>
                  {% empty %}
                    <tr><td colspan="3">No linked identifiers for this person yet.</td></tr>
                  {% endfor %}
                </tbody>
              </table>
            {% else %}
              <p class="help" style="margin-bottom: 0.75rem;">
                Open this page from Compose to map a person’s linked identifiers in one click.
              </p>
            {% endif %}

            <table class="table is-fullwidth is-striped is-size-7">
              <thead><tr><th>Project</th><th>Stats</th><th></th></tr></thead>
              <tbody>
                {% for project in projects %}
                  <tr>
                    <td>
                      <a href="{% url 'tasks_project' project_id=project.id %}">{{ project.name }}</a>
                    </td>
                    <td>
                      <span class="tag task-ui-badge">{{ project.task_count }} task{{ project.task_count|pluralize }}</span>
                      <span class="tag task-ui-badge">{{ project.epic_count }} epic{{ project.epic_count|pluralize }}</span>
                    </td>
                    <td class="has-text-right">
                      <form method="post" onsubmit="const v=prompt('Type {{ project.name|escapejs }} to confirm delete'); if(v===null){return false;} this.confirm_name.value=v; return true;">
                        {% csrf_token %}
                        <input type="hidden" name="action" value="project_delete">
                        <input type="hidden" name="project_id" value="{{ project.id }}">
                        <input type="hidden" name="confirm_name" value="">
                        <button class="button is-small is-danger is-light" type="submit">Delete</button>
                      </form>
                    </td>
                  </tr>
                {% empty %}
                  <tr><td colspan="3">No projects yet.</td></tr>
                {% endfor %}
              </tbody>
            </table>
          </article>
        </div>
        <div class="column">
          <article class="box">
            <div class="is-flex is-justify-content-space-between is-align-items-center" style="gap: 0.5rem; margin-bottom: 0.6rem; flex-wrap: wrap;">
              <h2 class="title is-6" style="margin: 0;">Create Task</h2>
              {% if scope.service or scope.identifier %}
                <span class="tag task-ui-badge">{{ scope.service|default:"web" }}{% if scope.identifier %} · {{ scope.identifier }}{% endif %}</span>
              {% else %}
                <span class="tag task-ui-badge">web</span>
              {% endif %}
            </div>
            <p class="help" style="margin-bottom: 0.65rem;">Use this to create canonical tasks directly from the web UI without relying on WhatsApp-derived source state.</p>
            <form method="post">
              {% csrf_token %}
              <input type="hidden" name="action" value="task_create">
              <input type="hidden" name="person" value="{{ scope.person_id }}">
              <input type="hidden" name="service" value="{{ scope.service }}">
              <input type="hidden" name="identifier" value="{{ scope.identifier }}">
              <div class="columns is-multiline">
                <div class="column is-7">
                  <label class="label is-size-7">Title</label>
                  <input class="input is-small" name="title" placeholder="Ship MCP browser validation for compose">
                </div>
                <div class="column is-5">
                  <label class="label is-size-7">Project</label>
                  <div class="select is-small is-fullwidth">
                    <select name="project_id">
                      {% for project in project_choices %}
                        <option value="{{ project.id }}" {% if selected_project and selected_project.id == project.id %}selected{% endif %}>{{ project.name }}</option>
                      {% endfor %}
                    </select>
                  </div>
                </div>
                <div class="column is-5">
                  <label class="label is-size-7">Epic (optional)</label>
                  <div class="select is-small is-fullwidth">
                    <select name="epic_id">
                      <option value="">No epic</option>
                      {% for epic in epic_choices %}
                        <option value="{{ epic.id }}">{{ epic.project.name }} / {{ epic.name }}</option>
                      {% endfor %}
                    </select>
                  </div>
                </div>
                <div class="column is-3">
                  <label class="label is-size-7">Due Date</label>
                  <input class="input is-small" type="date" name="due_date">
                </div>
                <div class="column is-4">
                  <label class="label is-size-7">Assignee</label>
                  <input class="input is-small" name="assignee_identifier" placeholder="@operator">
                </div>
              </div>
              <button class="button is-small is-link" type="submit">Create Task</button>
            </form>
          </article>
          <article class="box">
            <h2 class="title is-6">Recent Derived Tasks</h2>
            <table class="table is-fullwidth is-striped is-size-7">
              <thead><tr><th>Ref</th><th>Title</th><th>Created By</th><th>Project</th><th>Status</th><th>Actions</th></tr></thead>
              <tbody>
                {% for row in tasks %}
                  <tr>
                    <td>#{{ row.reference_code }}</td>
                    <td>{{ row.title }}</td>
                    <td>
                      {{ row.creator_label|default:"Unknown" }}
                      {% if row.creator_identifier %}
                        <div class="has-text-grey"><code>{{ row.creator_identifier }}</code></div>
                      {% endif %}
                    </td>
                    <td>{{ row.project.name }}{% if row.epic %} / {{ row.epic.name }}{% endif %}</td>
                    <td>{{ row.status_snapshot }}</td>
                    <td>
                      <a class="button is-small is-light" href="{% url 'tasks_task' task_id=row.id %}">Open</a>
                    </td>
                  </tr>
                {% empty %}
                  <tr><td colspan="6">No derived tasks yet.</td></tr>
                {% endfor %}
              </tbody>
            </table>
          </article>
        </div>
      </div>
    </div>
  </section>
{% endblock %}
