<?php

namespace App\Listeners;

use App\Events\NewWorksheet;
use App\Models\ParentAccount;
use App\Notifications\StudentWorksheetNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\ParentNotification;

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

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

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

        $parentsAccounts->each(function ($parent) {

            
            ParentNotification::create([
                'parent_account_id' => $parent->id,
                'message' => trans('general.worksheet_notification_message',['student' => $parent->student_name]),
                'title' => trans('general.worksheet_title'),
                'payload' => '{"page": "worksheets", "student":"'.$parent->student_id.'"}',
            ]);
            
            $parent->notify(new StudentWorksheetNotification($parent->student_name));
        });
    }
}
