{% extends "base.html" %}
{% load static %}

{% block title %}{{ message.subject }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <!-- Message Header -->
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">
                        {{ message.subject }}
                        {% if message.parent_message %}
                            <span class="badge bg-secondary ms-2">Reply</span>
                        {% endif %}
                    </h5>
                    <div class="btn-group" role="group">
                        <a href="{% url 'message_reply' message.id %}" class="btn btn-outline-info">
                            <i class="fas fa-reply"></i> Reply
                        </a>
                        {% if message.recipient == request.user %}
                            <a href="{% url 'message_mark_unread' message.id %}"
                               class="btn btn-outline-warning"
                               hx-post="{% url 'message_mark_unread' message.id %}">
                                <i class="fas fa-envelope"></i> Mark Unread
                            </a>
                        {% endif %}
                        <a href="{% url 'message_delete' message.id %}"
                           class="btn btn-outline-danger"
                           hx-get="{% url 'message_delete' message.id %}"
                           hx-confirm="Are you sure you want to delete this message?">
                            <i class="fas fa-trash"></i> Delete
                        </a>
                        <a href="{% url 'message_list' %}" class="btn btn-outline-secondary">
                            <i class="fas fa-arrow-left"></i> Back to Messages
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <strong>From:</strong>
                            <span class="text-primary">{{ message.sender.get_full_name|default:message.sender.username }}</span>
                        </div>
                        <div class="col-md-6">
                            <strong>To:</strong>
                            <span class="text-primary">{{ message.recipient.get_full_name|default:message.recipient.username }}</span>
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <strong>Type:</strong>
                            <span class="badge bg-{{ message.message_type|lower }}">
                                {{ message.get_message_type_display }}
                            </span>
                        </div>
                        <div class="col-md-6">
                            <strong>Status:</strong>
                            {% if message.is_read %}
                                <span class="badge bg-success">Read</span>
                                {% if message.read_at %}
                                    <small class="text-muted">({{ message.read_at|date:"M d, Y H:i" }})</small>
                                {% endif %}
                            {% else %}
                                <span class="badge bg-warning">Unread</span>
                            {% endif %}
                        </div>
                    </div>
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <strong>Created:</strong>
                            <span>{{ message.created_at|date:"M d, Y H:i" }}</span>
                        </div>
                        {% if message.job %}
                            <div class="col-md-6">
                                <strong>Related Job:</strong>
                                <a href="{% url 'job_detail' message.job.slug %}" class="text-primary">
                                    {{ message.job.title }}
                                </a>
                            </div>
                        {% endif %}
                    </div>
                    {% if message.parent_message %}
                        <div class="alert alert-info">
                            <strong>In reply to:</strong>
                            <a href="{% url 'message_detail' message.parent_message.id %}">
                                {{ message.parent_message.subject }}
                            </a>
                            <small class="text-muted d-block">
                                From {{ message.parent_message.sender.get_full_name|default:message.parent_message.sender.username }}
                                on {{ message.parent_message.created_at|date:"M d, Y H:i" }}
                            </small>
                        </div>
                    {% endif %}
                </div>
            </div>

            <!-- Message Content -->
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0">Message</h6>
                </div>
                <div class="card-body">
                    <div class="message-content">
                        {{ message.content|linebreaks }}
                    </div>
                </div>
            </div>

            <!-- Message Thread (if this is a reply and has replies) -->
            {% if message.replies.all %}
                <div class="card mt-4">
                    <div class="card-header">
                        <h6 class="mb-0">
                            <i class="fas fa-comments"></i> Replies ({{ message.replies.count }})
                        </h6>
                    </div>
                    <div class="card-body">
                        {% for reply in message.replies.all %}
                            <div class="border-start ps-3 mb-3">
                                <div class="d-flex justify-content-between align-items-start mb-2">
                                    <div>
                                        <strong>{{ reply.sender.get_full_name|default:reply.sender.username }}</strong>
                                        <small class="text-muted ms-2">
                                            {{ reply.created_at|date:"M d, Y H:i" }}
                                        </small>
                                    </div>
                                    <span class="badge bg-{{ reply.message_type|lower }}">
                                        {{ reply.get_message_type_display }}
                                    </span>
                                </div>
                                <div class="reply-content">
                                    {{ reply.content|linebreaks }}
                                </div>
                                <div class="mt-2">
                                    <a href="{% url 'message_reply' reply.id %}" class="btn btn-sm btn-outline-info">
                                        <i class="fas fa-reply"></i> Reply to this
                                    </a>
                                </div>
                            </div>
                        {% endfor %}
                    </div>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_css %}
<style>
.message-content {
    white-space: pre-wrap;
    word-wrap: break-word;
    line-height: 1.6;
    padding: 1rem;
    background-color: #f8f9fa;
    border-radius: 0.375rem;
    border: 1px solid #dee2e6;
}

.reply-content {
    white-space: pre-wrap;
    word-wrap: break-word;
    line-height: 1.5;
    font-size: 0.9rem;
}

.border-start {
    border-left: 3px solid #0d6efd;
}

.ps-3 {
    padding-left: 1rem;
}
</style>
{% endblock %}
