@model IEnumerable<LittleShop.DTOs.MessageThreadDto>

@{
    ViewData["Title"] = "Customer Messages";
}

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><i class="fas fa-comments"></i> Customer Messages</h2>
            </div>

            @if (!Model.Any())
            {
                <div class="alert alert-info">
                    <i class="fas fa-info-circle"></i> No customer messages yet.
                </div>
            }
            else
            {
                <div class="card">
                    <div class="card-header">
                        <h5>Active Conversations</h5>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-striped table-hover mb-0">
                                <thead class="table-dark">
                                    <tr>
                                        <th>Customer</th>
                                        <th>Subject</th>
                                        <th>Last Message</th>
                                        <th>Messages</th>
                                        <th>Status</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var thread in Model.OrderByDescending(t => t.LastMessageAt))
                                    {
                                        <tr class="@(thread.HasUnreadMessages ? "table-warning" : "")">
                                            <td>
                                                <strong>@thread.CustomerName</strong>
                                                <br><small class="text-muted">Customer conversation</small>
                                            </td>
                                            <td>
                                                <span class="text-muted">Latest activity</span>
                                            </td>
                                            <td>
                                                @thread.LastMessageAt.ToString("MMM dd, HH:mm")
                                                <br><small class="text-muted">Started: @thread.StartedAt.ToString("MMM dd")</small>
                                            </td>
                                            <td>
                                                <span class="badge bg-primary">@thread.MessageCount</span>
                                                @if (thread.HasUnreadMessages)
                                                {
                                                    <span class="badge bg-warning ms-1">Unread</span>
                                                }
                                            </td>
                                            <td>
                                                @if (thread.RequiresResponse)
                                                {
                                                    <span class="badge bg-danger">Needs Response</span>
                                                }
                                                else
                                                {
                                                    <span class="badge bg-success">Up to date</span>
                                                }
                                            </td>
                                            <td>
                                                <a href="@Url.Action("Customer", new { id = thread.CustomerId })" class="btn btn-sm btn-primary">
                                                    <i class="fas fa-comments"></i> View Conversation
                                                </a>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</div>