{% extends 'base.html' %}
{% block title %}System Health{% endblock %}

{% block content %}
<div class="max-w-6xl mx-auto my-8 p-6 bg-white rounded-lg shadow-md">
    <div class="flex justify-between items-center mb-6">
        <h2 class="text-2xl font-bold text-navy-800">System Health Status</h2>
        <div class="text-sm text-navy-600">{{ version_info.version }} - {{ version_info.release_name }}</div>
    </div>
    
    <!-- Health Summary Cards -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
        <div class="bg-{{ database_status.color }}-50 border border-{{ database_status.color }}-200 rounded-lg p-4">
            <div class="flex items-center mb-2">
                <div class="h-4 w-4 rounded-full bg-{{ database_status.color }}-500 mr-2"></div>
                <h3 class="font-semibold text-{{ database_status.color }}-700">Database</h3>
            </div>
            <p class="text-sm text-{{ database_status.color }}-600">{{ database_status.message }}</p>
        </div>
        
        <div class="bg-{{ storage_status.color }}-50 border border-{{ storage_status.color }}-200 rounded-lg p-4">
            <div class="flex items-center mb-2">
                <div class="h-4 w-4 rounded-full bg-{{ storage_status.color }}-500 mr-2"></div>
                <h3 class="font-semibold text-{{ storage_status.color }}-700">Storage</h3>
            </div>
            <p class="text-sm text-{{ storage_status.color }}-600">{{ storage_status.message }}</p>
        </div>
        
        <div class="bg-{{ api_status.color }}-50 border border-{{ api_status.color }}-200 rounded-lg p-4">
            <div class="flex items-center mb-2">
                <div class="h-4 w-4 rounded-full bg-{{ api_status.color }}-500 mr-2"></div>
                <h3 class="font-semibold text-{{ api_status.color }}-700">API Services</h3>
            </div>
            <p class="text-sm text-{{ api_status.color }}-600">{{ api_status.message }}</p>
        </div>
        
        <div class="bg-{{ memory_status.color }}-50 border border-{{ memory_status.color }}-200 rounded-lg p-4">
            <div class="flex items-center mb-2">
                <div class="h-4 w-4 rounded-full bg-{{ memory_status.color }}-500 mr-2"></div>
                <h3 class="font-semibold text-{{ memory_status.color }}-700">Memory</h3>
            </div>
            <p class="text-sm text-{{ memory_status.color }}-600">{{ memory_status.message }}</p>
        </div>
    </div>
    
    <!-- Database Details -->
    <div class="mb-8">
        <h3 class="text-xl font-semibold mb-4 text-navy-700">Database Information</h3>
        <div class="overflow-x-auto border rounded-lg">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Metric</th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Value</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            Total Songs
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ database_stats.song_count|default('0') }}
                        </td>
                    </tr>
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            Total Rounds
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ database_stats.round_count|default('0') }}
                        </td>
                    </tr>
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            Total Users
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ database_stats.user_count|default('0') }}
                        </td>
                    </tr>
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            Database File Size
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ database_stats.file_size|default('Unknown') }}
                        </td>
                    </tr>
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            Last Backup
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ database_stats.last_backup|default('Never') }}
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    
    <!-- Storage Details -->
    <div class="mb-8">
        <h3 class="text-xl font-semibold mb-4 text-navy-700">Storage Information</h3>
        <div class="overflow-x-auto border rounded-lg">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Directory</th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Files</th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Size</th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    {% for dir in storage_stats %}
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            {{ dir.name }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ dir.file_count }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ dir.size }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm">
                            {% if dir.writable %}
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                    Writable
                                </span>
                            {% else %}
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                                    Not Writable
                                </span>
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- Service Status -->
    <div class="mb-8">
        <h3 class="text-xl font-semibold mb-4 text-navy-700">External Services</h3>
        <div class="overflow-x-auto border rounded-lg">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Service</th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Details</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    {% for service in service_stats %}
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            {{ service.name }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm">
                            {% if service.status == 'ok' %}
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                    Available
                                </span>
                            {% elif service.status == 'warning' %}
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">
                                    Warning
                                </span>
                            {% else %}
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                                    Unavailable
                                </span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 text-sm text-gray-500">
                            {{ service.message }}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    
    <!-- Version Information -->
    <div class="mb-8">
        <h3 class="text-xl font-semibold mb-4 text-navy-700">Version Information</h3>
        <div class="bg-gray-50 rounded-lg p-4">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <p class="text-sm"><strong>Application Version:</strong> {{ version_info.version }}</p>
                    <p class="text-sm"><strong>Release Name:</strong> {{ version_info.release_name }}</p>
                    <p class="text-sm"><strong>Release Date:</strong> {{ version_info.release_date }}</p>
                </div>
                <div>
                    <p class="text-sm"><strong>Python Version:</strong> {{ system_info.python_version }}</p>
                    <p class="text-sm"><strong>Platform:</strong> {{ system_info.platform }}</p>
                    <p class="text-sm"><strong>Flask Version:</strong> {{ system_info.flask_version }}</p>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Actions -->
    <div class="flex justify-between">
        <a href="{{ url_for('users.backup_manager') }}" class="bg-teal-500 text-white px-6 py-2 rounded hover:bg-teal-600">
            <i class="fas fa-database mr-2"></i> Go to Backup Manager
        </a>
        
        <a href="{{ url_for('users.system_settings') }}" class="bg-navy-600 text-white px-6 py-2 rounded hover:bg-navy-700">
            <i class="fas fa-cog mr-2"></i> Back to System Settings
        </a>
    </div>
</div>
{% endblock %}