{% extends "layout.html" %}

{% block content %}
<div class="company-detail-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-building"></i></span>
                    {{ company.name }}
                </h1>
                <p class="page-subtitle">Company Details - System Administrator View</p>
            </div>
            <div class="header-actions">
                <a href="{{ url_for('system_admin.system_admin_companies') }}" class="btn btn-secondary">
                    <i class="ti ti-arrow-left"></i>
                    Back to Companies
                </a>
                <a href="{{ url_for('system_admin.system_admin_dashboard') }}" class="btn btn-primary">
                    <i class="ti ti-dashboard"></i>
                    Dashboard
                </a>
            </div>
        </div>
    </div>

    <!-- Company Statistics -->
    <div class="stats-section">
        <div class="stat-card">
            <div class="stat-value">{{ users|length }}</div>
            <div class="stat-label">Total Users</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ teams|length }}</div>
            <div class="stat-label">Teams</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ projects|length }}</div>
            <div class="stat-label">Projects</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ recent_time_entries }}</div>
            <div class="stat-label">Recent Entries</div>
            <span class="stat-hint">Last 7 days</span>
        </div>
    </div>

    <!-- Main Content Grid -->
    <div class="content-grid">
        <!-- Left Column -->
        <div class="content-column">
            <!-- Company Information Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-info-circle"></i></span>
                        Company Information
                    </h2>
                </div>
                <div class="card-body">
                    <div class="info-list">
                        <div class="info-row">
                            <span class="info-label">Company Name</span>
                            <span class="info-value">{{ company.name }}</span>
                        </div>
                        <div class="info-row">
                            <span class="info-label">Slug</span>
                            <span class="info-value code">{{ company.slug }}</span>
                        </div>
                        <div class="info-row">
                            <span class="info-label">Type</span>
                            <span class="info-value">
                                {% if company.is_personal %}
                                    <span class="badge badge-freelancer">Personal/Freelancer</span>
                                {% else %}
                                    <span class="badge badge-company">Business Company</span>
                                {% endif %}
                            </span>
                        </div>
                        <div class="info-row">
                            <span class="info-label">Status</span>
                            <span class="info-value">
                                {% if company.is_active %}
                                    <span class="status-badge status-active">Active</span>
                                {% else %}
                                    <span class="status-badge status-inactive">Inactive</span>
                                {% endif %}
                            </span>
                        </div>
                        <div class="info-row">
                            <span class="info-label">Created</span>
                            <span class="info-value">{{ company.created_at.strftime('%Y-%m-%d %H:%M') }}</span>
                        </div>
                        <div class="info-row">
                            <span class="info-label">Max Users</span>
                            <span class="info-value">{{ company.max_users or 'Unlimited' }}</span>
                        </div>
                        {% if company.description %}
                        <div class="info-row full-width">
                            <span class="info-label">Description</span>
                            <span class="info-value">{{ company.description }}</span>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- Role Distribution Card -->
            {% if role_counts %}
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-shield"></i></span>
                        Role Distribution
                    </h2>
                </div>
                <div class="card-body">
                    <div class="role-grid">
                        {% for role, count in role_counts.items() %}
                        <div class="role-item">
                            <div class="role-count">{{ count }}</div>
                            <div class="role-name">{{ role }}</div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- Management Actions Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-settings"></i></span>
                        Management Actions
                    </h2>
                </div>
                <div class="card-body">
                    <div class="action-grid">
                        <a href="{{ url_for('users.system_admin_users', company=company.id) }}" class="action-item">
                            <div class="action-icon"><i class="ti ti-users"></i></div>
                            <div class="action-content">
                                <h3>Manage Users</h3>
                                <p>View and edit all users</p>
                            </div>
                        </a>
                        <a href="{{ url_for('system_admin.system_admin_time_entries', company=company.id) }}" class="action-item">
                            <div class="action-icon"><i class="ti ti-clock"></i></div>
                            <div class="action-content">
                                <h3>Time Entries</h3>
                                <p>Browse tracking data</p>
                            </div>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- Right Column -->
        <div class="content-column">
            <!-- Users List Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-users"></i></span>
                        Users
                        <span class="card-count">{{ users|length }}</span>
                    </h2>
                </div>
                <div class="card-body">
                    {% if users %}
                        <div class="item-list">
                            {% for user in users[:10] %}
                            <div class="list-item">
                                <div class="item-content">
                                    <div class="item-title">{{ user.username }}</div>
                                    <div class="item-subtitle">{{ user.email or 'No email' }}</div>
                                </div>
                                <div class="item-badges">
                                    <span class="role-badge role-{{ user.role.name.lower() if user.role else 'team_member' }}">
                                        {{ user.role.value if user.role else 'Team Member' }}
                                    </span>
                                    {% if user.is_blocked %}
                                        <span class="status-badge status-blocked">Blocked</span>
                                    {% elif not user.is_verified %}
                                        <span class="status-badge status-unverified">Unverified</span>
                                    {% endif %}
                                </div>
                            </div>
                            {% endfor %}
                            {% if users|length > 10 %}
                            <div class="list-footer">
                                <a href="{{ url_for('users.system_admin_users', company=company.id) }}" class="btn btn-sm btn-ghost">
                                    View All {{ users|length }} Users <i class="ti ti-arrow-right"></i>
                                </a>
                            </div>
                            {% endif %}
                        </div>
                    {% else %}
                        <p class="empty-message">No users in this company.</p>
                    {% endif %}
                </div>
            </div>

            <!-- Teams List Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-users-group"></i></span>
                        Teams
                        <span class="card-count">{{ teams|length }}</span>
                    </h2>
                </div>
                <div class="card-body">
                    {% if teams %}
                        <div class="item-list">
                            {% for team in teams %}
                            <div class="list-item">
                                <div class="item-content">
                                    <div class="item-title">{{ team.name }}</div>
                                    {% if team.description %}
                                        <div class="item-subtitle">{{ team.description }}</div>
                                    {% endif %}
                                </div>
                                <div class="item-meta">
                                    <span class="date-text">{{ team.created_at.strftime('%Y-%m-%d') }}</span>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        <p class="empty-message">No teams in this company.</p>
                    {% endif %}
                </div>
            </div>

            <!-- Projects List Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <span class="icon"><i class="ti ti-folder"></i></span>
                        Projects
                        <span class="card-count">{{ projects|length }}</span>
                    </h2>
                </div>
                <div class="card-body">
                    {% if projects %}
                        <div class="item-list">
                            {% for project in projects[:10] %}
                            <div class="list-item">
                                <div class="item-content">
                                    <div class="item-title">{{ project.name }}</div>
                                    <div class="item-subtitle">
                                        <span class="code">{{ project.code }}</span>
                                        {% if project.description %}
                                            • {{ project.description[:50] }}{% if project.description|length > 50 %}...{% endif %}
                                        {% endif %}
                                    </div>
                                </div>
                                <div class="item-badges">
                                    {% if project.is_active %}
                                        <span class="status-badge status-active">Active</span>
                                    {% else %}
                                        <span class="status-badge status-inactive">Inactive</span>
                                    {% endif %}
                                </div>
                            </div>
                            {% endfor %}
                            {% if projects|length > 10 %}
                            <div class="list-footer">
                                <p class="text-muted">And {{ projects|length - 10 }} more projects...</p>
                            </div>
                            {% endif %}
                        </div>
                    {% else %}
                        <p class="empty-message">No projects in this company.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    
    <!-- Danger Zone -->
    <div class="danger-zone">
        <div class="danger-header">
            <h2 class="danger-title">
                <i class="ti ti-alert-triangle"></i>
                Danger Zone
            </h2>
        </div>
        <div class="danger-content">
            <div class="danger-warning">
                <h3>Delete Company</h3>
                <p>Once you delete a company, there is no going back. This will permanently delete:</p>
                <ul>
                    <li>All users in the company</li>
                    <li>All projects, tasks, and time entries</li>
                    <li>All teams and settings</li>
                </ul>
            </div>
            <form method="POST" action="{{ url_for('system_admin.delete_company', company_id=company.id) }}" 
                  onsubmit="return confirm('Are you absolutely sure you want to delete {{ company.name }}? This action cannot be undone!');">
                <button type="submit" class="btn btn-danger">
                    <i class="ti ti-trash"></i>
                    Delete This Company
                </button>
            </form>
        </div>
    </div>
</div>

<style>
/* Container */
.company-detail-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
}

/* Page Header */
.page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 16px;
    padding: 2rem;
    margin-bottom: 2rem;
    color: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 2rem;
}

.page-title {
    font-size: 2rem;
    font-weight: 700;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.page-icon {
    font-size: 2.5rem;
    display: inline-block;
    animation: float 3s ease-in-out infinite;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

.page-subtitle {
    font-size: 1.1rem;
    opacity: 0.9;
    margin: 0.5rem 0 0 0;
}

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

/* Stats Section */
.stats-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: white;
    padding: 1.5rem;
    border-radius: 12px;
    text-align: center;
    border: 1px solid #e5e7eb;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.3s ease;
    position: relative;
}

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

.stat-value {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    color: #667eea;
}

.stat-label {
    font-size: 0.9rem;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

.stat-hint {
    font-size: 0.75rem;
    color: #9ca3af;
    margin-top: 0.25rem;
    display: block;
}

/* Content Grid */
.content-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}

@media (max-width: 1024px) {
    .content-grid {
        grid-template-columns: 1fr;
    }
}

/* 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-count {
    font-size: 0.875rem;
    color: #6b7280;
    font-weight: 400;
    margin-left: auto;
    background: #e5e7eb;
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
}

.card-body {
    padding: 1.5rem;
}

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

.info-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    border-bottom: 1px solid #f3f4f6;
}

.info-row:last-child {
    border-bottom: none;
}

.info-row.full-width {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
}

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

.info-value {
    color: #1f2937;
    font-weight: 500;
}

.info-value.code {
    font-family: monospace;
    background: #f3f4f6;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.875rem;
}

/* Role Grid */
.role-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 1rem;
}

.role-item {
    text-align: center;
    padding: 1rem;
    background: #f8f9fa;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
}

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

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

/* Action Grid */
.action-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
}

.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;
}

/* Item List */
.item-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    max-height: 400px;
    overflow-y: auto;
}

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

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

.item-content {
    flex: 1;
    min-width: 0;
}

.item-title {
    font-weight: 600;
    color: #1f2937;
    margin-bottom: 0.25rem;
}

.item-subtitle {
    font-size: 0.875rem;
    color: #6b7280;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.item-subtitle .code {
    font-family: monospace;
    background: white;
    padding: 0.125rem 0.375rem;
    border-radius: 4px;
    font-size: 0.75rem;
}

.item-badges {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.item-meta {
    font-size: 0.875rem;
    color: #6b7280;
}

.list-footer {
    padding: 1rem;
    text-align: center;
    border-top: 1px solid #e5e7eb;
    margin-top: 0.5rem;
}

/* Badges */
.badge,
.status-badge,
.role-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;
}

.status-active {
    background: #d1fae5;
    color: #065f46;
}

.status-inactive {
    background: #fee2e2;
    color: #991b1b;
}

.status-blocked {
    background: #fee2e2;
    color: #991b1b;
}

.status-unverified {
    background: #fef3c7;
    color: #92400e;
}

.role-team_member {
    background: #e5e7eb;
    color: #374151;
}

.role-team_leader {
    background: #d1fae5;
    color: #065f46;
}

.role-supervisor {
    background: #dbeafe;
    color: #1e40af;
}

.role-admin {
    background: #fef3c7;
    color: #92400e;
}

.role-system_admin {
    background: #ede9fe;
    color: #5b21b6;
}

/* Danger Zone */
.danger-zone {
    margin-top: 3rem;
    background: #fef2f2;
    border: 2px solid #fecaca;
    border-radius: 12px;
    overflow: hidden;
}

.danger-header {
    background: #fee2e2;
    padding: 1.5rem;
    border-bottom: 1px solid #fecaca;
}

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

.danger-title i {
    font-size: 1.5rem;
}

.danger-content {
    padding: 2rem;
}

.danger-warning h3 {
    font-size: 1.1rem;
    color: #991b1b;
    margin: 0 0 0.5rem 0;
}

.danger-warning p {
    color: #7f1d1d;
    margin-bottom: 1rem;
}

.danger-warning ul {
    margin: 0 0 2rem 2rem;
    color: #7f1d1d;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.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-danger {
    background: #dc2626;
    color: white;
}

.btn-danger:hover {
    background: #b91c1c;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(220, 38, 38, 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 {
    font-size: 0.875rem;
    padding: 0.5rem 1rem;
}

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

.text-muted {
    color: #6b7280;
}

.date-text {
    color: #6b7280;
    font-size: 0.875rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    .company-detail-container {
        padding: 1rem;
    }
    
    .page-header {
        padding: 1.5rem;
    }
    
    .header-content {
        flex-direction: column;
        text-align: center;
    }
    
    .header-actions {
        width: 100%;
        flex-direction: column;
    }
    
    .action-grid {
        grid-template-columns: 1fr;
    }
}

/* 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 %}