<?php

namespace App\Listeners;

use App\Events\NewNote;
use App\Models\ParentAccount;
use App\Notifications\StudentNoteNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\ParentNotification;

class SendNoteNotification
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  NewNote  $event
     * @return void
     */
    public function handle(NewNote $event)
    {
        $student_id = $event->student_id;
        $parentsAccounts = ParentAccount::getParentsAccountsByStudent($student_id);
        $parentsAccounts->each(function ($parent) {
            ParentNotification::create([
                'parent_account_id' => $parent->id,
                'message' => trans('general.note_notification_message',['student' => $parent->student_name]),
                'title' => trans('general.note_title'),
                'payload' => '{"page": "notes", "student":"'.$parent->student_id.'"}',
            ]);
            $parent->notify(new StudentNoteNotification($parent->student_name));
        });
    }
}
