<?php

namespace App\Listeners;

use App\Events\ObjectActionCreate;
use App\Models\Enums\ActionObjectType;
use App\Models\Enums\ObjectAction;
use App\Services\Notification\ActionEmail\ActionEmailService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class SendActionEmailNotification implements ShouldQueue
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(ObjectActionCreate $event): void
    {
        (new ActionEmailService($event->action))->send();
    }

    public function shouldQueue(ObjectActionCreate $event): bool
    {
        $actionObjectType = ActionObjectType::tryFrom($event->action->object_type);
        $objectAction = ObjectAction::tryFrom($event->action->action);
        if (! $actionObjectType || !$objectAction) {
            return false;
        }

        return in_array($objectAction->value, $event->notificationSetting[$actionObjectType->value]['email'] ?? []);
    }
}