@extends('layouts.app')

@section('title', 'Messages')

@section('content')
    <h1 class="text-2xl font-bold mb-6">Messages</h1>
    <div class="grid md:grid-cols-2 gap-6">
        <div>
            <h2 class="text-xl font-semibold mb-2">Compose</h2>
            <form action="{{ route('messages.store') }}" method="POST" class="space-y-4">
                @csrf
                <div>
                    <label class="block font-semibold">Recipient</label>
                    <select name="receiver_id" class="w-full border rounded p-2" required>
                        @foreach($students as $student)
                            <option value="{{ $student->user->id ?? '' }}">{{ $student->full_name }} ({{ $student->class->name ?? 'N/A' }})</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block font-semibold">Message</label>
                    <textarea name="message" rows="4" class="w-full border rounded p-2" required></textarea>
                </div>
                <button type="submit" class="btn-primary">Send</button>
            </form>
        </div>
        <div>
            <h2 class="text-xl font-semibold mb-2">Inbox</h2>
            @foreach($messages as $message)
                <div class="border rounded p-3 mb-2 {{ $message->is_read ? 'bg-white' : 'bg-yellow-50' }}">
                    <p><strong>From:</strong> {{ $message->sender->name ?? 'Unknown' }}</p>
                    <p><strong>Message:</strong> {{ $message->message }}</p>
                    <p class="text-sm text-gray-500">{{ $message->created_at->diffForHumans() }}</p>
                    @if(!$message->is_read)
                        <a href="/messages/{{ $message->id }}/read" class="text-primary hover:underline">Mark as read</a>
                    @endif
                </div>
            @endforeach
        </div>
    </div>
@endsection