{% 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">
            <i class="fas fa-tree season-icon {% if season_filter == 'autumn' %}active{% endif %}" title="Autumn" style="{% if season_filter == 'autumn' %}color: #8B4513 !important; opacity: 1;{% endif %}"></i>
        </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 cursor-pointer" data-bs-toggle="modal" data-bs-target="#avatarModal">
        {% if profile.avatar %}
            <img src="{{ profile.avatar.url }}" class="rounded-circle border border-2 border-secondary shadow-sm" style="width: 48px; height: 48px; object-fit: cover;">
        {% else %}
            <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>
        {% endif %}
    </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-2 mb-2">
        {% 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" style="font-size: 0.65rem;">{{ day.day }}</div>
                    <div class="fw-bold {% if day.date == today %}accent-lavender{% endif %}" style="font-size: 0.9rem;">{{ day.date|date:"d" }}</div>
                </div>
                <div class="outfit-preview my-1 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: -8px; margin-right: -8px;">
                                <i class="fas fa-times-circle" style="font-size: 0.9rem; background: rgba(0,0,0,0.6); border-radius: 50%;"></i>
                            </button>
                        </form>
                        <div class="rounded bg-secondary position-relative overflow-hidden d-flex align-items-center justify-content-center p-1" style="height: 100px;">
                            <div class="d-flex align-items-center justify-content-center h-100 w-100 overflow-hidden" style="gap: 3px;">
                                {% for item in day.outfit.items.all %}
                                    <img src="{{ item.image.url }}" class="outfit-item-square shadow-sm" alt="{{ item.name }}">
                                {% endfor %}
                                {% for acc in day.outfit.accessories.all %}
                                    <img src="{{ acc.image.url }}" class="outfit-item-square shadow-sm" alt="{{ acc.name }}">
                                {% endfor %}
                            </div>
                            {% if not day.outfit.items.exists and not day.outfit.accessories.exists %}
                                <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 %}
                        </div>
                        <div class="small mt-1 text-truncate fw-bold" 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: 100px; 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: 1rem;"></i>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endfor %}
    </div>

    <!-- Row 2: Thu, Fri -->
    <div class="row g-2 mb-2">
        {% for day in calendar_data|slice:"3:5" %}
        <div class="col-6">
            <div class="glass-card p-2 text-center h-100 d-flex flex-column justify-content-between">
                <div>
                    <div class="small text-dim" style="font-size: 0.65rem;">{{ day.day }}</div>
                    <div class="fw-bold {% if day.date == today %}accent-lavender{% endif %}" style="font-size: 0.9rem;">{{ day.date|date:"d" }}</div>
                </div>
                <div class="outfit-preview my-1 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: -8px; margin-right: -8px;">
                                <i class="fas fa-times-circle" style="font-size: 0.9rem; background: rgba(0,0,0,0.6); border-radius: 50%;"></i>
                            </button>
                        </form>
                        <div class="rounded bg-secondary position-relative overflow-hidden d-flex align-items-center justify-content-center p-1" style="height: 100px;">
                            <div class="d-flex align-items-center justify-content-center h-100 w-100 overflow-hidden" style="gap: 4px;">
                                {% for item in day.outfit.items.all %}
                                    <img src="{{ item.image.url }}" class="outfit-item-square shadow-sm" alt="{{ item.name }}">
                                {% endfor %}
                                {% for acc in day.outfit.accessories.all %}
                                    <img src="{{ acc.image.url }}" class="outfit-item-square shadow-sm" alt="{{ acc.name }}">
                                {% endfor %}
                            </div>
                            {% if not day.outfit.items.exists and not day.outfit.accessories.exists %}
                                <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 %}
                        </div>
                        <div class="small mt-1 text-truncate fw-bold" 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: 100px; 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: 1rem;"></i>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endfor %}
    </div>

    <!-- Row 3: Sat, Sun -->
    <div class="row g-2">
        {% for day in calendar_data|slice:"5:" %}
        <div class="col-6">
            <div class="glass-card p-2 text-center h-100 d-flex flex-column justify-content-between">
                <div>
                    <div class="small text-dim" style="font-size: 0.65rem;">{{ day.day }}</div>
                    <div class="fw-bold {% if day.date == today %}accent-lavender{% endif %}" style="font-size: 0.9rem;">{{ day.date|date:"d" }}</div>
                </div>
                <div class="outfit-preview my-1 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: -8px; margin-right: -8px;">
                                <i class="fas fa-times-circle" style="font-size: 0.9rem; background: rgba(0,0,0,0.6); border-radius: 50%;"></i>
                            </button>
                        </form>
                        <div class="rounded bg-secondary position-relative overflow-hidden d-flex align-items-center justify-content-center p-1" style="height: 100px;">
                            <div class="d-flex align-items-center justify-content-center h-100 w-100 overflow-hidden" style="gap: 4px;">
                                {% for item in day.outfit.items.all %}
                                    <img src="{{ item.image.url }}" class="outfit-item-square shadow-sm" alt="{{ item.name }}">
                                {% endfor %}
                                {% for acc in day.outfit.accessories.all %}
                                    <img src="{{ acc.image.url }}" class="outfit-item-square shadow-sm" alt="{{ acc.name }}">
                                {% endfor %}
                            </div>
                            {% if not day.outfit.items.exists and not day.outfit.accessories.exists %}
                                <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 %}
                        </div>
                        <div class="small mt-1 text-truncate fw-bold" 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: 100px; 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: 1rem;"></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 d-flex align-items-center justify-content-center p-1" style="width: 80px; height: 50px; flex-shrink: 0;">
                                <div class="d-flex align-items-center justify-content-center h-100 w-100 overflow-hidden" style="gap: 2px;">
                                    {% for item in outfit.items.all|slice:":3" %}
                                        <img src="{{ item.image.url }}" class="outfit-item-square" style="height: 70% !important;" alt="">
                                    {% empty %}
                                        <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>
                                    {% endfor %}
                                </div>
                            </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>

<!-- Avatar Update Modal -->
<div class="modal fade" id="avatarModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content bg-dark text-white border-secondary">
            <div class="modal-header border-secondary">
                <h5 class="modal-title">Change Profile Picture</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{% url 'update_avatar' %}" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                <div class="modal-body">
                    <div class="mb-3 text-center">
                        {% if profile.avatar %}
                            <img src="{{ profile.avatar.url }}" class="rounded-circle border border-2 border-secondary mb-3" style="width: 100px; height: 100px; object-fit: cover;">
                        {% endif %}
                        <div class="text-start">
                            <label class="form-label small text-uppercase fw-bold text-dim">Select Image</label>
                            {{ avatar_form.avatar }}
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-secondary">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-accent px-4">Save Changes</button>
                </div>
            </form>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_css %}
<style>
    .text-dim { color: var(--text-dim); }
    .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;
    }
    .season-icon { font-size: 1.5rem; transition: all 0.2s; opacity: 0.5; }
    .season-icon.active { opacity: 1; transform: scale(1.2); }

    .outfit-item-square {
        height: 75%;
        max-width: 30%;
        aspect-ratio: 1/1;
        object-fit: cover;
        border-radius: 4px;
        transition: transform 0.2s;
        border: 1px solid rgba(255,255,255,0.1);
    }
    .outfit-item-square:hover {
        transform: scale(1.05);
        z-index: 5;
    }
</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 %}