<?php

namespace App\Listeners;

use App\Events\NewAnnouncement;
use App\Models\ParentAccount;
use App\Models\ParentNotification;
use App\Models\Parents;
use App\Models\Stage;
use App\Notifications\AnnouncementNotification;
use App\Notifications\ParentsNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

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

    /**
     * Handle the event.
     *
     * @param  NewAnnouncement  $event
     * @return void
     */
    public function handle(NewAnnouncement $event)
    {
        $stage_id = $event->satge_id;
        $announcement = $event->announcement;
        $stage = Stage::find($stage_id);

        $parentsAccounts = ParentAccount::getParentsAccountsByStage($stage_id);

        $parentsAccounts->each(function ($parent) use ($announcement)//, $stage
        {
            ParentNotification::create([
                'parent_account_id' => $parent->id,
                'message' => trans('general.announcement_notification_message'),//,['stage' => $stage->title]
                'title' => trans('general.announcement'),
                'payload' => '{"page": "announcements"}',
            ]);
            $parent->notify(new AnnouncementNotification($announcement));
        });
    }
}
