<?php

namespace App\Domain\Feeds\Observers;
use App\Domain\Feeds\Models\Feed;
use App\Notifications\RemoveFeed;
use App\Domain\Feeds\Enums\StatusEnum;
use App\Notifications\BannedMessageFeed;

class NovaFeedObserver
{

    public function updating(Feed $feed)
    {
       $oldStatus = $feed->getOriginal('status');
       $newStatus = $feed->status;

       if(($oldStatus === StatusEnum::PENDING() || $oldStatus === StatusEnum::EDITABLE()) && $newStatus === StatusEnum::BANNED()){
            $user = $feed->user;
            $message = [
                    'user_id' => $user->id,
                    'node_id' => $feed->id,
                    'text' => $feed->status_note,
                    'success' => false
                ];
            $user->notify(new BannedMessageFeed($message));
       }

       if(($oldStatus === StatusEnum::PENDING() || $oldStatus === StatusEnum::EDITABLE()) && $newStatus === StatusEnum::APPROVED()){
            $user = $feed->user;
            $message = [
                    'user_id' => $user->id,
                    'node_id' => $feed->id,
                    'text' => $feed->status_note,
                    'success' => true
                ];
            $user->notify(new BannedMessageFeed($message));
            $feed->status_note = '';
       }

    }

    public function deleting(Feed $feed)
    {

        if(!$feed->trashed()){
            $complaints = $feed->complaints;
            foreach ($complaints as $complaint) {
                $complaint->status = 'reviewed_bad';
                $complaint->moderator_checking_id = auth()->id();
                $complaint->save();
            }

            $user = $feed->user;
            $message = [
                'user_id' => $user->id,
                'node_id' => $feed->id,
            ];
            $user->notify(new RemoveFeed($message));
        }
    }
}
