@extends('layouts.app')

@section('title', 'My Notifications')
@section('breadcrumb', 'Notifications')

@section('content')
<section class="py-4 py-md-5">
    <div class="container">
        <div class="d-flex flex-wrap justify-content-between align-items-center mb-3">
            <h4 class="mb-2 mb-md-0">My Notifications</h4>
            @if(auth()->user()->unreadNotifications()->count() > 0)
                <form action="{{ route('customer.notifications.read-all') }}" method="POST">
                    @csrf
                    <button type="submit" class="btn btn-outline-primary btn-sm">
                        <i class="bi bi-check2-all me-1"></i> Mark all as read
                    </button>
                </form>
            @endif
        </div>

        <div class="card shadow-sm">
            <div class="card-body p-0">
                @forelse($notifications as $notification)
                    @php
                        $isUnread = is_null($notification->read_at);
                        $url = data_get($notification->data, 'url', route('customer.notifications.index'));
                    @endphp
                    <form action="{{ route('customer.notifications.read', $notification->id) }}" method="POST" class="border-bottom">
                        @csrf
                        <button type="submit" class="w-100 text-start p-3 btn btn-link text-decoration-none text-dark">
                            <div class="d-flex justify-content-between align-items-start gap-3">
                                <div>
                                    <div class="fw-semibold">
                                        {{ data_get($notification->data, 'title', 'Notification') }}
                                        @if($isUnread)
                                            <span class="badge bg-primary ms-2">New</span>
                                        @endif
                                    </div>
                                    <div class="text-muted small mt-1">{{ data_get($notification->data, 'message') }}</div>
                                    <div class="small mt-2">
                                        <a href="{{ $url }}" class="text-primary">Open</a>
                                    </div>
                                </div>
                                <small class="text-muted">{{ $notification->created_at?->diffForHumans() }}</small>
                            </div>
                        </button>
                    </form>
                @empty
                    <div class="p-4 text-muted">No notifications found.</div>
                @endforelse
            </div>
        </div>

        <div class="mt-3">
            {{ $notifications->links() }}
        </div>
    </div>
</section>
@endsection
