<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Models\Notification;
use App\Servicies\Notify;
use App\Models\User;
use Illuminate\Http\Request;

class NotificationController extends Controller
{


    public function index()
    {

        $notifications = Notification::all();

        return view('admin.notifications.index', compact('notifications'));
    } // end of index

    // destroy
    public function destroy($id)
    {
        $notification = Notification::find($id);

        if ($notification) {
            $notification->delete();
        }

        return redirect()->back();
    } // end of destroy


    // send notification
    public function sendNotification(Request $request)
    {
        $this->notificationRepository->sendNotification($request);

        return redirect()->back();
    } // end of sendNotification

    // getNotifications
    public function getNotifications(NotificationDataTable $dataTable)
    {

        // session put 
        session()->put('notification_type', 'notification');

        return $dataTable->render('dashboard.notifications.index');
    } // end of getNotifications

    // create 
    public function create()
    {

        $users = User::whereRelation('roles', 'name', 'user')->get();

        return view('admin.notifications.create', compact('users'));
    } // end of create

    // store
    public function store(Request $request)
    {
        // dd($request->all());

        $data = $request->all();

        $data['type'] = 'notification';

        // all_users
        if ($request->type == 'all_users') {

            $users = User::whereHas('roles', function ($query) {
                $query->where('name', 'user');
            })->get();
        } elseif ($request->type == 'users') {

            $users = User::whereIn('id', $request->user_ids)->get();
        }

        $message = new \stdClass();
        $message->message_ar = $request->message_ar;
        $message->message_en = $request->message_en;

        $title = new \stdClass();
        $title->title_ar = $request->title_ar;
        $title->title_en = $request->title_en;

        $msg_arr = [];

        if ($users) {

            foreach ($users as $user) {

                $arr = [
                    'title_ar' => $title->title_ar,
                    'title_en' => $title->title_en,
                    'message_ar' => $message->message_ar,
                    'message_en' => $message->message_en,
                    'user_id'    => $user->id,
                    'from_user_id' => auth()->user()->id,
                    'type'       => 'notification',
                    'image'      => $request->photo,
                    'created_at' => date('Y-m-d H:i:s'),
                ]; // end of arr

                array_push($msg_arr, $arr);
            } // end of foreach

            if (count($msg_arr) > 0) {
                foreach ($msg_arr as $msg_ar) {
                    Notification::create($msg_ar);
                }
            } // end of if

            Notify::NotifyMobile($title, $message, $request->type , $users, null);

        } else {
            return redirect()->back()->with('notification_delete_msg', 'هناك خطأ');
        } // end of if

        return redirect()->back()->with('notification_create_msg', 'تم الارسال بنجاح');
    } // end of store   
}
