{% extends "base.html" %}

{% block content %}
<!-- Header -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div class="season-filter d-flex gap-3">
        <a href="?season=summer" class="text-decoration-none">
            <i class="fas fa-sun season-icon {% if season_filter == 'summer' %}active text-warning{% endif %}" title="Summer"></i>
        </a>
        <a href="?season=spring" class="text-decoration-none">
            <i class="fas fa-seedling season-icon {% if season_filter == 'spring' %}active text-success{% endif %}" title="Spring"></i>
        </a>
        <a href="?season=autumn" class="text-decoration-none d-flex align-items-center">
            <span class="season-icon {% if season_filter == 'autumn' %}active{% endif %}" title="Autumn" style="{% if season_filter == 'autumn' %}color: #8B4513; opacity: 1;{% endif %}">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                    <path d="M12 2L10 4L12 6L14 4L12 2Z" />
                    <path d="M12 6V22" stroke="currentColor" stroke-width="2" />
                    <path d="M12 10L8 8" stroke="currentColor" stroke-width="2" />
                    <path d="M12 14L16 12" stroke="currentColor" stroke-width="2" />
                    <path d="M12 18L7 16" stroke="currentColor" stroke-width="2" />
                    <path d="M12 12L17 10" stroke="currentColor" stroke-width="2" />
                </svg>
            </span>
        </a>
        <a href="?season=winter" class="text-decoration-none">
            <i class="fas fa-snowflake season-icon {% if season_filter == 'winter' %}active text-info{% endif %}" title="Winter"></i>
        </a>
    </div>
    <div class="text-center">
        <div class="small text-uppercase tracking-wider text-dim" style="letter-spacing: 1px;">Total Items</div>
        <h3 class="mb-0 fw-bold accent-lavender">{{ total_items }}</h3>
    </div>
    <div class="avatar-selector">
        <div class="rounded-circle d-flex align-items-center justify-content-center border border-2 border-secondary shadow-sm" style="width: 48px; height: 48px; background-color: #333; color: #999;">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 12C14.21 12 16 10.21 16 8C16 5.79 14.21 4 12 4C9.79 4 8 5.79 8 8C8 10.21 9.79 12 12 12ZM12 14C9.33 14 4 15.34 4 18V20H20V18C20 15.34 14.67 14 12 14Z"/>
            </svg>
        </div>
    </div>
</div>

<!-- Weekly Calendar -->
<div class="calendar-grid">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h6 class="mb-0 text-dim fw-bold"><i class="far fa-calendar-alt me-2"></i>Weekly Planner</h6>
        <a href="{% url 'new_fit' %}" class="btn btn-sm btn-outline-secondary py-0" style="font-size: 0.7rem;">+ Plan</a>
    </div>
    
    <!-- Row 1: Mon, Tue, Wed -->
    <div class="row g-3 mb-3">
        {% for day in calendar_data|slice:":3" %}
        <div class="col-4">
            <div class="glass-card p-2 text-center h-100 d-flex flex-column justify-content-between">
                <div>
                    <div class="small text-dim">{{ day.day }}</div>
                    <div class="fw-bold {% if day.date == today %}accent-lavender{% endif %}">{{ day.date|date:"d" }}</div>
                </div>
                <div class="outfit-preview my-2 position-relative">
                    {% if day.outfit %}
                        <form action="{% url 'remove_assignment' day.date|date:'Y-m-d' %}" method="post" class="position-absolute top-0 end-0 z-1">
                            {% csrf_token %}
                            <button type="submit" class="btn btn-link text-danger p-0" style="line-height: 1; margin-top: -5px; margin-right: -5px;">
                                <i class="fas fa-times-circle" style="font-size: 0.8rem;"></i>
                            </button>
                        </form>
                        <div class="rounded bg-secondary position-relative overflow-hidden" style="height: 60px;">
                            {% with first_item=day.outfit.items.first %}
                                {% if first_item %}
                                    <img src="{{ first_item.image.url }}" class="w-100 h-100 object-fit-cover" alt="">
                                {% else %}
                                    <div class="w-100 h-100 d-flex align-items-center justify-content-center bg-dark">
                                        <i class="fas fa-tshirt text-dim"></i>
                                    </div>
                                {% endif %}
                            {% endwith %}
                        </div>
                        <div class="small mt-1 text-truncate" style="font-size: 0.7rem;">{{ day.outfit.name|default:"Outfit" }}</div>
                    {% else %}
                        <div class="rounded border border-dashed border-secondary d-flex align-items-center justify-content-center cursor-pointer" 
                             style="height: 60px; border-style: dashed !important; background: rgba(255,255,255,0.02);"
                             data-bs-toggle="modal" data-bs-target="#assignModal" data-date="{{ day.date|date:'Y-m-d' }}" data-day="{{ day.day }}">
                            <i class="fas fa-plus text-dim" style="font-size: 0.8rem;"></i>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endfor %}
    </div>

    <!-- Row 2: Thu, Fri -->
    <div class="row g-3 mb-3">
        {% for day in calendar_data|slice:"3:5" %}
        <div class="col-6">
            <div class="glass-card p-3 text-center h-100 d-flex flex-column justify-content-between">
                <div>
                    <div class="small text-dim">{{ day.day }}</div>
                    <div class="fw-bold {% if day.date == today %}accent-lavender{% endif %}">{{ day.date|date:"d" }}</div>
                </div>
                <div class="outfit-preview my-2 position-relative">
                    {% if day.outfit %}
                        <form action="{% url 'remove_assignment' day.date|date:'Y-m-d' %}" method="post" class="position-absolute top-0 end-0 z-1">
                            {% csrf_token %}
                            <button type="submit" class="btn btn-link text-danger p-0" style="line-height: 1; margin-top: -5px; margin-right: -5px;">
                                <i class="fas fa-times-circle" style="font-size: 0.9rem;"></i>
                            </button>
                        </form>
                        <div class="rounded bg-secondary position-relative overflow-hidden" style="height: 80px;">
                             {% with first_item=day.outfit.items.first %}
                                {% if first_item %}
                                    <img src="{{ first_item.image.url }}" class="w-100 h-100 object-fit-cover" alt="">
                                {% else %}
                                    <div class="w-100 h-100 d-flex align-items-center justify-content-center bg-dark">
                                        <i class="fas fa-tshirt text-dim"></i>
                                    </div>
                                {% endif %}
                            {% endwith %}
                        </div>
                        <div class="small mt-1 text-truncate">{{ day.outfit.name|default:"Outfit" }}</div>
                    {% else %}
                        <div class="rounded border border-dashed border-secondary d-flex align-items-center justify-content-center cursor-pointer" 
                             style="height: 80px; border-style: dashed !important; background: rgba(255,255,255,0.02);"
                             data-bs-toggle="modal" data-bs-target="#assignModal" data-date="{{ day.date|date:'Y-m-d' }}" data-day="{{ day.day }}">
                            <i class="fas fa-plus text-dim"></i>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endfor %}
    </div>

    <!-- Row 3: Sat, Sun -->
    <div class="row g-3">
        {% for day in calendar_data|slice:"5:" %}
        <div class="col-6">
            <div class="glass-card p-3 text-center h-100 d-flex flex-column justify-content-between">
                <div>
                    <div class="small text-dim">{{ day.day }}</div>
                    <div class="fw-bold {% if day.date == today %}accent-lavender{% endif %}">{{ day.date|date:"d" }}</div>
                </div>
                <div class="outfit-preview my-2 position-relative">
                    {% if day.outfit %}
                        <form action="{% url 'remove_assignment' day.date|date:'Y-m-d' %}" method="post" class="position-absolute top-0 end-0 z-1">
                            {% csrf_token %}
                            <button type="submit" class="btn btn-link text-danger p-0" style="line-height: 1; margin-top: -5px; margin-right: -5px;">
                                <i class="fas fa-times-circle" style="font-size: 0.9rem;"></i>
                            </button>
                        </form>
                        <div class="rounded bg-secondary position-relative overflow-hidden" style="height: 80px;">
                             {% with first_item=day.outfit.items.first %}
                                {% if first_item %}
                                    <img src="{{ first_item.image.url }}" class="w-100 h-100 object-fit-cover" alt="">
                                {% else %}
                                    <div class="w-100 h-100 d-flex align-items-center justify-content-center bg-dark">
                                        <i class="fas fa-tshirt text-dim"></i>
                                    </div>
                                {% endif %}
                            {% endwith %}
                        </div>
                        <div class="small mt-1 text-truncate">{{ day.outfit.name|default:"Outfit" }}</div>
                    {% else %}
                        <div class="rounded border border-dashed border-secondary d-flex align-items-center justify-content-center cursor-pointer" 
                             style="height: 80px; border-style: dashed !important; background: rgba(255,255,255,0.02);"
                             data-bs-toggle="modal" data-bs-target="#assignModal" data-date="{{ day.date|date:'Y-m-d' }}" data-day="{{ day.day }}">
                            <i class="fas fa-plus text-dim"></i>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

<!-- Assign Outfit Modal -->
<div class="modal fade" id="assignModal" tabindex="-1" aria-labelledby="assignModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-fullscreen-sm-down">
        <div class="modal-content bg-dark text-white border-secondary">
            <div class="modal-header border-secondary">
                <h5 class="modal-title" id="assignModalLabel">Select Outfit for <span id="modalDateDisplay"></span></h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body p-0" style="max-height: 70vh; overflow-y: auto;">
                <div class="list-group list-group-flush">
                    {% for outfit in all_outfits %}
                    <form action="{% url 'assign_outfit' %}" method="post">
                        {% csrf_token %}
                        <input type="hidden" name="outfit_id" value="{{ outfit.id }}">
                        <input type="hidden" name="date" class="modal-date-input">
                        <button type="submit" class="list-group-item list-group-item-action bg-dark text-white border-secondary d-flex align-items-center gap-3 py-3">
                            <div class="rounded bg-secondary overflow-hidden" style="width: 50px; height: 50px; flex-shrink: 0;">
                                {% with first_item=outfit.items.first %}
                                    {% if first_item %}
                                        <img src="{{ first_item.image.url }}" class="w-100 h-100 object-fit-cover" alt="">
                                    {% else %}
                                        <div class="w-100 h-100 d-flex align-items-center justify-content-center bg-dark">
                                            <i class="fas fa-tshirt text-dim"></i>
                                        </div>
                                    {% endif %}
                                {% endwith %}
                            </div>
                            <div class="flex-grow-1">
                                <div class="fw-bold">{{ outfit.name|default:"Unnamed Outfit" }}</div>
                                <div class="small text-dim">{{ outfit.items.count }} items, {{ outfit.accessories.count }} accessories</div>
                            </div>
                            <i class="fas fa-chevron-right text-dim"></i>
                        </button>
                    </form>
                    {% empty %}
                    <div class="p-4 text-center text-dim">
                        <i class="fas fa-info-circle mb-2 d-block" style="font-size: 2rem;"></i>
                        <p>No outfits created yet.</p>
                        <a href="{% url 'new_fit' %}" class="btn btn-sm btn-outline-lavender">Create your first fit</a>
                    </div>
                    {% endfor %}
                </div>
            </div>
            <div class="modal-footer border-secondary">
                <a href="#" id="createNewFitLink" class="btn btn-outline-lavender w-100">Create New Fit for this day</a>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_css %}
<style>
    .text-dim { color: var(--text-dim); }
    .object-fit-cover { object-fit: cover; }
    .tracking-wider { letter-spacing: 0.05em; }
    .cursor-pointer { cursor: pointer; }
    .btn-outline-lavender {
        color: var(--primary-accent);
        border-color: var(--primary-accent);
    }
    .btn-outline-lavender:hover {
        background-color: var(--primary-accent);
        color: #000;
    }
</style>
{% endblock %}

{% block extra_js %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        var assignModal = document.getElementById('assignModal');
        assignModal.addEventListener('show.bs.modal', function (event) {
            var button = event.relatedTarget;
            var date = button.getAttribute('data-date');
            var day = button.getAttribute('data-day');
            
            document.getElementById('modalDateDisplay').textContent = day + ' (' + date + ')';
            
            var dateInputs = assignModal.querySelectorAll('.modal-date-input');
            dateInputs.forEach(function(input) {
                input.value = date;
            });

            var createLink = document.getElementById('createNewFitLink');
            createLink.href = "{% url 'new_fit' %}?date=" + date;
        });
    });
</script>
{% endblock %}