<?php

declare(strict_types=1);

namespace App\Framework\Notification\Jobs;

use App\Framework\Notification\Notification;
use App\Framework\Notification\NotificationDispatcher;

/**
 * Queue job for asynchronous notification sending
 */
final readonly class SendNotificationJob
{
    public function __construct(
        public Notification $notification
    ) {
    }

    /**
     * Execute the job
     *
     * @param NotificationDispatcher $dispatcher Injected by container
     * @return array Result metadata
     */
    public function handle(NotificationDispatcher $dispatcher): array
    {
        $result = $dispatcher->sendNow($this->notification);

        return [
            'notification_id' => $this->notification->id->toString(),
            'recipient_id' => $this->notification->recipient_id,
            'success' => $result->isSuccess(),
            'channels_succeeded' => count($result->getSuccessful()),
            'channels_failed' => count($result->getFailed()),
            'errors' => $result->getErrors(),
        ];
    }

    public function getType(): string
    {
        return 'notification.send';
    }
}
