{% extends 'admin/base.html' %}
{% block title %}{{ t('tools.title') }}{% endblock %}

{% block content %}
<div class="page-header page-header--with-action">
    <div class="page-header__content">
        <h2>🔧 {{ t('tools.title') }}</h2>
    </div>
    {# height-keeper: the sticky bar is button-high on every page
       (posts-list standard); this page has no header action #}
    <span class="btn header-spacer" aria-hidden="true">&nbsp;</span>
</div>
<div class="page-help">{{ t('tools.subtitle') }}</div>

<div class="tools-grid">
    <!-- Sitemap -->
    <div class="tool-card">
        <h3>🗺️ {{ t('tools.sitemap_title') }}</h3>
        <p>{{ t('tools.sitemap_desc') }}</p>
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.sitemap_status') }}</span>
            <span class="status-badge status-active">✅ {{ t('tools.sitemap_active') }}</span>
        </div>
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.total_urls') }}</span>
            <span class="stat-value">{{ sitemap_count }}</span>
        </div>
        {# Rows are a ledger of the real sitemap sections and sum exactly
           to Total URLs (counted while the XML is built). #}
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.published_posts') }}</span>
            <span class="stat-value">{{ sitemap_stats.posts }}</span>
        </div>
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.published_pages') }}</span>
            <span class="stat-value">{{ sitemap_stats.pages }}</span>
        </div>
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.homepage') }}</span>
            <span class="stat-value">{{ sitemap_stats.homepage }}</span>
        </div>
        {% if sitemap_stats.listings %}
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.sitemap_listings') }}</span>
            <span class="stat-value">{{ sitemap_stats.listings }}</span>
        </div>
        {% endif %}
        <div class="url-box">
            <span class="url-display" id="sitemapUrl">/sitemap.xml</span>
            <a href="/sitemap.xml" target="_blank" class="btn btn-secondary">🔗 {{ t('tools.view') }}</a>
        </div>
        <p class="text-sm text-muted mt-3">💡 {{ t('tools.sitemap_auto') }}</p>
    </div>

    <!-- Robots.txt -->
    <div class="tool-card">
        <h3>🤖 {{ t('tools.robots_title') }}</h3>
        <p>{{ t('tools.robots_desc') }}</p>
        <form method="POST">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <div class="form-group">
                <label for="robots_txt">{{ t('tools.robots_content') }}</label>
                <textarea id="robots_txt" name="robots_txt" rows="10">{{ robots_content }}</textarea>
                <div class="help-text">{{ t('tools.robots_help') }}</div>
            </div>
            <div class="flex gap-3 items-center">
                <button type="submit" class="btn btn-primary">💾 {{ t('tools.save') }}</button>
                <a href="/robots.txt" target="_blank" class="btn btn-secondary">🔗 {{ t('tools.view') }}</a>
                <button type="button" class="btn btn-secondary" onclick="resetRobots()">↩️ {{ t('tools.reset_default') }}</button>
            </div>
        </form>
    </div>

    <!-- RSS Feed -->
    <div class="tool-card">
        <h3>📡 {{ t('tools.rss_title') }}</h3>
        <p>{{ t('tools.rss_desc') }}</p>
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.sitemap_status') }}</span>
            <span class="status-badge status-active">✅ {{ t('tools.sitemap_active') }}</span>
        </div>
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.rss_posts_per_feed') }}</span>
            <span class="stat-value">20</span>
        </div>
        <div class="mt-4">
            <div class="url-box mb-2">
                <span class="url-display">/feed</span>
                <a href="/feed" target="_blank" class="btn btn-secondary">🔗 {{ t('tools.view') }}</a>
            </div>
            {% for lang in active_languages %}
            <div class="url-box mb-1">
                <span class="url-display">/feed/{{ lang.code }}</span>
                <a href="/feed/{{ lang.code }}" target="_blank" class="btn btn-secondary btn-sm">🔗 {{ lang.name }}</a>
            </div>
            {% endfor %}
        </div>
        <p class="text-sm text-muted mt-3">💡 {{ t('tools.rss_auto') }}</p>
    </div>

    <!-- llms.txt -->
    <div class="tool-card">
        <h3>🤖 {{ t('tools.llms_title') }}</h3>
        <p>{{ t('tools.llms_desc') }}</p>
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.sitemap_status') }}</span>
            <span class="status-badge status-active">✅ {{ t('tools.sitemap_active') }}</span>
        </div>
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.llms_posts_included') }}</span>
            <span class="stat-value">{{ llms_post_count }}</span>
        </div>
        <div class="stat-row">
            <span class="stat-label">{{ t('tools.llms_pages_included') }}</span>
            <span class="stat-value">{{ llms_page_count }}</span>
        </div>
        <div class="url-box">
            <span class="url-display">/llms.txt</span>
            <a href="/llms.txt" target="_blank" class="btn btn-secondary">🔗 {{ t('tools.view') }}</a>
        </div>
        <p class="text-sm text-muted mt-3">💡 {{ t('tools.llms_auto') }}</p>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function resetRobots() {
    AdminDialog.confirm('{{ t("tools.reset_confirm") }}').then(function(ok) {
        if (!ok) return;
        document.getElementById('robots_txt').value = 'User-agent: *\nDisallow: /admin/\nDisallow: /api/\nAllow: /api/public/\nDisallow: /setup/\nDisallow: /unsubscribe\nDisallow: /confirm-email\nDisallow: /invite/\nAllow: /\n\nUser-agent: GPTBot\nAllow: /\n\nUser-agent: Google-Extended\nAllow: /\n\nUser-agent: ClaudeBot\nAllow: /\n\nUser-agent: Amazonbot\nAllow: /';
    });
}
</script>
{% endblock %}
