<?php

namespace App\Domain\Notification\Listeners;

use App\Domain\Notification\Services\PushNotificationService;
use App\Domain\Shared\Events\ServiceRequestResolved;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendServiceRequestResolvedPush implements ShouldQueue
{
    public function __construct(private PushNotificationService $pushService) {}

    public function handle(ServiceRequestResolved $event): void
    {
        $request = $event->serviceRequest;
        $user = User::find($request->user_id);

        if (!$user) {
            return;
        }

        $typeLabels = [
            'freeze' => 'تجميد',
            'unfreeze' => 'إلغاء تجميد',
            'transfer' => 'نقل',
            'cancellation' => 'إلغاء اشتراك',
            'other' => 'طلب',
        ];

        $typeLabel = $typeLabels[$request->type] ?? 'طلب';
        $statusLabel = $request->status === 'approved' ? 'تمت الموافقة' : 'تم الرفض';

        $this->pushService->sendToUser(
            $user,
            "نتيجة طلب {$typeLabel}",
            "{$statusLabel} على طلب {$typeLabel} الخاص بك",
            [
                'type' => 'service_request_resolved',
                'route' => '/service-requests',
                'request_uuid' => $request->uuid,
                'request_type' => $request->type,
                'request_status' => $request->status,
            ],
            'service_request.resolved'
        );
    }
}
