{% extends "base.html" %}
{% load static %}

{% block title %}Squads Hub | Roster{% endblock %}

{% block content %}
<div class="container-fluid dashboard-container px-lg-5">
    <div class="row">
        <!-- LEFT SIDEBAR -->
        <div class="col-lg-2 d-none d-lg-block">
            <div class="sticky-sidebar">
                <nav class="sidebar-nav">
                    <a href="{% url 'home' %}" class="nav-link"><i class="bi bi-compass"></i> Teammates</a>
                    <a href="{% url 'matches' %}" class="nav-link"><i class="bi bi-people"></i> My Teammates</a>
                    <a href="{% url 'inbox' %}" class="nav-link"><i class="bi bi-chat-dots"></i> Messages</a>
                    <a href="{% url 'events' %}" class="nav-link"><i class="bi bi-calendar-event"></i> Sessions</a>
                    <a href="{% url 'groups:hub' %}" class="nav-link active"><i class="bi bi-grid"></i> Squads</a>
                    <a href="{% url 'my_profile' %}" class="nav-link"><i class="bi bi-person"></i> Profile</a>
                </nav>
            </div>
        </div>

        <!-- CENTER COLUMN -->
        <div class="col-lg-7 col-md-12">
            <!-- Header -->
            <div class="dashboard-section pt-2 d-flex justify-content-between align-items-center">
                <div>
                    <h1 class="welcome-header mb-1">Squads</h1>
                    <p class="text-muted small">Intentional communities for gamers.</p>
                </div>
                <a href="{% url 'groups:create' %}" class="btn btn-primary-cg px-4">
                    <i class="bi bi-plus-lg me-2"></i> Create Squad
                </a>
            </div>

            <!-- YOUR CIRCLES -->
            {% if your_circles %}
            <div class="dashboard-section">
                <h2 class="section-header mb-4">Your Squads</h2>
                <div class="row g-4">
                    {% for group in your_circles %}
                    <div class="col-md-6">
                        <div class="card h-100 shadow-sm border-0" style="border-radius: 16px;">
                            <div class="card-body p-4">
                                <div class="d-flex justify-content-between align-items-start mb-3">
                                    <span class="post-badge">{{ group.get_group_type_display }}</span>
                                    <span class="text-muted small"><i class="bi bi-people me-1"></i> {{ group.memberships.filter.status_active_count|default:group.memberships.count }}</span>
                                </div>
                                <h3 class="h5 fw-bold mb-2">{{ group.name }}</h3>
                                <p class="text-muted small line-clamp-2 mb-4">{{ group.description }}</p>
                                
                                {% if group.group_type == 'mastermind' and group.recurring_schedule %}
                                <div class="bg-light p-3 rounded mb-4">
                                    <p class="extra-small fw-bold text-muted mb-1"><i class="bi bi-calendar-check me-2"></i> NEXT MEETING</p>
                                    <p class="small mb-0">{{ group.recurring_schedule|truncatechars:50 }}</p>
                                </div>
                                {% endif %}

                                <div class="d-grid">
                                    <a href="{% url 'groups:detail' group.pk %}" class="btn btn-outline-cg btn-sm">Enter Space</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
            {% endif %}

            <!-- RECOMMENDED FOR YOU -->
            {% if recommended_groups %}
            <div class="dashboard-section">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="section-header mb-0">Recommended For You</h2>
                </div>
                <div class="horizontal-scroll">
                    {% for group in recommended_groups %}
                    <div class="suggestion-card" style="width: 280px; text-align: left; padding: 24px;">
                        <span class="intent-badge mb-3 d-inline-block">{{ group.get_intent_type_display }}</span>
                        <h6 class="fw-bold mb-2">{{ group.name }}</h6>
                        <p class="extra-small text-muted mb-4 line-clamp-2">{{ group.description }}</p>
                        <div class="d-flex justify-content-between align-items-center mt-auto">
                            <span class="extra-small text-muted">{{ group.memberships.count }} members</span>
                            <a href="{% url 'groups:detail' group.pk %}" class="btn btn-primary-cg btn-sm px-3">View</a>
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
            {% endif %}

            <!-- DISCOVER GROUPS -->
            <div class="dashboard-section">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="section-header mb-0">Discover Squads</h2>
                </div>
                
                <!-- Search & Filters -->
                <div class="card p-3 mb-4 border-0 shadow-soft">
                    <form method="GET" class="row g-3">
                        <div class="col-md-6">
                            <div class="input-group">
                                <span class="input-group-text bg-white border-0"><i class="bi bi-search"></i></span>
                                <input type="text" name="q" class="form-control border-0 bg-white" placeholder="Search by name or focus...">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <select name="type" class="form-select border-0 bg-light">
                                <option value="">All Types</option>
                                <option value="squad">Squad</option>
                                <option value="community">Community</option>
                                <option value="mastermind">Pro Squad</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <button type="submit" class="btn btn-primary-cg w-100">Filter</button>
                        </div>
                    </form>
                </div>

                <div class="row g-4">
                    {% for group in discover_groups %}
                    <div class="col-md-6">
                        <div class="card h-100 shadow-sm border-0" style="border-radius: 16px;">
                            <div class="card-body p-4">
                                <div class="d-flex justify-content-between align-items-start mb-3">
                                    <span class="post-badge">{{ group.get_group_type_display }}</span>
                                    <span class="text-muted small"><i class="bi bi-geo-alt me-1"></i> {{ group.get_location_scope_display }}</span>
                                </div>
                                <h3 class="h5 fw-bold mb-2">{{ group.name }}</h3>
                                <p class="text-muted small line-clamp-2 mb-4">{{ group.description }}</p>
                                
                                <div class="d-flex justify-content-between align-items-center">
                                    <div class="d-flex align-items-center">
                                        <div class="members-overlap me-2">
                                            <!-- Placeholder for member avatars -->
                                            <div class="avatar-sm rounded-circle bg-light border d-inline-block" style="width: 24px; height: 24px;"></div>
                                        </div>
                                        <span class="extra-small text-muted">{{ group.memberships.count }} members</span>
                                    </div>
                                    <a href="{% url 'groups:detail' group.pk %}" class="btn btn-outline-cg btn-sm">Details</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% empty %}
                    <div class="col-12 text-center py-5">
                        <p class="text-muted">No squads found. Be the first to start one!</p>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>

        <!-- RIGHT SIDEBAR -->
        <div class="col-lg-3 d-none d-lg-block">
            <div class="sticky-sidebar pt-2">
                <div class="stat-widget">
                    <h2 class="section-header mb-4" style="font-size: 1.1rem;">Squad Guidelines</h2>
                    <ul class="list-unstyled small text-muted">
                        <li class="mb-3"><i class="bi bi-check2-circle text-sage me-2"></i> <strong>Coordination:</strong> Squads are for active play and team coordination.</li>
                        <li class="mb-3"><i class="bi bi-check2-circle text-sage me-2"></i> <strong>Respect:</strong> Maintain a high-trust, toxicity-free environment.</li>
                        <li class="mb-3"><i class="bi bi-check2-circle text-sage me-2"></i> <strong>Participation:</strong> Pro Squads expect active presence and regular sessions.</li>
                    </ul>
                </div>

                <div class="stat-widget mt-4">
                    <h2 class="section-header mb-4" style="font-size: 1.1rem;">Starting a Squad?</h2>
                    <p class="small text-muted mb-4">Pro Squads require a structured schedule to ensure all members are aligned on practice times.</p>
                    <a href="{% url 'groups:create' %}" class="btn btn-primary-cg btn-sm w-100 py-2">Get Started</a>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
