{% set status_filter = request.query_params.get('status', 'all') %}
<div class="watchlist-container" x-data="{ currentFilter: '{{ status_filter }}' }">
    <!-- Filter Tabs -->
    <div class="filter-tabs">
        <button class="filter-tab {{ 'active' if status_filter == 'all' or not status_filter else '' }}"
                hx-get="/api/watchlist?status=all"
                hx-target="#watchlist-items-container"
                hx-swap="outerHTML"
                @click="$el.closest('.watchlist-container').querySelector('.filter-tab').forEach(t => t.classList.remove('active')); $el.classList.add('active');">
            <i class="fas fa-list"></i> Tous
        </button>
        <button class="filter-tab {{ 'active' if status_filter == 'active' else '' }}"
                hx-get="/api/watchlist?status=active"
                hx-target="#watchlist-items-container"
                hx-swap="outerHTML"
                @click="$el.closest('.watchlist-container').querySelector('.filter-tab').forEach(t => t.classList.remove('active')); $el.classList.add('active');">
            <i class="fas fa-play"></i> Actifs
        </button>
        <button class="filter-tab {{ 'active' if status_filter == 'paused' else '' }}"
                hx-get="/api/watchlist?status=paused"
                hx-target="#watchlist-items-container"
                hx-swap="outerHTML"
                @click="$el.closest('.watchlist-container').querySelector('.filter-tab').forEach(t => t.classList.remove('active')); $el.classList.add('active');">
            <i class="fas fa-pause"></i> En pause
        </button>
        <button class="filter-tab {{ 'active' if status_filter == 'completed' else '' }}"
                hx-get="/api/watchlist?status=completed"
                hx-target="#watchlist-items-container"
                hx-swap="outerHTML"
                @click="$el.closest('.watchlist-container').querySelector('.filter-tab').forEach(t => t.classList.remove('active')); $el.classList.add('active');">
            <i class="fas fa-check"></i> Terminés
        </button>
    </div>

    <!-- Watchlist Items Grid -->
    {% if items and items | length > 0 %}
        <div class="watchlist-grid">
            {% for item in items %}
                <div class="watchlist-card" id="watchlist-{{ item.id }}" data-item-id="{{ item.id }}">
                    <!-- Poster -->
                    <div class="watchlist-poster">
                        <img src="{{ item.poster_image or item.cover_image or '/static/img/no-poster.png' }}"
                             alt="{{ item.anime_title }}"
                             onerror="this.src='/static/img/no-poster.png'">
                        <div class="poster-badge {{ item.status }}">
                            {% if item.status == 'active' %}
                                <i class="fas fa-play"></i> Actif
                            {% elif item.status == 'paused' %}
                                <i class="fas fa-pause"></i> En pause
                            {% elif item.status == 'completed' %}
                                <i class="fas fa-check"></i> Terminé
                            {% else %}
                                <i class="fas fa-archive"></i> Archivé
                            {% endif %}
                        </div>
                        {% if item.auto_download %}
                            <div class="auto-download-badge">
                                <i class="fas fa-magic"></i> Auto
                            </div>
                        {% endif %}
                    </div>

                    <!-- Content -->
                    <div class="watchlist-content">
                        <h3 class="watchlist-title">{{ item.anime_title }}</h3>

                        <div class="watchlist-meta">
                            <span class="meta-provider">
                                <i class="fas fa-tv"></i> {{ item.provider_id | upper }}
                            </span>
                            <span class="meta-lang">{{ item.lang | upper }}</span>
                            {% if item.quality_preference and item.quality_preference != 'auto' %}
                                <span class="meta-quality">{{ item.quality_preference }}</span>
                            {% endif %}
                        </div>

                        {% if item.synopsis %}
                            <p class="watchlist-synopsis">{{ item.synopsis | truncate(150) }}</p>
                        {% endif %}

                        <div class="watchlist-stats">
                            <span class="stat">
                                <i class="fas fa-download"></i>
                                Ép. {{ item.last_episode_downloaded }}
                                {% if item.total_episodes %}
                                / {{ item.total_episodes }}
                                {% endif %}
                            </span>
                            {% if item.added_at %}
                                <span class="stat" title="Ajouté le {{ item.added_at.strftime('%d/%m/%Y') }}">
                                    <i class="fas fa-calendar"></i>
                                    {{ item.added_at.strftime('%d/%m/%Y') }}
                                </span>
                            {% endif %}
                        </div>

                        <!-- Actions -->
                        <div class="watchlist-actions">
                            <!-- Pause/Resume Toggle -->
                            {% if item.status == 'active' %}
                                <button class="action-btn btn-pause"
                                        hx-put="/api/watchlist/{{ item.id }}"
                                        hx-vals='{"status": "paused"}'
                                        hx-swap="none"
                                        hx-on::after-request="htmx.trigger('#watchlist-items-container', 'refresh');"
                                        title="Mettre en pause">
                                    <i class="fas fa-pause"></i>
                                </button>
                            {% elif item.status == 'paused' %}
                                <button class="action-btn btn-resume"
                                        hx-put="/api/watchlist/{{ item.id }}"
                                        hx-vals='{"status": "active"}'
                                        hx-swap="none"
                                        hx-on::after-request="htmx.trigger('#watchlist-items-container', 'refresh');"
                                        title="Reprendre">
                                    <i class="fas fa-play"></i>
                                </button>
                            {% endif %}

                            <!-- Mark as completed -->
                            {% if item.status not in ['completed', 'archived'] %}
                                <button class="action-btn btn-complete"
                                        hx-put="/api/watchlist/{{ item.id }}"
                                        hx-vals='{"status": "completed"}'
                                        hx-swap="none"
                                        hx-on::after-request="htmx.trigger('#watchlist-items-container', 'refresh');"
                                        title="Marquer comme terminé">
                                    <i class="fas fa-check"></i>
                                </button>
                            {% endif %}

                            <!-- Delete -->
                            <button class="action-btn btn-delete"
                                    hx-delete="/api/watchlist/{{ item.id }}"
                                    hx-target="#watchlist-{{ item.id }}"
                                    hx-swap="outerHTML"
                                    hx-confirm="Supprimer '{{ item.anime_title }}' de votre watchlist ?"
                                    title="Supprimer">
                                <i class="fas fa-trash"></i>
                            </button>
                        </div>
                    </div>
                </div>
            {% endfor %}
        </div>
    {% else %}
        <div class="watchlist-empty">
            <i class="fas fa-inbox"></i>
            <h3>Votre watchlist est vide</h3>
            <p>Ajoutez des animes ou séries depuis l'onglet Recherche pour commencer à suivre leurs sorties.</p>
            <button class="btn btn-primary" @click="$dispatch('set-tab', {tab: 'anime'})">
                <i class="fas fa-search"></i> Rechercher des animes
            </button>
        </div>
    {% endif %}
</div>

<style>
/* Container */
.watchlist-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* Filter Tabs */
.filter-tabs {
    display: flex;
    gap: 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding-bottom: 12px;
    margin-bottom: 8px;
}

.filter-tab {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    border: none;
    background: transparent;
    color: var(--text-dim);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    border-radius: var(--input-radius);
    transition: var(--transition);
}

.filter-tab:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-main);
}

.filter-tab.active {
    background: var(--primary);
    color: var(--bg-dark);
}

/* Grid */
.watchlist-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 20px;
}

/* Card */
.watchlist-card {
    display: flex;
    gap: 16px;
    background: var(--bg-card);
    border-radius: var(--card-radius);
    padding: 16px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    transition: var(--transition);
}

.watchlist-card:hover {
    border-color: var(--primary);
    box-shadow: 0 4px 24px rgba(0, 217, 255, 0.15);
    transform: translateY(-2px);
}

/* Poster */
.watchlist-poster {
    position: relative;
    flex-shrink: 0;
    width: 100px;
    aspect-ratio: 2/3;
    border-radius: 8px;
    overflow: hidden;
    background: var(--bg-dark);
}

.watchlist-poster img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.poster-badge {
    position: absolute;
    top: 8px;
    left: 8px;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.65rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    display: flex;
    align-items: center;
    gap: 4px;
}

.poster-badge.active {
    background: rgba(0, 255, 136, 0.9);
    color: var(--bg-dark);
}

.poster-badge.paused {
    background: rgba(255, 193, 7, 0.9);
    color: var(--bg-dark);
}

.poster-badge.completed {
    background: rgba(156, 39, 176, 0.9);
    color: var(--bg-dark);
}

.poster-badge.archived {
    background: rgba(255, 255, 255, 0.15);
    color: var(--text-dim);
}

.auto-download-badge {
    position: absolute;
    bottom: 8px;
    left: 8px;
    padding: 4px 10px;
    border-radius: 20px;
    background: rgba(0, 217, 255, 0.9);
    color: var(--bg-dark);
    font-size: 0.65rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Content */
.watchlist-content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.watchlist-title {
    font-size: 1rem;
    font-weight: 700;
    margin: 0;
    color: var(--text-main);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.watchlist-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    font-size: 0.75rem;
}

.meta-provider,
.meta-lang,
.meta-quality {
    padding: 3px 10px;
    border-radius: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.meta-provider {
    background: rgba(0, 217, 255, 0.15);
    color: var(--primary);
    border: 1px solid rgba(0, 217, 255, 0.3);
}

.meta-lang {
    background: rgba(255, 107, 107, 0.15);
    color: var(--secondary);
    border: 1px solid rgba(255, 107, 107, 0.3);
}

.meta-quality {
    background: rgba(0, 255, 136, 0.15);
    color: var(--accent);
    border: 1px solid rgba(0, 255, 136, 0.3);
}

.watchlist-synopsis {
    font-size: 0.8rem;
    color: var(--text-dim);
    margin: 0;
    line-height: 1.5;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.watchlist-stats {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    font-size: 0.75rem;
    color: var(--text-dim);
}

.stat {
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Actions */
.watchlist-actions {
    display: flex;
    gap: 6px;
    margin-top: auto;
    padding-top: 8px;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
}

.action-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 6px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: var(--transition);
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-dim);
}

.action-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-main);
}

.btn-pause {
    color: #ffc107;
}

.btn-pause:hover {
    background: rgba(255, 193, 7, 0.15);
}

.btn-resume {
    color: var(--accent);
}

.btn-resume:hover {
    background: rgba(0, 255, 136, 0.15);
}

.btn-complete {
    color: #9c27b0;
}

.btn-complete:hover {
    background: rgba(156, 39, 176, 0.15);
}

.btn-delete {
    color: #f44336;
}

.btn-delete:hover {
    background: rgba(244, 67, 54, 0.15);
}

/* Empty State */
.watchlist-empty {
    text-align: center;
    padding: 80px 40px;
    background: var(--bg-card);
    border-radius: var(--card-radius);
    border: 1px dashed rgba(255, 255, 255, 0.1);
}

.watchlist-empty i {
    font-size: 4rem;
    color: var(--text-dim);
    opacity: 0.3;
    margin-bottom: 20px;
    display: block;
}

.watchlist-empty h3 {
    font-size: 1.3rem;
    margin-bottom: 8px;
    color: var(--text-main);
}

.watchlist-empty p {
    color: var(--text-dim);
    margin-bottom: 24px;
}

/* Responsive */
@media (max-width: 768px) {
    .watchlist-grid {
        grid-template-columns: 1fr;
    }

    .filter-tabs {
        overflow-x: auto;
        flex-wrap: nowrap;
    }

    .filter-tab {
        white-space: nowrap;
    }

    .watchlist-card {
        flex-direction: column;
        align-items: center;
        text-align: center;
    }

    .watchlist-poster {
        width: 140px;
    }

    .watchlist-meta,
    .watchlist-stats {
        justify-content: center;
    }
}
</style>
