{% extends "base.html" %}

{% block title %}Recommended News - DS Task AI News{% endblock %}

{% block content %}
<div class="space-y-8">
    <div class="bg-white rounded-lg shadow-md p-6 mb-8">
        <h2 class="text-2xl font-bold text-gray-800 mb-4">AI Insights</h2>
        <div class="prose max-w-none">
            {% if insights %}
                {% if insights is string %}
                    {# If insights is a string (JSON or markdown), try to parse it #}
                    {% set insights_data = insights | from_json %}
                    {% if insights_data %}
                        <div class="space-y-6">
                            {% if insights_data.themes %}
                            <div>
                                <h3 class="text-xl font-semibold text-gray-800 mb-2">Themes</h3>
                                <ul class="list-disc list-inside space-y-1">
                                    {% for theme in insights_data.themes %}
                                    <li class="text-gray-700">{{ theme }}</li>
                                    {% endfor %}
                                </ul>
                            </div>
                            {% endif %}
                            
                            {% if insights_data.insights %}
                            <div>
                                <h3 class="text-xl font-semibold text-gray-800 mb-2">Key Insights</h3>
                                <ul class="list-disc list-inside space-y-1">
                                    {% for insight in insights_data.insights %}
                                    <li class="text-gray-700">{{ insight }}</li>
                                    {% endfor %}
                                </ul>
                            </div>
                            {% endif %}
                            
                            {% if insights_data.implications %}
                            <div>
                                <h3 class="text-xl font-semibold text-gray-800 mb-2">Implications</h3>
                                <ul class="list-disc list-inside space-y-1">
                                    {% for implication in insights_data.implications %}
                                    <li class="text-gray-700">{{ implication }}</li>
                                    {% endfor %}
                                </ul>
                            </div>
                            {% endif %}
                            
                            {% if insights_data.related_areas %}
                            <div>
                                <h3 class="text-xl font-semibold text-gray-800 mb-2">Related Areas</h3>
                                <div class="flex flex-wrap gap-2">
                                    {% for area in insights_data.related_areas %}
                                    <span class="px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm">
                                        {{ area }}
                                    </span>
                                    {% endfor %}
                                </div>
                            </div>
                            {% endif %}
                        </div>
                    {% else %}
                        {# If parsing failed, display the raw insights #}
                        <div class="whitespace-pre-wrap">{{ insights }}</div>
                    {% endif %}
                {% else %}
                    {# If insights is already a dict/object #}
                    <div class="space-y-6">
                        {% if insights.themes %}
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-2">Themes</h3>
                            <ul class="list-disc list-inside space-y-1">
                                {% for theme in insights.themes %}
                                <li class="text-gray-700">{{ theme }}</li>
                                {% endfor %}
                            </ul>
                        </div>
                        {% endif %}
                        
                        {% if insights.insights %}
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-2">Key Insights</h3>
                            <ul class="list-disc list-inside space-y-1">
                                {% for insight in insights.insights %}
                                <li class="text-gray-700">{{ insight }}</li>
                                {% endfor %}
                            </ul>
                        </div>
                        {% endif %}
                        
                        {% if insights.implications %}
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-2">Implications</h3>
                            <ul class="list-disc list-inside space-y-1">
                                {% for implication in insights.implications %}
                                <li class="text-gray-700">{{ implication }}</li>
                                {% endfor %}
                            </ul>
                        </div>
                        {% endif %}
                        
                        {% if insights.related_areas %}
                        <div>
                            <h3 class="text-xl font-semibold text-gray-800 mb-2">Related Areas</h3>
                            <div class="flex flex-wrap gap-2">
                                {% for area in insights.related_areas %}
                                <span class="px-3 py-1 bg-blue-100 text-blue-800 rounded-full text-sm">
                                    {{ area }}
                                </span>
                                {% endfor %}
                            </div>
                        </div>
                        {% endif %}
                    </div>
                {% endif %}
            {% else %}
                <p class="text-gray-600">No insights available for these articles.</p>
            {% endif %}
        </div>
    </div>

    <h2 class="text-3xl font-bold text-gray-800 mb-6">Recommended Articles</h2>
    
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        {% for article in articles %}
        <article class="article-card bg-white rounded-lg shadow-md overflow-hidden">
            <div class="p-6">
                <h3 class="text-xl font-semibold text-gray-800 mb-2">
                    <a href="{{ article.link }}" target="_blank" class="hover:text-blue-600">
                        {{ article.title }}
                    </a>
                </h3>
                <p class="text-gray-600 mb-4">{{ article.content[:200] }}...</p>
                <div class="flex justify-between items-center text-sm text-gray-500">
                    <span>{{ article.source }}</span>
                    <span>{{ article.published }}</span>
                </div>
                {% if article.categories %}
                <div class="mt-4 flex flex-wrap gap-2">
                    {% for category in article.categories %}
                    <span class="px-2 py-1 bg-blue-100 text-blue-800 rounded-full text-xs">
                        {{ category }}
                    </span>
                    {% endfor %}
                </div>
                {% endif %}
                <div class="mt-4">
                    <a href="{{ article.link }}" target="_blank" class="inline-block bg-blue-600 text-white px-4 py-2 rounded-md font-medium hover:bg-blue-700 transition-colors duration-300">
                        Read More
                    </a>
                </div>
            </div>
        </article>
        {% endfor %}
    </div>
</div>
{% endblock %} 