{% extends 'base.html' %}
{% load static %}
{% load humanize %}

{% block title %}System Backup - HAVEN GRAZURI ADVANCE{% endblock %}

{% block content %}
<div class="min-h-screen bg-gray-50 py-8">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <!-- Header -->
        <div class="mb-8">
            <div class="flex items-center justify-between">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 flex items-center">
                        <span>💾</span>
                        System Backup
                    </h1>
                    <p class="text-gray-600 mt-2">Create and manage system backups</p>
                </div>
                <a href="{% url 'utils:settings' %}" class="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
                    <span>←</span> Back to Settings
                </a>
            </div>
        </div>

        {% if messages %}
            {% for message in messages %}
            <div class="mb-4 p-4 rounded-md {% if message.tags == 'error' %}bg-red-50 text-red-800 border border-red-200{% else %}bg-green-50 text-green-800 border border-green-200{% endif %}">
                <i class="fas {% if message.tags == 'error' %}fa-exclamation-circle{% else %}fa-check-circle{% endif %} mr-2"></i>
                {{ message }}
            </div>
            {% endfor %}
        {% endif %}

        <!-- Create Backup Section -->
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 mb-8">
            <div class="px-6 py-5 border-b border-gray-200">
                <h2 class="text-xl font-semibold text-gray-900 flex items-center">
                    <i class="fas fa-plus-circle text-primary mr-2"></i>
                    Create New Backup
                </h2>
            </div>
            <div class="px-6 py-6">
                <div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6">
                    <div class="flex items-start">
                        <span>ℹ️</span>
                        <div>
                            <h3 class="font-semibold text-blue-900 mb-2">What's Included in the Backup:</h3>
                            <ul class="list-disc list-inside text-sm text-blue-800 space-y-1">
                                <li>Complete database backup (all tables and data)</li>
                                <li>All media files (documents, images, uploads)</li>
                                <li>Static files (if configured)</li>
                                <li>System configuration information</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <form method="post" action="{% url 'utils:backup_system' %}">
                    {% csrf_token %}
                    <button type="submit" style="display: inline-flex; align-items: center; padding: 12px 24px; border: none; border-radius: 6px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); font-size: 16px; font-weight: 600; color: white; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); cursor: pointer; transition: all 0.2s;">
                        <span>⬇️</span>
                        Create Backup Now
                    </button>
                    <p class="text-sm text-gray-500 mt-3">
                        <span>⏰</span>
                        This may take a few minutes depending on the size of your data.
                    </p>
                </form>
            </div>
        </div>

        <!-- Backup History -->
        {% if backup_history %}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200">
            <div class="px-6 py-5 border-b border-gray-200">
                <h2 class="text-xl font-semibold text-gray-900 flex items-center">
                    <i class="fas fa-history text-primary mr-2"></i>
                    Backup History
                </h2>
            </div>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Filename</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Size</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        {% for backup in backup_history %}
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="flex items-center">
                                    <i class="fas fa-file-archive text-purple-500 mr-2"></i>
                                    <span class="text-sm font-medium text-gray-900">{{ backup.filename }}</span>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                {{ backup.size_mb }} MB
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                {{ backup.created|date:"M d, Y H:i" }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                <a href="{% url 'utils:download_backup' backup.filename %}" class="text-primary-600 hover:text-primary-900">
                                    <span>⬇️</span> Download
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        {% else %}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-8 text-center">
            <span>📥</span>
            <p class="text-gray-500">No backup history found. Create your first backup above.</p>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}

