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

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

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-chat-dots text-purple me-2"></i>
                {% trans "Social Media Monitoring" %}
            </h2>
            <p class="text-muted mb-0">{% trans "Track social media mentions and sentiment across all platforms" %}</p>
        </div>
        <div>
            <a href="{% url 'social:social_analytics' %}" class="btn btn-outline-primary me-2">
                <i class="bi bi-graph-up me-1"></i> {% trans "Analytics" %}
            </a>
            <a href="{% url 'social:social_export_csv' %}" class="btn btn-outline-success me-2">
                <i class="bi bi-file-earmark-csv me-1"></i> CSV
            </a>
            <a href="{% url 'social:social_export_excel' %}" class="btn btn-outline-success">
                <i class="bi bi-file-earmark-excel me-1"></i> Excel
            </a>
        </div>
    </div>

    <!-- Platform Cards -->
    <div class="row mb-4">
        {% for platform_code, platform_name in platforms %}
        <div class="col-md-2 col-sm-4 mb-3">
            <a href="{% url 'social:social_platform' platform_code %}" class="text-decoration-none">
            <div class="card platform-card h-100 border-0 shadow-sm">
                <div class="card-body text-center">
                    <div class="mb-2">
                        {% social_icon platform_code %}
                    </div>
                    <h6 class="mb-0">{{ platform_name }}</h6>
                    <small class="text-muted">
                        {{ stats|lookup:platform_code }} {% trans "comments" %}
                    </small>
                </div>
            </div>
            </a>
        </div>
        {% endfor %}
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-primary mb-3">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Total Comments" %}</h6>
                    <h3 class="mb-0">{{ stats.total }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-success mb-3">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Positive" %}</h6>
                    <h3 class="mb-0 text-success">{{ stats.positive }}</h3>
                    <small class="text-muted">
                        {% widthratio stats.positive stats.total 100 %}% {% trans "of total" %}
                    </small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-secondary mb-3">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Neutral" %}</h6>
                    <h3 class="mb-0 text-secondary">{{ stats.neutral }}</h3>
                    <small class="text-muted">
                        {% widthratio stats.neutral stats.total 100 %}% {% trans "of total" %}
                    </small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-danger mb-3">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Negative" %}</h6>
                    <h3 class="mb-0 text-danger">{{ stats.negative }}</h3>
                    <small class="text-muted">
                        {% widthratio stats.negative stats.total 100 %}% {% trans "of total" %}
                    </small>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="card mb-4">
        <div class="card-header">
            <button class="btn btn-link text-decoration-none p-0" type="button" data-bs-toggle="collapse" data-bs-target="#filtersCollapse">
                <i class="bi bi-funnel me-1"></i> {% trans "Advanced Filters" %}
            </button>
        </div>
        <div class="collapse" id="filtersCollapse">
            <div class="card-body">
                <form method="get" class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">{% trans "Platform" %}</label>
                        <select name="platform" class="form-select">
                            <option value="">{% trans "All Platforms" %}</option>
                            {% for platform_code, platform_name in platforms %}
                            <option value="{{ platform_code }}" {% if filters.platform == platform_code %}selected{% endif %}>
                                {{ platform_name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{% trans "Sentiment" %}</label>
                        <select name="sentiment" class="form-select">
                            <option value="">{% trans "All Sentiments" %}</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 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-search me-1"></i> {% trans "Filter" %}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Comments Feed -->
    <div class="row">
        {% for comment in comments %}
        <div class="col-lg-6 mb-3">
            <div class="card h-100 social-card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <div>
                            <span class="badge bg-primary mb-1">{{ comment.get_platform_display }}</span>
                            {% if comment.rating %}
                            <span class="badge bg-warning text-dark mb-1">{{ 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>
                    
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <small class="text-muted me-3">
                                <i class="bi bi-heart"></i> {{ comment.like_count }}
                            </small>
                            <small class="text-muted">
                                <i class="bi bi-chat"></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-dots" style="font-size: 3rem; color: #ccc;"></i>
                <p class="text-muted mt-3">{% trans "No comments found" %}</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>

<style>
.platform-card:hover {
    transform: translateY(-5px);
    transition: all 0.3s ease;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15) !important;
}

.platform-icon[data-platform="facebook"] { color: #1877F2; }
.platform-icon[data-platform="instagram"] { color: #C13584; }
.platform-icon[data-platform="youtube"] { color: #FF0000; }
.platform-icon[data-platform="twitter"] { color: #1DA1F2; }
.platform-icon[data-platform="linkedin"] { color: #0077B5; }
.platform-icon[data-platform="tiktok"] { color: #000000; }
.platform-icon[data-platform="google"] { color: #4285F4; }

.social-card {
    border-left: 4px solid #6c757d;
}

.social-card .badge.bg-primary {
    background-color: var(--platform-color) !important;
}
</style>

{% block extra_js %}
<script>
// Set platform colors dynamically
document.querySelectorAll('.platform-card').forEach(card => {
    const icon = card.querySelector('.platform-icon');
    const platform = icon.dataset.platform;
    const colors = {
        facebook: '#1877F2',
        instagram: 'linear-gradient(45deg, #f09433, #e6683c, #dc2743, #cc2366, #bc1888)',
        youtube: '#FF0000',
        twitter: '#1DA1F2',
        linkedin: '#0077B5',
        tiktok: '#000000',
        google: '#4285F4'
    };
    const color = colors[platform];
    if (platform === 'instagram') {
        card.style.background = color;
        card.style.color = 'white';
    } else {
        card.style.borderLeft = `4px solid ${color}`;
    }
});
</script>
{% endblock %}
{% endblock %}
