{% extends "layout.html" %}

{% block content %}
<div class="system-dashboard-container">
    <!-- Header Section -->
    <div class="page-header">
        <div class="header-content">
            <div class="header-left">
                <h1 class="page-title">
                    <span class="page-icon"><i class="ti ti-world"></i></span>
                    System Dashboard
                </h1>
                <p class="page-subtitle">Global system overview and management tools</p>
            </div>
            <div class="header-actions">
                <a href="{{ url_for('system_admin.system_admin_settings') }}" class="btn btn-secondary">
                    <i class="ti ti-settings"></i>
                    System Settings
                </a>
                <a href="{{ url_for('announcements.index') }}" class="btn btn-primary">
                    <i class="ti ti-speakerphone"></i>
                    Announcements
                </a>
            </div>
        </div>
    </div>

    <!-- System Overview Statistics -->
    <div class="stats-section">
        <div class="stat-card">
            <div class="stat-value">{{ total_companies }}</div>
            <div class="stat-label">Total Companies</div>
            <a href="{{ url_for('system_admin.system_admin_companies') }}" class="stat-link">Manage <i class="ti ti-arrow-right"></i></a>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ total_users }}</div>
            <div class="stat-label">Total Users</div>
            <a href="{{ url_for('users.system_admin_users') }}" class="stat-link">View all <i class="ti ti-arrow-right"></i></a>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ total_teams }}</div>
            <div class="stat-label">Total Teams</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ total_projects }}</div>
            <div class="stat-label">Total Projects</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ total_time_entries|default(0) }}</div>
            <div class="stat-label">Time Entries</div>
            <a href="{{ url_for('system_admin.system_admin_time_entries') }}" class="stat-link">View <i class="ti ti-arrow-right"></i></a>
        </div>
    </div>

    <!-- Alert Section for System Health Issues -->
    {% if orphaned_users > 0 or orphaned_time_entries > 0 %}
    <div class="alert-banner">
        <div class="alert-icon">
            <i class="ti ti-alert-triangle"></i>
        </div>
        <div class="alert-content">
            <h3>System Health Issues Detected</h3>
            <div class="alert-details">
                {% if orphaned_users > 0 %}
                <div class="alert-item">
                    <strong>{{ orphaned_users }} Orphaned Users</strong> - Users without company assignment
                </div>
                {% endif %}
                {% if orphaned_time_entries > 0 %}
                <div class="alert-item">
                    <strong>{{ orphaned_time_entries }} Orphaned Time Entries</strong> - Time entries without user assignment
                </div>
                {% endif %}
            </div>
        </div>
        <div class="alert-actions">
            <a href="{{ url_for('system_admin.system_admin_health') }}" class="btn btn-warning">
                <i class="ti ti-heart-rate-monitor"></i>
                System Health
            </a>
        </div>
    </div>
    {% endif %}

    <!-- Main Content Grid -->
    <div class="content-grid">
        <!-- Left Column -->
        <div class="content-column">
            <!-- Administrator Overview Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-shield"></i></span>
                        Administrator Overview
                    </h2>
                </div>
                <div class="card-body">
                    <div class="info-grid">
                        <div class="info-item">
                            <div class="info-value">{{ system_admins }}</div>
                            <div class="info-label">System Administrators</div>
                        </div>
                        <div class="info-item">
                            <div class="info-value">{{ regular_admins }}</div>
                            <div class="info-label">Company Administrators</div>
                        </div>
                        <div class="info-item">
                            <div class="info-value">{{ blocked_users }}</div>
                            <div class="info-label">Blocked Users</div>
                            {% if blocked_users > 0 %}
                            <a href="{{ url_for('users.system_admin_users', filter='blocked') }}" class="info-link">Review</a>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>

            <!-- Recent Activity Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-trending-up"></i></span>
                        Recent Activity
                        <span class="card-subtitle">Last 7 days</span>
                    </h2>
                </div>
                <div class="card-body">
                    <div class="activity-list">
                        <div class="activity-item">
                            <div class="activity-icon new-users">
                                <i class="ti ti-user-plus"></i>
                            </div>
                            <div class="activity-content">
                                <div class="activity-value">{{ recent_users }}</div>
                                <div class="activity-label">New Users</div>
                            </div>
                        </div>
                        <div class="activity-item">
                            <div class="activity-icon new-companies">
                                <i class="ti ti-building-plus"></i>
                            </div>
                            <div class="activity-content">
                                <div class="activity-value">{{ recent_companies }}</div>
                                <div class="activity-label">New Companies</div>
                            </div>
                        </div>
                        <div class="activity-item">
                            <div class="activity-icon time-entries">
                                <i class="ti ti-clock"></i>
                            </div>
                            <div class="activity-content">
                                <div class="activity-value">{{ recent_time_entries }}</div>
                                <div class="activity-label">Time Entries</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Quick Actions Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-bolt"></i></span>
                        Quick Actions
                    </h2>
                </div>
                <div class="card-body">
                    <div class="action-grid">
                        <a href="{{ url_for('users.system_admin_users') }}" class="action-item">
                            <div class="action-icon"><i class="ti ti-users"></i></div>
                            <div class="action-content">
                                <h3>All Users</h3>
                                <p>System-wide user management</p>
                            </div>
                        </a>
                        
                        <a href="{{ url_for('system_admin.system_admin_companies') }}" class="action-item">
                            <div class="action-icon"><i class="ti ti-building"></i></div>
                            <div class="action-content">
                                <h3>Companies</h3>
                                <p>Manage all companies</p>
                            </div>
                        </a>
                        
                        <a href="{{ url_for('system_admin.branding') }}" class="action-item">
                            <div class="action-icon"><i class="ti ti-palette"></i></div>
                            <div class="action-content">
                                <h3>Branding</h3>
                                <p>System appearance</p>
                            </div>
                        </a>

                        <a href="{{ url_for('system_admin.system_admin_health') }}" class="action-item">
                            <div class="action-icon"><i class="ti ti-heart-rate-monitor"></i></div>
                            <div class="action-content">
                                <h3>System Health</h3>
                                <p>Monitor & diagnostics</p>
                            </div>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- Right Column -->
        <div class="content-column">
            <!-- Top Companies Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-building"></i></span>
                        Top Companies by Users
                    </h2>
                </div>
                <div class="card-body">
                    {% if top_companies %}
                        <div class="company-list">
                            {% for company in top_companies %}
                            <div class="company-item">
                                <div class="company-info">
                                    <h4>{{ company.name }}</h4>
                                    <div class="company-stats">
                                        <span class="user-count"><i class="ti ti-users"></i> {{ company.user_count }} users</span>
                                    </div>
                                </div>
                                <a href="{{ url_for('system_admin.system_admin_company_detail', company_id=company.id) }}" class="btn btn-sm btn-ghost">
                                    View <i class="ti ti-arrow-right"></i>
                                </a>
                            </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        <p class="empty-message">No companies found.</p>
                    {% endif %}
                </div>
            </div>

            <!-- Recent Companies Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-sparkles"></i></span>
                        Recent Companies
                    </h2>
                </div>
                <div class="card-body">
                    {% if recent_companies_list %}
                        <div class="company-list">
                            {% for company in recent_companies_list %}
                            <div class="company-item">
                                <div class="company-info">
                                    <h4>{{ company.name }}</h4>
                                    <div class="company-meta">
                                        <span class="company-date"><i class="ti ti-calendar"></i> {{ company.created_at.strftime('%Y-%m-%d') }}</span>
                                        {% if company.is_personal %}
                                            <span class="badge badge-freelancer">Freelancer</span>
                                        {% else %}
                                            <span class="badge badge-company">Company</span>
                                        {% endif %}
                                    </div>
                                </div>
                                <a href="{{ url_for('system_admin.system_admin_company_detail', company_id=company.id) }}" class="btn btn-sm btn-ghost">
                                    View <i class="ti ti-arrow-right"></i>
                                </a>
                            </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        <p class="empty-message">No recent companies found.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<style>
/* System dashboard specific styles */

.header-actions {
    display: flex;
    gap: 1rem;
}

/* Stats section styles already in common style.css */

.stat-link {
    position: absolute;
    bottom: 0.75rem;
    right: 1rem;
    font-size: 0.875rem;
    color: #667eea;
    text-decoration: none;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.stat-link:hover {
    opacity: 1;
    text-decoration: underline;
}

/* Alert Banner */
.alert-banner {
    background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
    border: 2px solid #ef4444;
    border-radius: 12px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

.alert-icon {
    font-size: 2.5rem;
    color: #dc2626;
    flex-shrink: 0;
}

.alert-content {
    flex: 1;
}

.alert-content h3 {
    margin: 0 0 0.5rem 0;
    color: #991b1b;
    font-size: 1.25rem;
}

.alert-details {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.alert-item {
    color: #7f1d1d;
    font-size: 0.95rem;
}

/* Content grid styles already in common style.css */

/* Cards */
.card {
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    border: 1px solid #e5e7eb;
    margin-bottom: 1.5rem;
    overflow: hidden;
    transition: all 0.3s ease;
}

.card:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
}

.card-header {
    background: #f8f9fa;
    padding: 1.5rem;
    border-bottom: 1px solid #e5e7eb;
}

.card-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: #1f2937;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-title .icon {
    font-size: 1.5rem;
}

.card-subtitle {
    font-size: 0.875rem;
    color: #6b7280;
    font-weight: 400;
    margin-left: auto;
}

.card-body {
    padding: 1.5rem;
}

/* Info Grid */
.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 1.5rem;
}

.info-item {
    text-align: center;
}

.info-value {
    font-size: 2rem;
    font-weight: 700;
    color: #667eea;
    margin-bottom: 0.25rem;
}

.info-label {
    font-size: 0.875rem;
    color: #6b7280;
    font-weight: 500;
}

.info-link {
    display: inline-block;
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: #667eea;
    text-decoration: none;
}

.info-link:hover {
    text-decoration: underline;
}

/* Activity List */
.activity-list {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.activity-item {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.activity-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    flex-shrink: 0;
}

.activity-icon.new-users {
    background: #dbeafe;
    color: #2563eb;
}

.activity-icon.new-companies {
    background: #e0e7ff;
    color: #4f46e5;
}

.activity-icon.time-entries {
    background: #f3e8ff;
    color: #9333ea;
}

.activity-content {
    flex: 1;
}

.activity-value {
    font-size: 1.5rem;
    font-weight: 700;
    color: #1f2937;
}

.activity-label {
    font-size: 0.875rem;
    color: #6b7280;
}

/* Action grid styles already in common style.css */

.action-item {
    display: flex;
    gap: 1rem;
    padding: 1.25rem;
    background: #f8f9fa;
    border-radius: 8px;
    text-decoration: none;
    transition: all 0.2s ease;
    border: 2px solid transparent;
}

.action-item:hover {
    background: white;
    border-color: #667eea;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
}

.action-icon {
    font-size: 2rem;
    flex-shrink: 0;
}

.action-icon i {
    font-size: 2rem;
    color: #667eea;
}

.action-content h3 {
    font-size: 1.05rem;
    font-weight: 600;
    color: #1f2937;
    margin: 0 0 0.25rem 0;
}

.action-content p {
    font-size: 0.875rem;
    color: #6b7280;
    margin: 0;
}

/* Company List */
.company-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.company-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background: #f8f9fa;
    border-radius: 8px;
    transition: all 0.2s ease;
}

.company-item:hover {
    background: #f3f4f6;
}

.company-info h4 {
    font-size: 1.05rem;
    font-weight: 600;
    color: #1f2937;
    margin: 0 0 0.5rem 0;
}

.company-stats,
.company-meta {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-size: 0.875rem;
    color: #6b7280;
}

.user-count i,
.company-date i {
    font-size: 1rem;
    margin-right: 0.25rem;
}

/* Badges */
.badge {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
}

.badge-company {
    background: #dbeafe;
    color: #1e40af;
}

.badge-freelancer {
    background: #d1fae5;
    color: #065f46;
}

/* System dashboard specific button styles */
.btn-secondary {
    background: white;
    color: #667eea;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.5);
}

.btn-warning {
    background: #f59e0b;
    color: white;
}

.btn-warning:hover {
    background: #d97706;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
}

.btn-ghost {
    background: transparent;
    color: #667eea;
    border: 2px solid #e5e7eb;
    padding: 0.5rem 1rem;
}

.btn-ghost:hover {
    background: #f3f4f6;
    border-color: #667eea;
}

/* btn-sm already in common style.css */

/* Empty State */
.empty-message {
    text-align: center;
    color: #6b7280;
    padding: 2rem;
    font-style: italic;
}

/* System dashboard specific responsive styles */
@media (max-width: 768px) {
    .system-dashboard-container {
        padding: 1rem;
    }
    
    .header-actions {
        width: 100%;
        flex-direction: column;
    }
    
    .alert-banner {
        flex-direction: column;
        text-align: center;
    }
}

/* Animations */
@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: slideIn 0.3s ease-out;
    animation-fill-mode: both;
}

.card:nth-child(1) { animation-delay: 0.1s; }
.card:nth-child(2) { animation-delay: 0.2s; }
.card:nth-child(3) { animation-delay: 0.3s; }
</style>
{% endblock %}