@extends('layouts.app')

@section('title', 'Notification Center')

@section('content')
<main class="nxl-container">
    <div class="nxl-content">
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h5 class="mb-1">Notifications</h5>
                <div class="text-muted">Updates, delivery status, and retry history</div>
            </div>
            <div class="d-flex gap-2">
                <a class="btn btn-light" href="{{ route('notification-preferences.edit') }}">Preferences</a>
                <form method="POST" action="{{ route('notifications.read-all') }}">
                    @csrf
                    <button class="btn btn-primary">Mark all as read</button>
                </form>
            </div>
        </div>

        <div class="main-content">
            @if(session('success')) <div class="alert alert-success">{{ session('success') }}</div> @endif
            <div class="card mb-3"><x-server-list-toolbar placeholder="Search notifications and delivery history" :filters="['date_from'=>['label'=>'From','type'=>'date'],'date_to'=>['label'=>'To','type'=>'date']]" /></div>
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between">
                    <h5 class="card-title mb-0">Notification Center</h5>
                    <div>
                        <a class="btn btn-sm {{ request('filter') ? 'btn-light' : 'btn-primary' }}" href="{{ route('notifications.index') }}">All</a>
                        <a class="btn btn-sm {{ request('filter') === 'unread' ? 'btn-primary' : 'btn-light' }}" href="{{ route('notifications.index', ['filter' => 'unread']) }}">
                            Unread ({{ auth()->user()->unreadNotifications()->count() }})
                        </a>
                    </div>
                </div>
                <div class="list-group list-group-flush">
                    @forelse($notifications as $notification)
                        <a href="{{ route('notifications.open', $notification->id) }}" class="list-group-item list-group-item-action py-3 {{ $notification->read_at ? '' : 'bg-light' }}">
                            <div class="d-flex justify-content-between gap-3">
                                <div>
                                    <div class="fw-semibold">{{ $notification->data['title'] ?? 'CRM Notification' }}</div>
                                    <div class="text-muted">{{ $notification->data['message'] ?? '' }}</div>
                                    @if(!empty($notification->data['entity_type']))
                                        <span class="badge bg-soft-primary text-primary mt-2">{{ $notification->data['entity_type'] }} #{{ $notification->data['entity_id'] ?? '' }}</span>
                                    @endif
                                </div>
                                <div class="text-nowrap text-muted">{{ $notification->created_at->diffForHumans() }}</div>
                            </div>
                        </a>
                    @empty
                        <div class="p-4 text-center text-muted">No notifications found.</div>
                    @endforelse
                </div>
                @if($notifications->hasPages()) <div class="card-footer">{{ $notifications->links() }}</div> @endif
            </div>

            <div class="card">
                <div class="card-header"><h5 class="card-title mb-0">Delivery & Retry History</h5></div>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead><tr><th>Event</th><th>Channel</th><th>Status</th><th>Attempt</th><th>Time</th><th>Error</th><th></th></tr></thead>
                        <tbody>
                        @forelse($attempts as $attempt)
                            <tr>
                                <td>{{ ucwords(str_replace('_', ' ', $attempt->event)) }}</td>
                                <td>{{ ucfirst($attempt->channel) }}</td>
                                <td><span class="badge {{ $attempt->status === 'sent' ? 'bg-soft-success text-success' : ($attempt->status === 'failed' ? 'bg-soft-danger text-danger' : 'bg-soft-warning text-warning') }}">{{ ucfirst($attempt->status) }}</span></td>
                                <td>{{ $attempt->attempt }}</td>
                                <td>{{ optional($attempt->attempted_at)->format('Y-m-d H:i:s') }}</td>
                                <td class="text-danger" style="max-width:340px">{{ $attempt->error }}</td>
                                <td>
                                    @if($attempt->status === 'failed')
                                        <form method="POST" action="{{ route('notification-attempts.retry', $attempt) }}">@csrf<button class="btn btn-sm btn-outline-primary">Retry</button></form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="7" class="text-center text-muted py-4">No delivery attempts recorded yet.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
                @if($attempts->hasPages()) <div class="card-footer">{{ $attempts->links() }}</div> @endif
            </div>
        </div>
    </div>
</main>
@endsection
