{% extends "base.html" %}

{% block title %}Resource Historie{% endblock %}

{% block extra_css %}
<style>
    /* Resource Info Card */
    .resource-info-card {
        border: none;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    
    /* Resource Value Display */
    .resource-value {
        font-size: 1.5rem;
        font-weight: 600;
        color: #212529;
        background-color: #f8f9fa;
        padding: 0.5rem 1rem;
        border-radius: 0.5rem;
        display: inline-block;
        font-family: 'Courier New', monospace;
    }
    
    /* Status Badge Large */
    .status-badge-large {
        padding: 0.5rem 1rem;
        font-size: 1rem;
        border-radius: 0.5rem;
        font-weight: 500;
    }
    
    /* Timeline Styling */
    .timeline {
        position: relative;
        padding: 20px 0;
    }
    
    .timeline::before {
        content: '';
        position: absolute;
        left: 30px;
        top: 0;
        bottom: 0;
        width: 3px;
        background: linear-gradient(to bottom, #e9ecef 0%, #dee2e6 100%);
    }
    
    .timeline-item {
        position: relative;
        padding-left: 80px;
        margin-bottom: 40px;
    }
    
    .timeline-marker {
        position: absolute;
        left: 20px;
        top: 0;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        border: 3px solid #fff;
        box-shadow: 0 2px 4px rgba(0,0,0,0.2);
        z-index: 1;
    }
    
    .timeline-content {
        background: #fff;
        border-radius: 10px;
        padding: 20px;
        box-shadow: 0 3px 6px rgba(0,0,0,0.1);
        position: relative;
        transition: transform 0.2s ease;
    }
    
    .timeline-content:hover {
        transform: translateY(-2px);
        box-shadow: 0 5px 10px rgba(0,0,0,0.15);
    }
    
    .timeline-content::before {
        content: '';
        position: absolute;
        left: -10px;
        top: 15px;
        width: 0;
        height: 0;
        border-style: solid;
        border-width: 10px 10px 10px 0;
        border-color: transparent #fff transparent transparent;
    }
    
    /* Action Icons */
    .action-icon {
        width: 35px;
        height: 35px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        font-size: 1rem;
        margin-right: 10px;
    }
    
    .action-created { background-color: #d4edda; color: #155724; }
    .action-allocated { background-color: #cce5ff; color: #004085; }
    .action-deallocated { background-color: #d1ecf1; color: #0c5460; }
    .action-quarantined { background-color: #fff3cd; color: #856404; }
    .action-released { background-color: #d4edda; color: #155724; }
    .action-deleted { background-color: #f8d7da; color: #721c24; }
    
    /* Details Box */
    .details-box {
        background-color: #f8f9fa;
        border: 1px solid #e9ecef;
        border-radius: 8px;
        padding: 15px;
        margin-top: 15px;
        font-family: 'Courier New', monospace;
        font-size: 0.875rem;
    }
    
    /* Info Grid */
    .info-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 20px;
    }
    
    .info-item {
        padding: 15px;
        background-color: #f8f9fa;
        border-radius: 8px;
        border-left: 4px solid #007bff;
    }
    
    .info-label {
        font-size: 0.875rem;
        color: #6c757d;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 5px;
    }
    
    .info-value {
        font-size: 1.1rem;
        font-weight: 500;
        color: #212529;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="mb-0">Resource Historie</h1>
            <p class="text-muted mb-0">Detaillierte Aktivitätshistorie</p>
        </div>
        <a href="{{ url_for('resources.resources') }}" class="btn btn-secondary">
            <i class="fas fa-arrow-left"></i> Zurück zur Übersicht
        </a>
    </div>

    <!-- Resource Info Card -->
    <div class="card resource-info-card mb-4">
        <div class="card-header bg-white">
            <h5 class="mb-0">📋 Resource Details</h5>
        </div>
        <div class="card-body">
            <!-- Main Resource Info -->
            <div class="text-center mb-4">
                <div class="mb-3">
                    {% if resource.resource_type == 'domain' %}
                        <span class="display-1">🌐</span>
                    {% elif resource.resource_type == 'ipv4' %}
                        <span class="display-1">🖥️</span>
                    {% else %}
                        <span class="display-1">📱</span>
                    {% endif %}
                </div>
                <div class="resource-value mb-3">{{ resource.resource_value }}</div>
                <div>
                    {% if resource.status == 'available' %}
                        <span class="status-badge-large badge bg-success">
                            ✅ Verfügbar
                        </span>
                    {% elif resource.status == 'allocated' %}
                        <span class="status-badge-large badge bg-primary">
                            🔗 Zugeteilt
                        </span>
                    {% else %}
                        <span class="status-badge-large badge bg-warning text-dark">
                            ⚠️ Quarantäne
                        </span>
                    {% endif %}
                </div>
            </div>

            <!-- Detailed Info Grid -->
            <div class="info-grid">
                <div class="info-item">
                    <div class="info-label">Ressourcentyp</div>
                    <div class="info-value">{{ resource.resource_type|upper }}</div>
                </div>
                
                <div class="info-item">
                    <div class="info-label">Erstellt am</div>
                    <div class="info-value">
                        {{ resource.created_at.strftime('%d.%m.%Y %H:%M') if resource.created_at else '-' }}
                    </div>
                </div>
                
                <div class="info-item">
                    <div class="info-label">Status geändert</div>
                    <div class="info-value">
                        {{ resource.status_changed_at.strftime('%d.%m.%Y %H:%M') if resource.status_changed_at else '-' }}
                    </div>
                </div>
                
                {% if resource.allocated_to_license %}
                <div class="info-item">
                    <div class="info-label">Zugewiesen an Lizenz</div>
                    <div class="info-value">
                        <a href="{{ url_for('licenses.edit_license', license_id=resource.allocated_to_license) }}" 
                           class="text-decoration-none">
                            {{ license_info.license_key if license_info else 'ID: ' + resource.allocated_to_license|string }}
                        </a>
                    </div>
                </div>
                {% endif %}
                
                {% if resource.quarantine_reason %}
                <div class="info-item">
                    <div class="info-label">Quarantäne-Grund</div>
                    <div class="info-value">
                        <span class="badge bg-warning text-dark">{{ resource.quarantine_reason }}</span>
                    </div>
                </div>
                {% endif %}
                
                {% if resource.quarantine_until %}
                <div class="info-item">
                    <div class="info-label">Quarantäne bis</div>
                    <div class="info-value">
                        {{ resource.quarantine_until.strftime('%d.%m.%Y') }}
                    </div>
                </div>
                {% endif %}
            </div>

            {% if resource.notes %}
            <div class="mt-4">
                <div class="alert alert-info mb-0">
                    <h6 class="alert-heading">📝 Notizen</h6>
                    <p class="mb-0">{{ resource.notes }}</p>
                </div>
            </div>
            {% endif %}
        </div>
    </div>

    <!-- History Timeline -->
    <div class="card">
        <div class="card-header bg-white">
            <h5 class="mb-0">⏱️ Aktivitäts-Historie</h5>
        </div>
        <div class="card-body">
            {% if history %}
            <div class="timeline">
                {% for event in history %}
                <div class="timeline-item">
                    <div class="timeline-marker 
                        {% if event.action == 'created' %}bg-success
                        {% elif event.action == 'allocated' %}bg-primary
                        {% elif event.action == 'deallocated' %}bg-info
                        {% elif event.action == 'quarantined' %}bg-warning
                        {% elif event.action == 'released' %}bg-success
                        {% elif event.action == 'deleted' %}bg-danger
                        {% else %}bg-secondary{% endif %}">
                    </div>
                    <div class="timeline-content">
                        <div class="d-flex justify-content-between align-items-start">
                            <div class="flex-grow-1">
                                <div class="d-flex align-items-center mb-2">
                                    {% if event.action == 'created' %}
                                        <span class="action-icon action-created">
                                            <i class="fas fa-plus"></i>
                                        </span>
                                        <h6 class="mb-0">Ressource erstellt</h6>
                                    {% elif event.action == 'allocated' %}
                                        <span class="action-icon action-allocated">
                                            <i class="fas fa-link"></i>
                                        </span>
                                        <h6 class="mb-0">An Lizenz zugeteilt</h6>
                                    {% elif event.action == 'deallocated' %}
                                        <span class="action-icon action-deallocated">
                                            <i class="fas fa-unlink"></i>
                                        </span>
                                        <h6 class="mb-0">Von Lizenz freigegeben</h6>
                                    {% elif event.action == 'quarantined' %}
                                        <span class="action-icon action-quarantined">
                                            <i class="fas fa-ban"></i>
                                        </span>
                                        <h6 class="mb-0">In Quarantäne gesetzt</h6>
                                    {% elif event.action == 'released' %}
                                        <span class="action-icon action-released">
                                            <i class="fas fa-check"></i>
                                        </span>
                                        <h6 class="mb-0">Aus Quarantäne entlassen</h6>
                                    {% elif event.action == 'deleted' %}
                                        <span class="action-icon action-deleted">
                                            <i class="fas fa-trash"></i>
                                        </span>
                                        <h6 class="mb-0">Ressource gelöscht</h6>
                                    {% else %}
                                        <h6 class="mb-0">{{ event.action }}</h6>
                                    {% endif %}
                                </div>
                                
                                <div class="text-muted small">
                                    <i class="fas fa-user"></i> {{ event.action_by }}
                                    {% if event.ip_address %}
                                        &nbsp;&bull;&nbsp; <i class="fas fa-globe"></i> {{ event.ip_address }}
                                    {% endif %}
                                    {% if event.license_id %}
                                        &nbsp;&bull;&nbsp; 
                                        <i class="fas fa-key"></i> 
                                        <a href="{{ url_for('licenses.edit_license', license_id=event.license_id) }}">
                                            Lizenz #{{ event.license_id }}
                                        </a>
                                    {% endif %}
                                </div>
                                
                                {% if event.details %}
                                <div class="details-box">
                                    <strong>Details:</strong>
                                    <pre class="mb-0" style="white-space: pre-wrap;">{{ event.details|tojson(indent=2) }}</pre>
                                </div>
                                {% endif %}
                            </div>
                            <div class="text-end ms-3">
                                <div class="badge bg-light text-dark">
                                    <i class="far fa-calendar"></i> {{ event.action_at.strftime('%d.%m.%Y') }}
                                </div>
                                <div class="small text-muted mt-1">
                                    <i class="far fa-clock"></i> {{ event.action_at.strftime('%H:%M:%S') }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
            {% else %}
            <div class="text-center py-5">
                <i class="fas fa-history text-muted" style="font-size: 3rem; opacity: 0.5;"></i>
                <p class="text-muted mt-3">Keine Historie-Einträge vorhanden.</p>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}