@extends('layouts.app')

@section('title', 'In-app Messaging | Institution Axis')

@section('sidebar-menu')
    @include('schooladmin.partials.sidebar')
@endsection

@section('breadcrumb')
    <span><a href="{{ route('schooladmin.dashboard') }}">Home</a></span> / <span><a href="{{ route('schooladmin.communication.index') }}">Communication</a></span> / <span class="active-page">Messages</span>
@endsection

@section('content')
<div class="row g-4">
    <div class="col-lg-4">
        <div class="card shadow-sm border-0 rounded-4 h-100 min-vh-75">
            <div class="card-header bg-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="fas fa-comments me-2 text-primary"></i>Conversations</h5>
            </div>
            <div class="card-body p-0 overflow-auto">
                <div class="list-group list-group-flush">
                    @forelse($conversations as $conv)
                    <a href="{{ route('schooladmin.communication.messages.chat', $conv->other_user_id) }}" class="list-group-item list-group-item-action py-3 border-0 border-bottom">
                        <div class="d-flex align-items-center">
                            <img src="https://ui-avatars.com/api/?name={{ urlencode($users[$conv->other_user_id]->name) }}&background=random" class="rounded-circle me-3" width="45" height="45">
                            <div class="flex-grow-1 overflow-hidden">
                                <h6 class="mb-0 text-truncate">{{ $users[$conv->other_user_id]->name }}</h6>
                                <small class="text-muted text-capitalize">{{ $users[$conv->other_user_id]->role }}</small>
                            </div>
                            <div class="text-end ms-2">
                                <small class="text-muted d-block" style="font-size: 0.75rem;">{{ \Carbon\Carbon::parse($conv->last_message_time)->diffForHumans() }}</small>
                            </div>
                        </div>
                    </a>
                    @empty
                    <div class="text-center py-5">
                        <i class="fas fa-comment-slash fs-2 text-muted mb-3 d-block"></i>
                        <p class="text-muted px-4">No conversations found. Start by sending a notification.</p>
                    </div>
                    @endforelse
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-8">
        <div class="card shadow-sm border-0 rounded-4 h-100 min-vh-75 d-flex justify-content-center align-items-center">
            <div class="text-center p-5">
                <div class="mb-4">
                    <img src="https://institution-axis.com/assets/img/messaging-illustration.svg" class="img-fluid" style="max-width: 250px; opacity: 0.2;" onerror="this.src='https://ui-avatars.com/api/?name=Chat&size=200&background=f8f9fa&color=dee2e6'">
                </div>
                <h4>Select a Conversation</h4>
                <p class="text-muted">Pick a contact from the left list to start messaging.</p>
            </div>
        </div>
    </div>
</div>
@endsection

@section('custom-styles')
<style>
    .min-vh-75 { min-height: 75vh; }
    .list-group-item { transition: background 0.2s; border-left: 3px solid transparent !important; }
    .list-group-item:hover { background-color: #f8f9fa; }
    .list-group-item.active { background-color: #fef4f3; border-left-color: #e54027 !important; color: inherit; }
</style>
@endsection
