{% extends "layouts/base.html" %}
{% load i18n %}
{% load static %}
{% load star_rating %}
{% load social_icons %}

{% block title %}{{ platform_display }} - {% trans "Social Media Monitoring" %} - PX360{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-3">
        <ol class="breadcrumb">
            <li class="breadcrumb-item">
                <a href="{% url 'social:social_comment_list' %}">{% trans "Social Media" %}</a>
            </li>
            <li class="breadcrumb-item active" aria-current="page">{{ platform_display }}</li>
        </ol>
    </nav>

    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div class="d-flex align-items-center">
            <div class="me-3">
                {% social_icon platform %}
            </div>
            <div>
                <h2 class="mb-1">{{ platform_display }}</h2>
                <p class="text-muted mb-0">{% trans "Monitor and analyze" %} {{ platform_display }} {% trans "comments" %}</p>
            </div>
        </div>
        <div>
            <a href="{% url 'social:social_analytics' %}?platform={{ platform }}" class="btn btn-outline-primary me-2">
                <i class="bi bi-graph-up me-1"></i> {% trans "View Analytics" %}
            </a>
            <a href="{% url 'social:social_comment_list' %}" class="btn btn-outline-secondary">
                <i class="bi bi-grid me-1"></i> {% trans "All Platforms" %}
            </a>
        </div>
    </div>

    <!-- Platform Statistics -->
    <div class="row mb-4">
        <div class="col-md-2">
            <div class="card border-primary mb-3">
                <div class="card-body text-center">
                    <h6 class="text-muted mb-1">{% trans "Total" %}</h6>
                    <h3 class="mb-0">{{ stats.total }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-success mb-3">
                <div class="card-body text-center">
                    <h6 class="text-muted mb-1">{% trans "Positive" %}</h6>
                    <h3 class="mb-0 text-success">{{ stats.positive }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-secondary mb-3">
                <div class="card-body text-center">
                    <h6 class="text-muted mb-1">{% trans "Neutral" %}</h6>
                    <h3 class="mb-0 text-secondary">{{ stats.neutral }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-danger mb-3">
                <div class="card-body text-center">
                    <h6 class="text-muted mb-1">{% trans "Negative" %}</h6>
                    <h3 class="mb-0 text-danger">{{ stats.negative }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-info mb-3">
                <div class="card-body text-center">
                    <h6 class="text-muted mb-1">{% trans "Avg Sentiment" %}</h6>
                    <h3 class="mb-0 text-info">{{ stats.avg_sentiment|floatformat:2 }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-warning mb-3">
                <div class="card-body text-center">
                    <h6 class="text-muted mb-1">{% trans "Engagement" %}</h6>
                    <h3 class="mb-0 text-warning">{{ stats.total_likes|add:stats.total_replies }}</h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Time Filter & Search -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-md-2">
                    <label class="form-label">{% trans "Time Period" %}</label>
                    <select name="time_filter" class="form-select" onchange="this.form.submit()">
                        <option value="all" {% if time_filter == 'all' %}selected{% endif %}>{% trans "All Time" %}</option>
                        <option value="today" {% if time_filter == 'today' %}selected{% endif %}>{% trans "Today" %}</option>
                        <option value="week" {% if time_filter == 'week' %}selected{% endif %}>{% trans "This Week" %}</option>
                        <option value="month" {% if time_filter == 'month' %}selected{% endif %}>{% trans "This Month" %}</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">{% trans "Sentiment" %}</label>
                    <select name="sentiment" class="form-select">
                        <option value="">{% trans "All" %}</option>
                        <option value="positive" {% if filters.sentiment == 'positive' %}selected{% endif %}>{% trans "Positive" %}</option>
                        <option value="neutral" {% if filters.sentiment == 'neutral' %}selected{% endif %}>{% trans "Neutral" %}</option>
                        <option value="negative" {% if filters.sentiment == 'negative' %}selected{% endif %}>{% trans "Negative" %}</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">{% trans "Date From" %}</label>
                    <input type="date" name="date_from" class="form-control" value="{{ filters.date_from|default:'' }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">{% trans "Date To" %}</label>
                    <input type="date" name="date_to" class="form-control" value="{{ filters.date_to|default:'' }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">{% trans "Search" %}</label>
                    <input type="text" name="search" class="form-control" placeholder="{% trans 'Search comments...' %}" value="{{ filters.search|default:'' }}">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-search me-1"></i> {% trans "Filter" %}
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Comments Feed -->
    <div class="row">
        {% for comment in comments %}
        <div class="col-lg-6 mb-3">
            <div class="card h-100" style="border-left: 4px solid {{ platform_color }};">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <div>
                            <span class="badge" style="background-color: {{ platform_color }};">{{ comment.get_platform_display }}</span>
                            {% if comment.rating %}
                            <span class="badge bg-warning text-dark">{{ comment.rating|star_rating }} {{ comment.rating }}/5</span>
                            {% endif %}
                            {% if comment.ai_analysis %}
                                {% with sentiment=comment.ai_analysis.sentiment.classification.en %}
                                    {% if sentiment == 'positive' %}
                                    <span class="badge bg-success">Positive</span>
                                    {% elif sentiment == 'negative' %}
                                    <span class="badge bg-danger">Negative</span>
                                    {% else %}
                                    <span class="badge bg-secondary">Neutral</span>
                                    {% endif %}
                                {% endwith %}
                            {% else %}
                            <span class="badge bg-light text-dark">Not Analyzed</span>
                            {% endif %}
                        </div>
                        <small class="text-muted">
                            {% if comment.published_at %}
                                {{ comment.published_at|date:"M d, Y H:i" }}
                            {% else %}
                                {{ comment.scraped_at|date:"M d, Y H:i" }}
                            {% endif %}
                        </small>
                    </div>
                    
                    {% if comment.author %}
                    <p class="mb-1"><strong>@{{ comment.author }}</strong></p>
                    {% endif %}
                    
                    <p class="mb-3">{{ comment.comments|truncatewords:30 }}</p>
                    
                    {% if comment.ai_analysis and comment.ai_analysis.keywords.en %}
                    <div class="mb-2">
                        {% for keyword in comment.ai_analysis.keywords.en|slice:":5" %}
                        <span class="badge bg-light text-dark me-1">{{ keyword }}</span>
                        {% endfor %}
                    </div>
                    {% endif %}
                    
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <small class="text-muted me-3">
                                <i class="bi bi-heart text-danger"></i> {{ comment.like_count }}
                            </small>
                            <small class="text-muted">
                                <i class="bi bi-chat text-primary"></i> {{ comment.reply_count }}
                            </small>
                        </div>
                        <a href="{% url 'social:social_comment_detail' comment.pk %}" class="btn btn-sm btn-outline-primary">
                            {% trans "View Details" %}
                        </a>
                    </div>
                </div>
            </div>
        </div>
        {% empty %}
        <div class="col-12">
            <div class="text-center py-5">
                <i class="bi bi-chat-quote" style="font-size: 3rem; color: #ccc;"></i>
                <p class="text-muted mt-3">{% trans "No comments found for this platform" %}</p>
            </div>
        </div>
        {% endfor %}
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Comments pagination" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-left"></i>
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ num }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                        {{ num }}
                    </a>
                </li>
                {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-right"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>

{% block extra_css %}
<style>
.platform-color {
    color: {{ platform_color }};
}
</style>
{% endblock %}
{% endblock %}
