{% extends "base.html" %}

{% block title %}My Bookmarks - Knowledge Base{% endblock %}

{% block content %}
<div class="row">
    <!-- Sidebar -->
    <div class="col-md-3">
        <div class="card p-3 mb-4 shadow-sm border-0 bg-white rounded-3">
            <h5 class="h6 text-uppercase fw-bold text-muted mb-3">Filter by Tag</h5>
            <div class="d-flex flex-wrap gap-2">
                <a href="{% url 'home' %}" class="badge {% if not request.GET.tag %}bg-primary{% else %}bg-light text-dark border{% endif %} text-decoration-none p-2">All</a>
                {% for tag in all_tags %}
                    <a href="?tag={{ tag.name }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}" 
                       class="badge {% if request.GET.tag == tag.name %}bg-primary{% else %}bg-light text-dark border{% endif %} text-decoration-none p-2">
                        #{{ tag.name }}
                    </a>
                {% endfor %}
            </div>
        </div>

        <div class="card p-3 mb-4 shadow-sm border-0 bg-white rounded-3">
            <h5 class="h6 text-uppercase fw-bold text-muted mb-3">My Teams</h5>
            <div class="list-group list-group-flush small">
                {% for team in teams %}
                    <a href="{% url 'team-detail' team.pk %}" class="list-group-item list-group-item-action border-0 px-0 d-flex justify-content-between align-items-center">
                        <span><i class="bi bi-people me-2"></i>{{ team.name }}</span>
                    </a>
                {% empty %}
                    <p class="text-muted small mb-0">No teams yet. <a href="{% url 'team-list' %}">Explore</a></p>
                {% endfor %}
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="col-md-9">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1 class="h3 mb-0">{% if request.GET.tag %}Tag: #{{ request.GET.tag }}{% else %}My Bookmarks{% endif %}</h1>
            <a href="{% url 'bookmark-add' %}" class="btn btn-primary rounded-pill px-4">
                <i class="bi bi-plus-lg me-1"></i> New Bookmark
            </a>
        </div>

        <form method="get" class="mb-4">
            <div class="input-group input-group-lg shadow-sm">
                <span class="input-group-text bg-white border-end-0"><i class="bi bi-search text-muted"></i></span>
                <input type="text" name="q" class="form-control border-start-0 ps-0" placeholder="Search by title, url, notes or content..." value="{{ request.GET.q }}">
                {% if request.GET.tag %}
                    <input type="hidden" name="tag" value="{{ request.GET.tag }}">
                {% endif %}
                <button type="submit" class="btn btn-primary px-4">Search</button>
            </div>
        </form>

        <div class="row">
            {% for bookmark in bookmarks %}
            <div class="col-md-12 mb-3">
                <div class="card border-0 shadow-sm hover-elevate">
                    <div class="card-body p-4">
                        <div class="d-flex justify-content-between align-items-start">
                            <div class="flex-grow-1">
                                <div class="d-flex align-items-center mb-1">
                                    <h5 class="card-title mb-0 me-2">
                                        <a href="{% url 'bookmark-detail' bookmark.pk %}" class="text-decoration-none text-dark fw-bold">{{ bookmark.title|default:bookmark.url }}</a>
                                    </h5>
                                    {% if bookmark.is_favorite %}
                                        <i class="bi bi-star-fill text-warning" title="Favorite"></i>
                                    {% endif %}
                                </div>
                                <p class="text-muted small mb-3 text-break">{{ bookmark.url }}</p>
                                
                                {% if bookmark.notes %}
                                    <p class="card-text text-secondary mb-3">{{ bookmark.notes|truncatewords:40 }}</p>
                                {% endif %}

                                <div class="d-flex flex-wrap gap-1 mt-2">
                                    {% for tag in bookmark.tags.all %}
                                        <span class="badge bg-light text-muted border-0 small">#{{ tag.name }}</span>
                                    {% endfor %}
                                </div>
                            </div>
                            <div class="text-end ms-3">
                                <div class="dropdown">
                                    <button class="btn btn-link text-muted p-0" type="button" data-bs-toggle="dropdown">
                                        <i class="bi bi-three-dots-vertical"></i>
                                    </button>
                                    <ul class="dropdown-menu dropdown-menu-end">
                                        <li><a class="dropdown-item" href="{% url 'bookmark-detail' bookmark.pk %}">View Details</a></li>
                                        <li><a class="dropdown-item" href="{% url 'bookmark-edit' bookmark.pk %}">Edit</a></li>
                                        <li><hr class="dropdown-divider"></li>
                                        <li>
                                            <form action="{% url 'bookmark-delete' bookmark.pk %}" method="post" style="display: inline;">
                                                {% csrf_token %}
                                                <button type="submit" class="dropdown-item text-danger" onclick="return confirm('Are you sure?')">Delete</button>
                                            </form>
                                        </li>
                                    </ul>
                                </div>
                                <span class="text-muted small d-block mt-4">{{ bookmark.created_at|date:"M d" }}</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% empty %}
            <div class="col-12 text-center py-5">
                <div class="card border-0 shadow-sm p-5 bg-white">
                    <div class="display-1 text-light mb-3"><i class="bi bi-bookmark-plus"></i></div>
                    <h3 class="h4">No bookmarks found</h3>
                    <p class="text-muted">Start building your knowledge base by adding your first bookmark.</p>
                    <div class="mt-3">
                        <a href="{% url 'bookmark-add' %}" class="btn btn-primary rounded-pill px-4">Add Bookmark</a>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>

        {% if is_paginated %}
        <nav class="mt-4">
            <ul class="pagination justify-content-center">
                {% if page_obj.has_previous %}
                    <li class="page-item"><a class="page-link rounded-circle me-2 border-0 shadow-sm" href="?page={{ page_obj.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.tag %}&tag={{ request.GET.tag }}{% endif %}"><i class="bi bi-chevron-left"></i></a></li>
                {% endif %}
                
                {% for i in paginator.page_range %}
                    {% if page_obj.number == i %}
                        <li class="page-item active"><span class="page-link rounded-circle me-2 border-0 shadow-sm">{{ i }}</span></li>
                    {% elif i > page_obj.number|add:'-3' and i < page_obj.number|add:'3' %}
                        <li class="page-item"><a class="page-link rounded-circle me-2 border-0 shadow-sm text-dark" href="?page={{ i }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.tag %}&tag={{ request.GET.tag }}{% endif %}">{{ i }}</a></li>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                    <li class="page-item"><a class="page-link rounded-circle border-0 shadow-sm" href="?page={{ page_obj.next_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.tag %}&tag={{ request.GET.tag }}{% endif %}"><i class="bi bi-chevron-right"></i></a></li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    </div>
</div>

<style>
    .hover-elevate:hover {
        transform: translateY(-4px);
        box-shadow: 0 10px 20px rgba(0,0,0,0.08) !important;
        transition: all 0.3s ease;
    }
</style>
{% endblock %}