{% extends "base.html" %}

{% block title %}Contact Management - mc-webui{% endblock %}

{% block extra_head %}
{% endblock %}

{% block content %}
<div class="container-fluid px-3 py-4">
    <div class="row">
        <div class="col-12 col-lg-8 mx-auto">
            <!-- Page Header -->
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2 class="mb-0">
                    <i class="bi bi-person-check"></i> Contact Management
                </h2>
                <button class="btn btn-outline-secondary" onclick="window.history.back();" title="Go back">
                    <i class="bi bi-arrow-left"></i>
                </button>
            </div>

            <!-- Manual Approval Settings Section (Compact) -->
            <div class="compact-setting">
                <div class="form-check form-switch mb-0 d-flex align-items-center gap-2">
                    <input class="form-check-input" type="checkbox" role="switch" id="manualApprovalSwitch" style="cursor: pointer; min-width: 3rem; min-height: 1.5rem;">
                    <label class="form-check-label mb-0" for="manualApprovalSwitch" style="cursor: pointer; font-weight: 500;">
                        <span id="switchLabel">Loading...</span>
                    </label>
                </div>
                <i class="bi bi-info-circle info-icon"
                   data-bs-toggle="tooltip"
                   data-bs-placement="top"
                   title="When enabled, new contacts must be manually approved before they can communicate with your node"></i>
            </div>

            <!-- Pending Contacts Section (Compact) -->
            <div class="mb-3">
                <div class="d-flex justify-content-between align-items-center section-compact">
                    <h6 class="mb-0">
                        <i class="bi bi-hourglass-split"></i> Pending Contacts
                        <span class="badge bg-primary rounded-pill" id="pendingCount" style="display: none;">0</span>
                    </h6>
                    <button class="btn btn-sm btn-outline-primary" id="refreshPendingBtn">
                        <i class="bi bi-arrow-clockwise"></i>
                    </button>
                </div>

                <!-- Loading State -->
                <div id="pendingLoading" class="text-center py-2" style="display: none;">
                    <div class="spinner-border spinner-border-sm text-primary"></div>
                    <span class="ms-2 text-muted small">Loading...</span>
                </div>

                <!-- Empty State (Compact) -->
                <div id="pendingEmpty" class="empty-state compact" style="display: none;">
                    <i class="bi bi-check-circle"></i>
                    <p class="mb-0 small">No pending requests</p>
                </div>

                <!-- Pending Contacts List -->
                <div id="pendingList"></div>

                <!-- Error State -->
                <div id="pendingError" class="alert alert-danger py-2 mb-0" style="display: none;" role="alert">
                    <i class="bi bi-exclamation-triangle"></i>
                    <span class="small" id="errorMessage">Failed to load pending contacts</span>
                </div>
            </div>

            <!-- Existing Contacts Section -->
            <div class="mb-4">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <h5 class="mb-0">
                        <i class="bi bi-person-lines-fill"></i> Existing Contacts
                        <span class="badge counter-badge counter-ok rounded-pill" id="contactsCounter" style="display: none;">0 / 350</span>
                    </h5>
                    <button class="btn btn-sm btn-outline-primary" id="refreshExistingBtn">
                        <i class="bi bi-arrow-clockwise"></i> Refresh
                    </button>
                </div>

                <!-- Search and Filter Toolbar -->
                <div class="search-toolbar">
                    <input type="text" class="form-control" id="searchInput" placeholder="Search by name or public key...">
                    <select class="form-select" id="typeFilter" style="max-width: 150px;">
                        <option value="ALL">All Types</option>
                        <option value="COM">COM</option>
                        <option value="REP">REP</option>
                        <option value="ROOM">ROOM</option>
                        <option value="SENS">SENS</option>
                    </select>
                </div>

                <!-- Loading State -->
                <div id="existingLoading" class="text-center py-3" style="display: none;">
                    <div class="spinner-border spinner-border-sm text-primary"></div>
                    <span class="ms-2 text-muted">Loading contacts...</span>
                </div>

                <!-- Empty State -->
                <div id="existingEmpty" class="empty-state" style="display: none;">
                    <i class="bi bi-inbox"></i>
                    <p class="mb-0">No contacts found</p>
                    <small class="text-muted">Contacts will appear here once added</small>
                </div>

                <!-- Existing Contacts List -->
                <div id="existingList"></div>

                <!-- Error State -->
                <div id="existingError" class="alert alert-danger" style="display: none;" role="alert">
                    <i class="bi bi-exclamation-triangle"></i>
                    <span id="existingErrorMessage">Failed to load contacts</span>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteContactModal" tabindex="-1" aria-labelledby="deleteContactModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title" id="deleteContactModalLabel">
                    <i class="bi bi-exclamation-triangle"></i> Confirm Delete
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p class="mb-2">Are you sure you want to delete this contact?</p>
                <div class="alert alert-warning mb-0">
                    <strong id="deleteContactName"></strong><br>
                    <small class="font-monospace" id="deleteContactKey"></small>
                </div>
                <p class="text-muted small mt-2 mb-0">This action cannot be undone.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-danger" id="confirmDeleteBtn">
                    <i class="bi bi-trash"></i> Delete Contact
                </button>
            </div>
        </div>
    </div>
</div>

<!-- Toast container for notifications (position classes applied by JS from ui_settings) -->
<div class="toast-container position-fixed bottom-0 end-0 p-3" data-toast-container>
    <div id="contactToast" class="toast" role="alert">
        <div class="toast-header">
            <strong class="me-auto">Contact Management</strong>
            <button type="button" class="btn-close" data-bs-dismiss="toast"></button>
        </div>
        <div class="toast-body"></div>
    </div>
</div>
{% endblock %}

{% block extra_scripts %}
<script src="{{ url_for('static', filename='js/contacts.js') }}"></script>
{% endblock %}
