{% extends 'admin/base.html.twig' %}

{% block title %}Ajanslar{% endblock %}
{% block page_title %}Haber Ajanslari{% endblock %}

{% block body %}
    {# Header info #}
    <div class="mb-6">
        <p class="text-sm text-gray-500">
            Haber ajanslarinizi yapilandirin. Ucretli ajanslar (AA, DHA, IHA) veya ucretsiz RSS kaynaklarindan otomatik haber cekin.
        </p>
    </div>

    {# Agency Cards Grid #}
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        {% for config in configs %}
            {% set slug = config.agencySlug %}
            {% set stat = stats[slug]|default({pending: 0, imported: 0, skipped: 0, total: 0}) %}
            <div class="card overflow-hidden">
                {# Agency Header #}
                <div class="px-6 py-4 border-b border-gray-100 flex items-center justify-between">
                    <div class="flex items-center gap-3">
                        <div class="w-12 h-12 rounded-xl flex items-center justify-center
                            {% if slug == 'rss' %}
                                bg-orange-50 text-orange-600
                            {% elseif slug == 'aa' %}
                                bg-blue-50 text-blue-600
                            {% elseif slug == 'dha' %}
                                bg-red-50 text-red-600
                            {% elseif slug == 'iha' %}
                                bg-green-50 text-green-600
                            {% else %}
                                bg-gray-50 text-gray-600
                            {% endif %}
                        ">
                            <i class="fas fa-{{ slug == 'rss' ? 'rss-square' : 'rss' }} text-xl"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-gray-900">{{ config.agencyName }}</h3>
                            <div class="flex items-center gap-2">
                                <p class="text-xs text-gray-500">{{ slug|upper }}</p>
                                {% if slug == 'rss' %}
                                    <span class="inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-bold bg-green-100 text-green-700">
                                        <i class="fas fa-gift mr-0.5"></i> UCRETSIZ
                                    </span>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    <div>
                        {% if config.isActive %}
                            <span class="badge-success">Aktif</span>
                        {% else %}
                            <span class="badge-gray">Pasif</span>
                        {% endif %}
                    </div>
                </div>

                {# Stats #}
                <div class="px-6 py-4">
                    <div class="grid grid-cols-3 gap-3 mb-4">
                        <div class="text-center p-2 rounded-lg bg-yellow-50">
                            <p class="text-lg font-bold text-yellow-600">{{ stat.pending }}</p>
                            <p class="text-xs text-gray-500">Bekleyen</p>
                        </div>
                        <div class="text-center p-2 rounded-lg bg-green-50">
                            <p class="text-lg font-bold text-green-600">{{ stat.imported }}</p>
                            <p class="text-xs text-gray-500">Import</p>
                        </div>
                        <div class="text-center p-2 rounded-lg bg-gray-50">
                            <p class="text-lg font-bold text-gray-600">{{ stat.total }}</p>
                            <p class="text-xs text-gray-500">Toplam</p>
                        </div>
                    </div>

                    {# Config Details #}
                    <div class="space-y-2 text-sm">
                        {% if slug == 'rss' %}
                            {# RSS - Feed sayisi goster #}
                            {% set rssFeeds = config.config.feeds|default([]) %}
                            {% set rssCustom = config.config.custom_feeds|default([]) %}
                            {% set activeFeeds = 0 %}
                            {% for f in rssFeeds %}{% if f.enabled is defined and f.enabled %}{% set activeFeeds = activeFeeds + 1 %}{% endif %}{% endfor %}
                            {% for f in rssCustom %}{% if f.enabled is defined and f.enabled %}{% set activeFeeds = activeFeeds + 1 %}{% endif %}{% endfor %}
                            <div class="flex items-center justify-between">
                                <span class="text-gray-500">Aktif Feed:</span>
                                <span class="text-orange-600 text-xs font-medium">{{ activeFeeds }} kaynak</span>
                            </div>
                            <div class="flex items-center justify-between">
                                <span class="text-gray-500">Tur:</span>
                                <span class="text-green-600 text-xs"><i class="fas fa-gift mr-1"></i>Ucretsiz RSS</span>
                            </div>
                        {% else %}
                            {# Ucretli ajanslar - API durumu goster #}
                            <div class="flex items-center justify-between">
                                <span class="text-gray-500">API URL:</span>
                                {% if config.apiUrl %}
                                    <span class="text-green-600 text-xs"><i class="fas fa-check-circle mr-1"></i>Tanimli</span>
                                {% else %}
                                    <span class="text-gray-400 text-xs"><i class="fas fa-minus-circle mr-1"></i>Tanimsiz</span>
                                {% endif %}
                            </div>
                            <div class="flex items-center justify-between">
                                <span class="text-gray-500">API Anahtari:</span>
                                {% if config.apiKey %}
                                    <span class="text-green-600 text-xs"><i class="fas fa-check-circle mr-1"></i>Tanimli</span>
                                {% else %}
                                    <span class="text-gray-400 text-xs"><i class="fas fa-minus-circle mr-1"></i>Tanimsiz</span>
                                {% endif %}
                            </div>
                        {% endif %}
                        <div class="flex items-center justify-between">
                            <span class="text-gray-500">Otomatik Yayinla:</span>
                            {% if config.isAutoPublish %}
                                <span class="text-blue-600 text-xs"><i class="fas fa-check mr-1"></i>Evet</span>
                            {% else %}
                                <span class="text-gray-400 text-xs">Hayir</span>
                            {% endif %}
                        </div>
                        <div class="flex items-center justify-between">
                            <span class="text-gray-500">Cekme Araligi:</span>
                            <span class="text-gray-700 text-xs">{{ config.fetchInterval }} dk</span>
                        </div>
                        {% if config.lastFetchedAt %}
                            <div class="flex items-center justify-between">
                                <span class="text-gray-500">Son Cekme:</span>
                                <span class="text-gray-700 text-xs">{{ config.lastFetchedAt|date('d.m.Y H:i') }}</span>
                            </div>
                        {% endif %}
                    </div>
                </div>

                {# Actions #}
                <div class="px-6 py-3 bg-gray-50 border-t border-gray-100 flex items-center justify-between gap-2">
                    <div class="flex items-center gap-2">
                        <a href="{{ path('admin_agency_edit', {id: config.id}) }}"
                           class="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-lg bg-white border border-gray-200 text-gray-700 hover:bg-gray-50 transition-colors">
                            <i class="fas fa-cog"></i> Ayarlar
                        </a>
                        {% if config.isActive %}
                            <a href="{{ path('admin_agency_items', {slug: config.agencySlug}) }}"
                               class="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-lg bg-white border border-gray-200 text-gray-700 hover:bg-gray-50 transition-colors">
                                <i class="fas fa-list"></i> Haberler
                                {% if stat.pending > 0 %}
                                    <span class="inline-flex items-center justify-center w-5 h-5 rounded-full bg-yellow-100 text-yellow-700 text-xs font-bold">{{ stat.pending }}</span>
                                {% endif %}
                            </a>
                        {% endif %}
                    </div>
                    <div class="flex items-center gap-2">
                        {% if config.isActive %}
                            <form method="post" action="{{ path('admin_agency_fetch', {id: config.id}) }}">
                                <input type="hidden" name="_token" value="{{ csrf_token('fetch_agency_' ~ config.id) }}">
                                <button type="submit"
                                        class="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-lg bg-primary-600 text-white hover:bg-primary-700 transition-colors">
                                    <i class="fas fa-download"></i> Cek
                                </button>
                            </form>
                        {% endif %}
                        <form method="post" action="{{ path('admin_agency_toggle', {id: config.id}) }}">
                            <input type="hidden" name="_token" value="{{ csrf_token('toggle_agency_' ~ config.id) }}">
                            <button type="submit"
                                    class="inline-flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-lg transition-colors
                                    {% if config.isActive %}
                                        bg-red-50 text-red-600 hover:bg-red-100
                                    {% else %}
                                        bg-green-50 text-green-600 hover:bg-green-100
                                    {% endif %}
                                    ">
                                {% if config.isActive %}
                                    <i class="fas fa-pause"></i> Durdur
                                {% else %}
                                    <i class="fas fa-play"></i> Aktif Et
                                {% endif %}
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        {% else %}
            <div class="col-span-full card">
                <div class="card-body text-center py-16">
                    <i class="fas fa-rss text-6xl text-gray-300 mb-4"></i>
                    <h3 class="text-xl font-medium text-gray-600 mb-2">Ajans Bulunamadi</h3>
                    <p class="text-gray-500">Henuz hicbir haber ajansi yapilandirilmamis.</p>
                </div>
            </div>
        {% endfor %}
    </div>

    {# How it works info box #}
    <div class="mt-8 card">
        <div class="card-body">
            <h3 class="text-base font-bold text-gray-900 mb-4 flex items-center gap-2">
                <i class="fas fa-info-circle text-blue-500"></i> Nasil Calisir?
            </h3>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="text-center">
                    <div class="w-12 h-12 mx-auto mb-3 rounded-full bg-blue-50 flex items-center justify-center">
                        <i class="fas fa-cog text-blue-600"></i>
                    </div>
                    <h4 class="font-semibold text-gray-900 mb-1">1. Yapilandirma</h4>
                    <p class="text-sm text-gray-500">Ajans API bilgilerini girin ve ajansi aktif edin.</p>
                </div>
                <div class="text-center">
                    <div class="w-12 h-12 mx-auto mb-3 rounded-full bg-green-50 flex items-center justify-center">
                        <i class="fas fa-download text-green-600"></i>
                    </div>
                    <h4 class="font-semibold text-gray-900 mb-1">2. Haber Cekme</h4>
                    <p class="text-sm text-gray-500">Otomatik veya manuel olarak ajanstan haberler cekilir.</p>
                </div>
                <div class="text-center">
                    <div class="w-12 h-12 mx-auto mb-3 rounded-full bg-yellow-50 flex items-center justify-center">
                        <i class="fas fa-newspaper text-yellow-600"></i>
                    </div>
                    <h4 class="font-semibold text-gray-900 mb-1">3. Import / Atla</h4>
                    <p class="text-sm text-gray-500">Gelen haberleri inceleyin, import edin veya atlayin.</p>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
