{% extends "base.html" %}
{% set active = "home" %}
{% block title %}Cortex — Home{% endblock %}

{% block content %}
<h2 class="page-title page-title-lg">Dashboard</h2>

<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-value">{{ stats.sqlite_total }}</div>
        <div class="stat-label">Documents</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ stats.graph_triples }}</div>
        <div class="stat-label">Triples</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ stats.entities }}</div>
        <div class="stat-label">Entities</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ alerts|length }}</div>
        <div class="stat-label">Alerts</div>
    </div>
</div>

{% if alerts %}
<div class="card">
    <div class="card-header">Active Alerts</div>
    {% for alert in alerts %}
    <div class="alert alert-{{ 'danger' if alert.severity == 'high' else 'warning' if alert.severity == 'medium' else 'info' }}">
        <strong>{{ alert.type|capitalize }}:</strong> {{ alert.message }}
    </div>
    {% endfor %}
</div>
{% endif %}

<div class="card-header">Projects</div>
{% if projects %}
<div class="project-grid">
    {% for proj in projects %}
    <a href="/project/{{ proj.name | urlencode }}" class="project-card">
        <div class="project-name">{{ proj.name }}</div>
        {% if proj.last_activity %}
        <div class="project-last-activity">Last activity: {{ proj.last_activity }}</div>
        {% endif %}
        <div class="project-stat">
            <span class="project-count">{{ proj.doc_count }}</span>
            <span class="project-count-label">{{ 'document' if proj.doc_count == 1 else 'documents' }}</span>
        </div>
    </a>
    {% endfor %}
</div>
{% else %}
<div class="card">
    <p class="table-empty">No projects yet. Capture knowledge with a project tag to see it here.</p>
</div>
{% endif %}

<div class="card">
    <div class="card-header">Recent Activity</div>
    {% if recent %}
    <table>
        <thead><tr><th>Type</th><th>Title</th><th>Project</th><th>Created</th></tr></thead>
        <tbody>
        {% for doc in recent %}
        <tr>
            <td><span class="badge badge-{{ doc.type }}">{{ doc.type }}</span></td>
            <td><a href="/documents/{{ doc.id }}">{{ doc.title }}</a></td>
            <td>{{ doc.project }}</td>
            <td>{{ doc.created_at[:10] if doc.created_at else '' }}</td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
    {% else %}
    <p class="table-empty">No activity yet.</p>
    {% endif %}
</div>
{% endblock %}
