file_path = 'core/templates/core/settings.html'

with open(file_path, 'r') as f:
    content = f.read()

# 1. Add Nav Tab
if 'id="devices-tab"' not in content:
    whatsapp_tab_end = 'id="whatsapp-tab" data-bs-toggle="pill" data-bs-target="#whatsapp" type="button" role="tab">\n                <i class="bi bi-whatsapp me-2"></i>{% trans "WhatsApp Gateway" %}\n            </button>\n        li>'
    
    insert_str = """
        <li class="nav-item" role="presentation">
            <button class="nav-link fw-bold px-4" id="devices-tab" data-bs-toggle="pill" data-bs-target="#devices" type="button" role="tab">
                <i class="bi bi-hdd-network me-2"></i>{% trans "Devices" %}
            </button>
        </li>"
    
    if whatsapp_tab_end in content:
        content = content.replace(whatsapp_tab_end, whatsapp_tab_end + insert_str)
        print("Added Devices Tab Nav.")
    else:
        # Fallback search if exact string match fails due to whitespace
        print("Could not find exact match for Nav Tab insertion. Trying simpler match.")
        simple_search = '{% trans "WhatsApp Gateway" %}'
        parts = content.split(simple_search)
        if len(parts) > 1:
            # Reconstruct slightly differently but risky
            pass

# 2. Add Tab Content
if 'id="devices" role="tabpanel"' not in content:
    devices_pane = """
        <!-- Devices Tab -->
        <div class="tab-pane fade" id="devices" role="tabpanel">
            <div class="card shadow-sm border-0 glassmorphism mb-4">
                <div class="card-header bg-transparent border-0 py-3 d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0 fw-bold">{% trans "Connected Devices" %}</h5>
                    <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addDeviceModal">
                        <i class="bi bi-plus-lg me-1"></i> {% trans "Add Device" %}
                    </button>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th class="ps-4">{% trans "Device Name" %}</th>
                                    <th>{% trans "Type" %}</th>
                                    <th>{% trans "Connection" %}</th>
                                    <th>{% trans "IP / Port" %}</th>
                                    <th>{% trans "Status" %}</th>
                                    <th class="text-end pe-4">{% trans "Actions" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for device in devices %}
                                <tr>
                                    <td class="ps-4 fw-bold">{{ device.name }}</td>
                                    <td>
                                        <span class="badge bg-secondary-soft text-secondary">{{ device.get_device_type_display }}</span>
                                    </td>
                                    <td>{{ device.get_connection_type_display }}</td>
                                    <td>
                                        {% if device.ip_address %}
                                            {{ device.ip_address }}{% if device.port %}:{{ device.port }}{% endif %}
                                        {% else %}
                                            <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if device.is_active %}
                                            <span class="badge bg-success-soft text-success">{% trans "Active" %}</span>
                                        {% else %}
                                            <span class="badge bg-danger-soft text-danger">{% trans "Inactive" %}</span>
                                        {% endif %}
                                    </td>
                                    <td class="text-end pe-4">
                                        <button class="btn btn-sm btn-light text-primary" data-bs-toggle="modal" data-bs-target="#editDeviceModal{{ device.id }}">
                                            <i class="bi bi-pencil"></i>
                                        </button>
                                        <button class="btn btn-sm btn-light text-danger" data-bs-toggle="modal" data-bs-target="#deleteDeviceModal{{ device.id }}">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </td>
                                </tr>
                                
                                <!-- Edit Device Modal -->
                                <div class="modal fade" id="editDeviceModal{{ device.id }}" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content border-0">
                                            <form action="{% url 'edit_device' device.id %}" method="post">
                                                {% csrf_token %}
                                                <div class="modal-header">
                                                    <h5 class="modal-title fw-bold">{% trans "Edit Device" %}</h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <div class="row g-3">
                                                        <div class="col-12">
                                                            <label class="form-label fw-semibold">{% trans "Device Name" %}</label>
                                                            <input type="text" name="name" class="form-control" value="{{ device.name }}" required>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <label class="form-label fw-semibold">{% trans "Device Type" %}</label>
                                                            <select name="device_type" class="form-select">
                                                                <option value="printer" {% if device.device_type == 'printer' %}selected{% endif %}>{% trans "Printer" %}</option>
                                                                <option value="scanner" {% if device.device_type == 'scanner' %}selected{% endif %}>{% trans "Scanner" %}</option>
                                                                <option value="scale" {% if device.device_type == 'scale' %}selected{% endif %}>{% trans "Weight Scale" %}</option>
                                                                <option value="display" {% if device.device_type == 'display' %}selected{% endif %}>{% trans "Customer Display" %}</option>
                                                                <option value="other" {% if device.device_type == 'other' %}selected{% endif %}>{% trans "Other" %}</option>
                                                            </select>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <label class="form-label fw-semibold">{% trans "Connection Type" %}</label>
                                                            <select name="connection_type" class="form-select">
                                                                <option value="network" {% if device.connection_type == 'network' %}selected{% endif %}>{% trans "Network (IP)" %}</option>
                                                                <option value="usb" {% if device.connection_type == 'usb' %}selected{% endif %}>{% trans "USB" %}</option>
                                                                <option value="bluetooth" {% if device.connection_type == 'bluetooth' %}selected{% endif %}>{% trans "Bluetooth" %}</option>
                                                            </select>
                                                        </div>
                                                        <div class="col-md-8">
                                                            <label class="form-label fw-semibold">{% trans "IP Address" %}</label>
                                                            <input type="text" name="ip_address" class="form-control" value="{{ device.ip_address|default:'' }}" placeholder="192.168.1.100">
                                                        </div>
                                                        <div class="col-md-4">
                                                            <label class="form-label fw-semibold">{% trans "Port" %}</label>
                                                            <input type="number" name="port" class="form-control" value="{{ device.port|default:'' }}" placeholder="9100">
                                                        </div>
                                                        <div class="col-12">
                                                            <div class="form-check form-switch">
                                                                <input class="form-check-input" type="checkbox" name="is_active" {% if device.is_active %}checked{% endif %}>
                                                                <label class="form-check-label">{% trans "Active" %}</label>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="modal-footer bg-light border-0">
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                                                    <button type="submit" class="btn btn-primary">{% trans "Save Changes" %}</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                                
                                <!-- Delete Device Modal -->
                                <div class="modal fade" id="deleteDeviceModal{{ device.id }}" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content border-0">
                                            <div class="modal-header">
                                                <h5 class="modal-title fw-bold">{% trans "Delete Device" %}</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                            </div>
                                            <div class="modal-body">
                                                <p>{% trans "Are you sure you want to delete" %} <strong>{{ device.name }}</strong>?</p>
                                            </div>
                                            <div class="modal-footer bg-light border-0">
                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                                                <a href="{% url 'delete_device' device.id %}" class="btn btn-danger">{% trans "Delete" %}</a>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                {% empty %}
                                <tr>
                                    <td colspan="6" class="text-center py-5">
                                        <div class="text-muted">
                                            <i class="bi bi-hdd-network fs-1 d-block mb-3"></i>
                                            {% trans "No devices configured." %}
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    "
    
    parts = content.split('<!-- Add Tier Modal -->')
    if len(parts) > 1:
        last_div = parts[0].rfind('</div>')
        second_last_div = parts[0].rfind('</div>', 0, last_div)
        
        if second_last_div != -1:
            new_part0 = parts[0][:second_last_div] + devices_pane + parts[0][second_last_div:]
            content = new_part0 + '<!-- Add Tier Modal -->' + parts[1]
            print("Added Devices Tab Pane.")
        else:
            print("Could not find insertion point for Devices Pane.")

# 3. Add Add Device Modal
if 'id="addDeviceModal"' not in content:
    modal_content = """
<!-- Add Device Modal -->
<div class="modal fade" id="addDeviceModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content border-0 shadow">
            <form action="{% url 'add_device' %}" method="post">
                {% csrf_token %}
                <div class="modal-header">
                    <h5 class="modal-title fw-bold">{% trans "Add New Device" %}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-12">
                            <label class="form-label fw-semibold">{% trans "Device Name" %}</label>
                            <input type="text" name="name" class="form-control" required placeholder="e.g. Kitchen Printer">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">{% trans "Device Type" %}</label>
                            <select name="device_type" class="form-select">
                                <option value="printer">{% trans "Printer" %}</option>
                                <option value="scanner">{% trans "Scanner" %}</option>
                                <option value="scale">{% trans "Weight Scale" %}</option>
                                <option value="display">{% trans "Customer Display" %}</option>
                                <option value="other">{% trans "Other" %}</option>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">{% trans "Connection Type" %}</label>
                            <select name="connection_type" class="form-select">
                                <option value="network" selected>{% trans "Network (IP)" %}</option>
                                <option value="usb">{% trans "USB" %}</option>
                                <option value="bluetooth">{% trans "Bluetooth" %}</option>
                            </select>
                        </div>
                        <div class="col-md-8">
                            <label class="form-label fw-semibold">{% trans "IP Address" %}</label>
                            <input type="text" name="ip_address" class="form-control" placeholder="192.168.1.100">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label fw-semibold">{% trans "Port" %}</label>
                            <input type="number" name="port" class="form-control" placeholder="9100">
                        </div>
                        <div class="col-12">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="is_active" checked>
                                <label class="form-check-label">{% trans "Active" %}</label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer bg-light border-0">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Close" %}</button>
                    <button type="submit" class="btn btn-primary">{% trans "Add Device" %}</button>
                </div>
            </form>
        </div>
    </div>
</div>
"
    content = content.replace('{% endblock %}', modal_content + '\n{% endblock %}')
    print("Added Add Device Modal.")

with open(file_path, 'w') as f:
    f.write(content)