{% extends "app_home/base.html" %}
{% load static %}
{% load i18n %}

{% block navbar_app %}
{% include 'app_home/_sidebar.html' %}
{% endblock %}

{% block title %}
<h3>{% translate "Admin tools" %}</h3>
<p class="text-muted">Dangerous tools ahead !</p>
{% endblock %}


{% block content %}

<hr>


<div class="row"> <!-- Counter / Bigset -->
  <div class="col-md-2 d-flex align-items-center justify-content-center">
    <button type="button" class="btn btn-primary btn-lg w-100" data-bs-toggle="modal" data-bs-target="#updateBigsetModal">
      <i class="fa fa-refresh"></i> {% translate "Counters" %}
    </button>
  </div>

  <div class="col-md-10">
    <div class="alert alert-primary">
      <p>{% translate "Manually update all Schema counters and bigset status." %}</p>
    </div>
  </div>
</div>




<hr>

<div class="row"> <!-- EAV Refresh -->
  <div class="col-md-2 d-flex align-items-center justify-content-center">
    <button type="button" class="btn btn-warning btn-lg w-100" data-bs-toggle="modal" data-bs-target="#eavRefreshModal">
      <i class="fa fa-database"></i> {% translate "EAV Refresh" %}
    </button>
  </div>

  <div class="col-md-10">
    <div class="alert alert-warning">
      <p>{% translate "Refresh the EAV cache for improved performance with large datasets." %}</p>
    </div>
  </div>
</div>

<hr>

<div class="row"> <!-- EAV Purge -->
  <div class="col-md-2 d-flex align-items-center justify-content-center">
    <button type="button" class="btn btn-warning btn-lg w-100" data-bs-toggle="modal" data-bs-target="#eavPurgeModal">
      <i class="fa fa-trash"></i> {% translate "EAV Purge" %}
    </button>
  </div>

  <div class="col-md-10">
    <div class="alert alert-warning">
      <p>{% translate "Purge the EAV cache to free up storage space. Cache will be rebuilt on next use." %}</p>
    </div>
  </div>
</div>

<hr>

<div class="row"> <!-- Revision -->
  <div class="col-md-2 d-flex align-items-center justify-content-center">
    <a href="{% url 'app_home:admintools_revision' %}" class="btn btn-info btn-lg w-100" role="button">
      <i class="fa fa-history"></i> {% translate "Revision" %}
    </a>
  </div>

  <div class="col-md-10">
    <div class="alert alert-info">
      <p>{% translate "View and manage data revision tracking and history." %}</p>
    </div>
  </div>
</div>

<hr>

<div class="row"> <!-- DB FACTORY RESET -->
  <div class="col-md-2 d-flex align-items-center justify-content-center">
    <button type="button" class="btn btn-danger btn-lg w-100" data-bs-toggle="modal" data-bs-target="#factoryResetModal">
      <i class="fa fa-tools"></i> {% translate "DB Reset" %}
    </button>
  </div>

  <div class="col-md-10">
    <div class="alert alert-danger">
      <p>{% translate "DANGER ! - Empty database, remove all schemas, fields, instances, users, groups, etc.  ALL DATA IS REMOVED !" %}</p>
    </div>
  </div>
</div>

<!-- MODALS  -->
<!-- Factory Reset Confirmation Modal -->
<div class="modal fade" id="factoryResetModal" tabindex="-1" aria-labelledby="factoryResetLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content border-danger">
      <div class="modal-header bg-danger text-white">
        <h5 class="modal-title" id="factoryResetLabel">
          <i class="fa fa-exclamation-triangle"></i> {% translate "Confirm Factory Reset" %}
        </h5>
        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <p><strong>{% translate "Warning: This action cannot be undone!" %}</strong></p>
        <p>{% translate "Factory reset will:" %}</p>
        <ul>
          <li>{% translate "Delete all configuration data" %}</li>
          <li>{% translate "Reset the database to initial state" %}</li>
          <li>{% translate "Remove all user-created content" %}</li>
        </ul>
        <p class="text-danger">{% translate "Are you sure you want to proceed?" %}</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% translate "Cancel" %}</button>
        <form method="post" action="{% url 'app_home:admintools' %}" class="cv-form-inline">
          {% csrf_token %}
          <input type="hidden" name="action" value="factory_reset">
          <button type="submit" class="btn btn-danger">
            <i class="fa fa-exclamation-triangle"></i> {% translate "Confirm Factory Reset" %}
          </button>
        </form>
      </div>
    </div>
  </div>
</div>

<!-- Update BigSet Confirmation Modal -->
<div class="modal fade" id="updateBigsetModal" tabindex="-1" aria-labelledby="updateBigsetLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content border-primary">
      <div class="modal-header bg-primary text-dark">
        <h5 class="modal-title" id="updateBigsetLabel">
          <i class="fa fa-refresh"></i> {% translate "Update BigSet Cache" %}
        </h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <p><strong>{% translate "This will update Counters/Bigsetvalues for all schemas." %}</strong></p>
        <p>{% translate "This operation may take some time depending on the amount of data." %}</p>
        <p>{% translate "Are you sure you want to proceed?" %}</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% translate "Cancel" %}</button>
        <form method="post" action="{% url 'app_home:admintools_updatebigset' %}" class="cv-form-inline">
          {% csrf_token %}
          <button type="submit" class="btn btn-primary">
            <i class="fa fa-refresh"></i> {% translate "Update Schema counters" %}
          </button>
        </form>
      </div>
    </div>
  </div>
</div>

<!-- EAV Refresh Confirmation Modal -->
<div class="modal fade" id="eavRefreshModal" tabindex="-1" aria-labelledby="eavRefreshLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content border-warning">
      <div class="modal-header bg-warning text-dark">
        <h5 class="modal-title" id="eavRefreshLabel">
          <i class="fa fa-database"></i> {% translate "Refresh EAV Cache" %}
        </h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <p><strong>{% translate "This will refresh the EAV cache for all schemas." %}</strong></p>
        <p>{% translate "The EAV (Entity-Attribute-Value) cache stores denormalized data for improved performance." %}</p>
        <p>{% translate "This operation may take some time depending on the amount of data." %}</p>
        <p>{% translate "Are you sure you want to proceed?" %}</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% translate "Cancel" %}</button>
        <form method="post" action="{% url 'app_home:admintools_eavrefresh' %}" class="cv-form-inline">
          {% csrf_token %}
          <button type="submit" class="btn btn-warning">
            <i class="fa fa-database"></i> {% translate "Refresh EAV Cache" %}
          </button>
        </form>
      </div>
    </div>
  </div>
</div>

<!-- EAV Purge Confirmation Modal -->
<div class="modal fade" id="eavPurgeModal" tabindex="-1" aria-labelledby="eavPurgeLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content border-warning">
      <div class="modal-header bg-warning text-dark">
        <h5 class="modal-title" id="eavPurgeLabel">
          <i class="fa fa-trash"></i> {% translate "Purge EAV Cache" %}
        </h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <p><strong>{% translate "This will purge the EAV cache for all schemas." %}</strong></p>
        <p>{% translate "The EAV cache will be deleted, freeing up storage space." %}</p>
        <p>{% translate "The cache will be automatically rebuilt on next use (performance may be reduced until rebuild completes)." %}</p>
        <p>{% translate "Are you sure you want to proceed?" %}</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% translate "Cancel" %}</button>
        <form method="post" action="{% url 'app_home:admintools_eavpurge' %}" class="cv-form-inline">
          {% csrf_token %}
          <button type="submit" class="btn btn-warning">
            <i class="fa fa-trash"></i> {% translate "Purge EAV Cache" %}
          </button>
        </form>
      </div>
    </div>
  </div>
</div>

{% endblock %}
