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

{% block title %}{% trans "Sources" %}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3 mb-0">{% trans "Integration Sources" %}</h1>
                <a href="{% url 'source_create' %}" class="btn btn-main-action">
                    {% trans "Create Source for Integration" %} <i class="fas fa-plus"></i>
                </a>
            </div>

            <!-- Search and Filters -->
            <div class="card mb-4">
                <div class="card-body">
                    <form method="get" class="row g-3">
                        <div class="col-md-8">
                            <div class="input-group">
                                <span class="input-group-text">
                                    <i class="fas fa-search"></i>
                                </span>
                                <input type="text" class="form-control" name="q"
                                       placeholder="Search sources..." value="{{ search_query }}">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <button type="submit" class="btn btn-outline-primary">
                                <i class="fas fa-search"></i> {% trans "Search" %}
                            </button>
                            {% if search_query %}
                                <a href="{% url 'source_list' %}" class="btn btn-outline-secondary">
                                    <i class="fas fa-times"></i> {% trans "Clear" %}
                                </a>
                            {% endif %}
                        </div>
                    </form>
                </div>
            </div>

            <!-- Results Summary -->
            {% if search_query %}
                <div class="alert alert-info">
                    Found {{ total_sources }} source{{ total_sources|pluralize }} matching "{{ search_query }}"
                </div>
            {% endif %}

            <!-- Sources Table -->
            <div class="card">
                <div class="card-body">
                    {% if page_obj %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>{% trans "Name" %}</th>
                                        <th>{% trans "Type" %}</th>
                                        <th>{% trans "Status" %}</th>
                                        <th>{% trans "API Key" %}</th>
                                        <th>{% trans "Created" %}</th>
                                        <th>{% trans "Actions" %}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for source in page_obj %}
                                        <tr>
                                            <td>
                                                <a href="{% url 'source_detail' source.pk %}" class="text-decoration-none">
                                                    <strong>{{ source.name }}</strong>
                                                </a>

                                            </td>
                                            <td>
                                                <span class="badge bg-info">{{ source.source_type }}</span>
                                            </td>
                                            <td>
                                                {% if source.is_active %}
                                                    <span class="badge bg-success">{% trans "Active" %}</span>
                                                {% else %}
                                                    <span class="badge bg-secondary">{% trans "Inactive" %}</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <code class="small">{{ source.api_key|truncatechars:20 }}</code>
                                            </td>
                                            <td>
                                                <small class="text-muted">{{ source.created_at|date:"M d, Y" }}</small>
                                            </td>
                                            <td>
                                                <div class="btn-group" role="group">
                                                    <a href="{% url 'source_detail' source.pk %}"
                                                       class="btn btn-sm btn-outline-primary" title="View">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                    <a href="{% url 'source_update' source.pk %}"
                                                       class="btn btn-sm btn-outline-secondary" title="Edit">
                                                        <i class="fas fa-edit"></i>
                                                    </a>
                                                    {% comment %} <button type="button"
                                                            class="btn btn-sm btn-outline-warning"
                                                            hx-post="{% url 'toggle_source_status' source.pk %}"
                                                            hx-confirm="Are you sure you want to {{ source.is_active|yesno:'deactivate,activate' }} this source?"
                                                            title="{{ source.is_active|yesno:'Deactivate,Activate' }}">
                                                        <i class="fas fa-{{ source.is_active|yesno:'pause,play' }}"></i>
                                                    </button> {% endcomment %}
                                                    {% comment %} <a href="{% url 'source_delete' source.pk %}"
                                                       class="btn btn-sm btn-outline-danger" title="Delete">
                                                        <i class="fas fa-trash"></i>
                                                    </a> {% endcomment %}
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        {% if page_obj.has_other_pages %}
                            <nav aria-label="Sources pagination">
                                <ul class="pagination justify-content-center">
                                    {% if page_obj.has_previous %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page=1{% if search_query %}&q={{ search_query }}{% endif %}">
                                                <i class="fas fa-angle-double-left"></i>
                                            </a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&q={{ search_query }}{% endif %}">
                                                <i class="fas fa-angle-left"></i>
                                            </a>
                                        </li>
                                    {% endif %}

                                    {% for num in page_obj.paginator.page_range %}
                                        {% if page_obj.number == num %}
                                            <li class="page-item active">
                                                <span class="page-link">{{ num }}</span>
                                            </li>
                                        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                            <li class="page-item">
                                                <a class="page-link" href="?page={{ num }}{% if search_query %}&q={{ search_query }}{% endif %}">{{ num }}</a>
                                            </li>
                                        {% endif %}
                                    {% endfor %}

                                    {% if page_obj.has_next %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&q={{ search_query }}{% endif %}">
                                                <i class="fas fa-angle-right"></i>
                                            </a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&q={{ search_query }}{% endif %}">
                                                <i class="fas fa-angle-double-right"></i>
                                            </a>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}
                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-database fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">{% trans "No sources found" %}</h5>
                            <p class="text-muted">
                                {% if search_query %}
                                    {% blocktrans with query=query %}No sources match your search criteria "{{ query }}".{% endblocktrans %}
                                {% else %}
                                    {% trans "Get started by creating your first source." %}
                                {% endif %}
                            </p>
                            <a href="{% url 'source_create' %}" class="btn btn-main-action">
                                <i class="fas fa-plus"></i> {% trans "Create Source" %}
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// Auto-refresh after status toggle
document.body.addEventListener('htmx:afterRequest', function(evt) {
    if (evt.detail.successful) {
        // Reload the page after a short delay to show updated status
        setTimeout(() => {
            window.location.reload();
        }, 500);
    }
});
</script>
{% endblock %}
