{% extends "admin/base_site.html" %}

{% block content %}
  <style>
    .error-detail-container {
      max-width: 1200px;
      margin: 20px auto;
      padding: 20px;
    }

    .error-header {
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      color: white;
      padding: 30px;
      border-radius: 8px 8px 0 0;
      margin-bottom: 0;
    }

    .error-header h1 {
      margin: 0 0 15px 0;
      font-size: 28px;
      font-weight: 600;
      color: white;
    }

    .error-stats {
      background-color: #ffffff;
      border: 1px solid #e0e0e0;
      border-top: none;
      border-radius: 0 0 8px 8px;
      padding: 20px 30px;
      margin-bottom: 30px;
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
      gap: 20px;
    }

    .stat-item {
      display: flex;
      flex-direction: column;
    }

    .stat-label {
      font-size: 12px;
      text-transform: uppercase;
      color: #666;
      font-weight: 600;
      margin-bottom: 5px;
      letter-spacing: 0.5px;
    }

    .stat-value {
      font-size: 16px;
      color: #333;
      font-weight: 500;
    }

    .error-section {
      background-color: #ffffff;
      border: 1px solid #e0e0e0;
      border-radius: 8px;
      padding: 25px;
      margin-bottom: 25px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }

    .error-section h2 {
      margin: 0 0 15px 0;
      font-size: 20px;
      color: #333;
      border-bottom: 2px solid #667eea;
      padding-bottom: 10px;
    }

    .error-section h3 {
      margin: 20px 0 10px 0;
      font-size: 16px;
      color: #555;
      font-weight: 600;
    }

    .error-message {
      background-color: #fff3cd;
      border-left: 4px solid #ffc107;
      padding: 15px;
      margin: 15px 0;
      border-radius: 4px;
      color: #856404;
      font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
      font-size: 14px;
      word-wrap: break-word;
    }

    pre {
      background-color: #1e1e1e;
      color: #d4d4d4;
      border: 1px solid #333;
      padding: 20px;
      overflow-x: auto;
      white-space: pre-wrap;
      word-wrap: break-word;
      border-radius: 6px;
      font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
      font-size: 13px;
      line-height: 1.6;
      box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
    }

    .back-button {
      display: inline-block;
      padding: 12px 24px;
      background-color: #b5bfee;
      color: white;
      text-decoration: none;
      border-radius: 6px;
      font-weight: 500;
      transition: background-color 0.3s ease;
      margin-top: 20px;
    }

    .back-button:hover {
      background-color: #5568d3;
      color: white;
      text-decoration: none;
    }

    .error-not-found {
      background-color: #f8d7da;
      border: 1px solid #f5c6cb;
      border-radius: 8px;
      padding: 40px;
      text-align: center;
      margin: 40px 0;
    }

    .error-not-found h1 {
      color: #721c24;
      margin-bottom: 15px;
    }

    .error-not-found p {
      color: #721c24;
      font-size: 16px;
    }

    .badge {
      display: inline-block;
      padding: 4px 12px;
      background-color: #dc3545;
      color: white;
      border-radius: 12px;
      font-size: 14px;
      font-weight: 600;
      margin-left: 10px;
    }
  </style>

  <div class="error-detail-container">
    {% if error_data %}
      <div class="error-header">
        <h1>
          {{ error_data.error_type }}
          <span class="badge">{{ error_data.count }} occurrence{{ error_data.count|pluralize }}</span>
        </h1>
      </div>

      <div class="error-stats">
        <div class="stat-item">
          <span class="stat-label">First Seen</span>
          <span class="stat-value">{{ error_data.first_seen }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">Last Seen</span>
          <span class="stat-value">{{ error_data.last_seen }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">Total Count</span>
          <span class="stat-value">{{ error_data.count }}</span>
        </div>
      </div>

      <div class="error-section">
        <h2>Last Recorded Error</h2>
        
        <h3>URL</h3>
        <div style="background-color: #f8f9fa; padding: 12px; border-radius: 6px; margin-bottom: 20px;">
          <code style="font-size: 14px; color: #667eea; word-wrap: break-word;">{{ error_data.url }}</code>
        </div>
        
        <h3>Error Message</h3>
        <div class="error-message">
          {{ error_data.latest_instance.error_message }}
        </div>

        <h3>Full Stack Trace</h3>
        <pre>{{ error_data.latest_instance.traceback }}</pre>
      </div>

    {% else %}
      <div class="error-not-found">
        <h1>Error Not Found</h1>
        <p>Could not retrieve details for this error.</p>
      </div>
    {% endif %}

    <a href="{% url 'admin:pensieve_client_monitoredproject_changelist' %}" class="back-button">
      ← Back to Dashboard
    </a>
  </div>
{% endblock %}