<?php

declare(strict_types=1);

namespace Seka\Vzdelavanie\Listeners\Kniznica;

use Illuminate\Contracts\Queue\ShouldQueue;
use Seka\Vzdelavanie\Models\Kniznica\Ucebnica;
use Seka\Vzdelavanie\Models\Popularita\Popularita;
use Seka\Vzdelavanie\Events\Kniznica\Frontend\UcebnyMaterialOpened;

/**
 * Class UcebnyMaterialCreatePopularityRecord
 *
 * @package Seka\Vzdelavanie\Listeners\Kniznica
 */
final class UcebnyMaterialCreatePopularityRecord implements ShouldQueue
{
    /**
     * UcebnyMaterialCreatePopularityRecord constructor.
     */
    public function __construct()
    {

    }

    /**
     * @param \Seka\Vzdelavanie\Events\Kniznica\Frontend\UcebnyMaterialOpened $event
     */
    public function handle(UcebnyMaterialOpened $event): void
    {
        $popularita = new Popularita();
        $popularita->pouzivatel()->associate($event->pouzivatel->getAuthIdentifier());

        /** @var Ucebnica $ucebnica */
        $ucebnica = Ucebnica::query()->with('popularita')->findOrFail($event->ucebnica->id);
        $ucebnica->popularita()->save($popularita);
    }
}
