{% extends "base.html" %}
{% block title %}{{ deployment.name }} · iVistaar{% endblock %}
{% block content %}
<div class="page-head">
  <div>
    <h1>{{ deployment.name }}</h1>
    <p class="muted">
      <span class="status status-{{ deployment.status }}">{{ deployment.status }}</span>
      · connected folder
      · <a href="{{ deployment.public_url }}" target="_blank" rel="noopener">{{ deployment.public_url }}</a>
    </p>
  </div>
  <a class="btn" href="{{ url_for('admin.dashboard') }}">Back</a>
</div>

{% if dry_run %}
<p class="banner">Dry-run mode: IIS was not modified on this host.</p>
{% endif %}

{% if deployment.last_error %}
<pre class="error-box">{{ deployment.last_error }}</pre>
{% endif %}

<section class="card">
  <h2>Details</h2>
  <dl class="meta">
    <dt>Slug</dt><dd>{{ deployment.slug }}</dd>
    <dt>Connected folder</dt><dd><code>{{ deployment.source_path or '—' }}</code></dd>
    <dt>Build path</dt><dd><code>{{ deployment.app_path }}</code></dd>
    <dt>Entrypoint</dt><dd><code>{{ deployment.entrypoint }}</code></dd>
    <dt>Tracked sources</dt><dd>{{ deployment.last_py_bytes }} bytes · fingerprint {{ (deployment.last_fingerprint or '—')[:12] }}{% if deployment.last_fingerprint %}…{% endif %}</dd>
    <dt>Created</dt><dd>{{ deployment.created_at }}</dd>
    <dt>Updated</dt><dd>{{ deployment.updated_at }}</dd>
  </dl>
</section>

<section class="card">
  <h2>Auto-deploy</h2>
  <p class="muted">Watches the connected folder every {{ watch_interval }}s. When Python sources (or requirements.txt) change — including size increases — iVistaar records an event and redeploys automatically.</p>
  <form method="post" action="{{ url_for('admin.toggle_auto_deploy', slug=deployment.slug) }}" class="actions">
    <label class="radio">
      <input type="checkbox" name="auto_deploy" {% if deployment.auto_deploy %}checked{% endif %} onchange="this.form.submit()">
      Enable auto-deploy for this project
    </label>
  </form>
</section>

<section class="card">
  <h2>Actions</h2>
  <div class="actions wrap">
    <form method="post" action="{{ url_for('admin.redeploy', slug=deployment.slug) }}">
      <button type="submit" class="btn primary">Redeploy</button>
    </form>
    <form method="post" action="{{ url_for('admin.start', slug=deployment.slug) }}">
      <button type="submit" class="btn">Start</button>
    </form>
    <form method="post" action="{{ url_for('admin.stop', slug=deployment.slug) }}">
      <button type="submit" class="btn">Stop</button>
    </form>
    <a class="btn" href="{{ url_for('admin.events', slug=deployment.slug) }}">All events</a>
    <form method="post" action="{{ url_for('admin.delete', slug=deployment.slug) }}" onsubmit="return confirm('Delete {{ deployment.slug }}? This removes the IIS app, build files, and release history (your project folder is left untouched).');">
      <button type="submit" class="btn danger">Delete</button>
    </form>
  </div>
</section>

<section class="card">
  <h2>Releases</h2>
  <p class="muted">Successful deploys are archived so you can roll back.</p>
  {% if releases %}
  <table class="table">
    <thead>
      <tr>
        <th>Release</th>
        <th>When</th>
        <th>Size</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      {% for r in releases %}
      <tr>
        <td>
          <code>{{ r.label }}</code>
          {% if r.is_current %}<span class="status status-running">current</span>{% endif %}
        </td>
        <td>{{ r.created_at }}</td>
        <td>{{ r.py_bytes }} B</td>
        <td>
          {% if not r.is_current %}
          <form method="post" action="{{ url_for('admin.rollback', slug=deployment.slug, release_id=r.id) }}" onsubmit="return confirm('Roll back to {{ r.label }}?');" class="inline">
            <button type="submit" class="btn">Rollback</button>
          </form>
          {% else %}
          —
          {% endif %}
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  {% else %}
  <p class="muted">No archived releases yet.</p>
  {% endif %}
</section>

<section class="card">
  <h2>Recent events</h2>
  {% if events %}
  <table class="table">
    <thead>
      <tr>
        <th>When</th>
        <th>Actor</th>
        <th>Event</th>
        <th>Status</th>
        <th>Source mtime</th>
        <th>Owner</th>
        <th>Details</th>
      </tr>
    </thead>
    <tbody>
      {% for e in events %}
      <tr>
        <td>{{ e.created_at }}</td>
        <td><code>{{ e.actor or '—' }}</code></td>
        <td><span class="event-type">{{ e.event_type }}</span></td>
        <td><span class="status status-{{ e.status }}">{{ e.status }}</span></td>
        <td>{{ e.source_mtime or '—' }}</td>
        <td>{{ e.source_owner or '—' }}</td>
        <td class="msg-cell">{{ e.message or '—' }}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  {% else %}
  <p class="muted">No events for this project yet.</p>
  {% endif %}
</section>

<section class="card">
  <h2>Deploy log</h2>
  <pre class="log">{{ log_text }}</pre>
</section>
{% endblock %}
