<header class="page-heading">
  <p class="eyebrow">CARTOGRAFÍA</p>
  <h1 id="galaxy-map-title">Galaxia {{ selected_galaxy }}, sistema {{ selected_system }}</h1>
  <p>Consulta planetas, lunas, campos de escombros y anomalías conocidas en las quince posiciones del sistema.</p>
  <p>Topología: sistemas {{ 'circulares' if config.get('system_topology','linear') == 'circular' else 'lineales' }} y galaxias {{ 'circulares' if config.get('galaxy_topology','linear') == 'circular' else 'lineales' }}. En una topología circular, el último índice enlaza con el primero para todas las flotas y la IA.</p>
</header>

<form class="system-jump panel" method="get" aria-label="Cambiar sistema galáctico">
  <label for="galaxy">Galaxia</label>
  <input id="galaxy" name="galaxy" type="number" min="1" max="{{ config.get('galaxy_count',1) }}" value="{{ selected_galaxy }}">
  <label for="system">Sistema</label>
  <input id="system" name="system" type="number" min="1" max="{{ game.galaxy_size }}" value="{{ selected_system }}">
  <button class="button" type="submit">Explorar sistema</button>
  <div class="pager" aria-label="Sistemas contiguos">
    <a class="button button-secondary" href="?galaxy={{ selected_galaxy }}&amp;system={{ previous_system }}">Sistema anterior, {{ previous_system }}</a>
    <a class="button button-secondary" href="?galaxy={{ selected_galaxy }}&amp;system={{ next_system }}">Sistema siguiente, {{ next_system }}</a>
    {% if config.get('galaxy_count',1)|int > 1 %}<a class="button button-secondary" href="?galaxy={{ previous_galaxy }}&amp;system={{ selected_system }}">Galaxia anterior, {{ previous_galaxy }}</a><a class="button button-secondary" href="?galaxy={{ next_galaxy }}&amp;system={{ selected_system }}">Galaxia siguiente, {{ next_galaxy }}</a>{% endif %}
  </div>
</form>

<div class="table-wrap galaxy-table">
  <table>
    <caption>
      Posiciones de [{{ selected_galaxy }}:{{ selected_system }}]:
      {{ detected_count }} {{ 'planeta' if detected_count == 1 else 'planetas' }},
      {{ system_moons|length }} {{ 'luna' if system_moons|length == 1 else 'lunas' }},
      {{ system_debris|length }} {{ 'campo de escombros' if system_debris|length == 1 else 'campos de escombros' }} y
      {{ system_anomalies|length }} {{ 'anomalía conocida' if system_anomalies|length == 1 else 'anomalías conocidas' }}.
    </caption>
    <thead><tr>
      <th scope="col">Posición</th><th scope="col">Objeto</th><th scope="col">Imperio</th>
      <th scope="col">Luna</th><th scope="col">Escombros</th><th scope="col">Puntos</th><th scope="col">Acciones</th>
    </tr></thead>
    <tbody>
      {% for position, found in system_positions %}
        {% set anomaly = system_anomalies.get(position) %}
        {% set debris = system_debris.get(position) %}
        {% set moon = system_moons.get(position) %}
        <tr>
          <th scope="row">{{ position }}</th>
          <td>
            {% if found %}<strong>{{ found.name }}</strong>{% elif anomaly %}<strong>{{ anomaly.name }}</strong>{% else %}<span class="muted">Espacio vacío</span>{% endif %}
            {% if anomaly %}<small class="anomaly-note">Anomalía {{ anomaly.kind }}, dificultad {{ anomaly.difficulty }}</small>{% endif %}
          </td>
          <td>
            {% if found %}<span class="faction-dot" style="--faction-color:{{ found.color }}" aria-hidden="true"></span>{{ found.empire_name }}{% if found.is_human %} (tú){% endif %}<small class="galaxy-secondary">Comandante: {{ found.commander }}</small>{% else %}<span class="muted">Sin imperio</span>{% endif %}
          </td>
          <td>
            {% if moon %}<strong>{{ moon.name }}</strong><small class="galaxy-secondary">{{ moon.diameter|number }} km · {{ moon.fields_max }} campos</small>{% else %}<span class="muted">Sin luna</span>{% endif %}
          </td>
          <td>
            {% if debris %}<strong>{{ (debris.metal + debris.crystal + debris.deuterium)|number }} unidades</strong><small class="galaxy-secondary">{{ debris.metal|number }} metal · {{ debris.crystal|number }} cristal · {{ debris.deuterium|number }} deuterio</small>{% else %}<span class="muted">Sin escombros</span>{% endif %}
          </td>
          <td>{{ found.score|number if found else '—' }}</td>
          <td>
            {% if found and not found.is_human %}
              <a href="{{ url_for('game_view', section='fleet', galaxy=selected_galaxy, system=selected_system, position=position) }}">Enviar misión</a>
              <form method="post" action="{{ url_for('bookmark_save_action') }}" class="inline-compact-form"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="planet_id" value="{{ found.id }}"><input type="hidden" name="priority" value="observe"><input type="hidden" name="label" value="{{ found.name }}"><button class="link-button" type="submit">Guardar objetivo</button></form>
            {% elif found %}<span>Posición propia</span>
            {% elif not anomaly %}<a href="{{ url_for('game_view', section='fleet', galaxy=selected_galaxy, system=selected_system, position=position, mission='colonize') }}">Colonizar</a>{% endif %}
            {% if debris %}<span aria-hidden="true"> · </span><a href="{{ url_for('game_view', section='fleet', galaxy=selected_galaxy, system=selected_system, position=position, mission='recycle') }}">Reciclar campo</a>{% endif %}
            {% if anomaly %}<span aria-hidden="true"> · </span><a href="{{ url_for('game_view', section='expeditions', galaxy=selected_galaxy, system=selected_system, position=position) }}">Investigar anomalía</a>{% endif %}
          </td>
        </tr>
      {% endfor %}
    </tbody>
  </table>
</div>
