{% extends "base.html" %}
{% block title %}Chat with {{ partner.first_name }} | Roster{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card shadow-sm border-0 rounded-3">
                <div class="card-header bg-white p-3 border-bottom d-flex align-items-center">
                    <a href="{% url 'inbox' %}" class="btn btn-link text-dark p-0 me-3"><i class="bi bi-chevron-left h4 mb-0"></i></a>
                    <img src="{{ partner.profile.avatar_url|default:'https://ui-avatars.com/api/?name='|add:partner.first_name }}" class="rounded-circle me-2" width="40" height="40" alt="{{ partner.username }}">
                    <div>
                        <h6 class="mb-0 fw-bold">{{ partner.get_full_name|default:partner.username }}</h6>
                        <small class="text-success">Active now</small>
                    </div>
                </div>
                
                <div class="card-body p-4" style="height: 450px; overflow-y: auto;" id="message-container">
                    {% for message in chat_messages %}
                        <div class="d-flex mb-3 {% if message.sender == request.user %}justify-content-end{% endif %}">
                            <div class="p-3 rounded-3 {% if message.sender == request.user %}bg-primary text-white{% else %}bg-light text-dark{% endif %}" style="max-width: 75%;">
                                <p class="mb-1">{{ message.body }}</p>
                                <small class="{% if message.sender == request.user %}text-white-50{% else %}text-muted{% endif %} d-block text-end" style="font-size: 0.7rem;">
                                    {{ message.timestamp|date:"H:i" }}
                                </small>
                            </div>
                        </div>
                    {% empty %}
                        <div class="text-center py-5">
                            <p class="text-muted">No messages yet. Say hello to {{ partner.first_name }}!</p>
                        </div>
                    {% endfor %}
                </div>
                
                <div class="card-footer bg-white p-3 border-top">
                    <form method="POST" class="d-flex gap-2">
                        {% csrf_token %}
                        <input type="text" name="body" class="form-control rounded-pill border-light bg-light px-3" placeholder="Type a message..." required autocomplete="off">
                        <button type="submit" class="btn btn-primary-cg rounded-circle p-2 d-flex align-items-center justify-content-center" style="width: 40px; height: 40px;">
                            <i class="bi bi-send-fill text-white"></i>
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // Scroll to bottom of message container
    const container = document.getElementById('message-container');
    container.scrollTop = container.scrollHeight;
</script>
{% endblock %}
