<?php

namespace App\View\Components;

use App\Models\NotificationDeliveryAttempt;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class ProfileNotificationHistory extends Component
{
    public $attempts;

    public function __construct(public string $entityType, public int $entityId)
    {
        $this->attempts = NotificationDeliveryAttempt::with('user')
            ->latest('attempted_at')
            ->limit(200)
            ->get()
            ->filter(fn ($attempt) =>
                strcasecmp((string) data_get($attempt->payload, 'entityType'), $entityType) === 0
                && (int) data_get($attempt->payload, 'entityId') === $entityId
            )
            ->take(25);
    }

    public function render(): View
    {
        return view('components.profile-notification-history');
    }
}
