
import os

file_path = 'core/templates/base.html'

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

search_text = """            {% if user.is_authenticated %}
            <div class="p-3">
                <button type="button" id="sidebarCollapse" class="btn btn-light shadow-sm">
                    <i class="bi bi-list fs-5"></i>
                </button>
            </div>
            {% endif %}"""

replace_text = """            {% if user.is_authenticated %}
            <div class="p-3 d-flex justify-content-between align-items-center">
                <button type="button" id="sidebarCollapse" class="btn btn-light shadow-sm">
                    <i class="bi bi-list fs-5"></i>
                </button>

                <div class="language-switcher">
                    <form action="{% url 'set_language' %}" method="post" class="d-flex align-items-center">
                        {% csrf_token %}
                        <input type="hidden" name="next" value="{{ request.get_full_path|default:'/' }}">
                        <i class="bi bi-globe2 me-2 text-muted"></i>
                        <select name="language" class="form-select form-select-sm shadow-sm border-0" style="width: auto; background-color: #f8f9fa;" onchange="this.form.submit()">
                            {% get_current_language as LANGUAGE_CODE %}
                            {% get_available_languages as LANGUAGES %}
                            {% get_language_info_list for LANGUAGES as languages %}
                            {% for language in languages %}
                                <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
                                    {{ language.name_local }}
                                </option>
                            {% endfor %}
                        </select>
                    </form>
                </div>
            </div>
            {% endif %}"""

if search_text in content:
    new_content = content.replace(search_text, replace_text)
    with open(file_path, 'w') as f:
        f.write(new_content)
    print("Successfully patched base.html")
else:
    print("Search text not found in base.html. Please check formatting.")
