<?php

namespace App\Listeners;

use App\Events\NewExamProgram;
use App\Models\ParentAccount;
use App\Notifications\StudentExamProgramNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\ParentNotification;

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

    /**
     * Handle the event.
     *
     * @param  NewExamProgram  $event
     * @return void
     */
    public function handle(NewExamProgram $event)
    {
        $division_id = $event->division_id;
        $examProgram = $event->examProgram;

        $parentsAccounts = ParentAccount::getParentsAccountsByDivision($division_id);

        $parentsAccounts->each(function ($parent) use ($examProgram) {
            ParentNotification::create([
                'parent_account_id' => $parent->id,
                'message' => trans('general.examProgram_notification_message',['examProgram' => $examProgram->title]),
                'title' => trans('general.examProgram_title'),
                'payload' => '{"page": "examProgram", "student":"'.$parent->student_id.'"}',
            ]);

            $parent->notify(new StudentExamProgramNotification($examProgram, $parent->student_name));
        });
    }
}
