<?php

namespace Modules\Notification\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Notification\Models\Notification;
use Modules\Notification\Requests\Admin\NotificationRequest;
use Modules\Notification\Services\NotificationService;
use Modules\User\Services\UserService;

class NotificationController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/notifications';
    /** View folder */
    private $viewDirectory= 'admin.notifications.';
    //
    private NotificationService $notificationService;
    private UserService $userService;

    public function __construct(NotificationService $notificationService , UserService $userService)
    {
        $this->notificationService = $notificationService;
        $this->userService = $userService;
    }

    /**
     * Display a listing of the notifications.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('notifications.head');
        $users = $this->userService->get($request);
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the notifications.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $request->merge(['paginate'=> true]);
        $notifications= $this->notificationService->get($request);

        return view($this->viewDirectory.'grid',compact('notifications'));
    }

    /**
     * Show the form for creating a new Notification.
     *
     * @return View
     */
    public function create(Request $request) :View
    {
        $action= route('notifications.store');
        $head= __('notifications.new');
        $users = $this->userService->get($request);
        return view($this->viewDirectory.'form', get_defined_vars());
    }

    /**
     * Store a newly created Notification in storage.
     * @param NotificationRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(NotificationRequest $request) :JsonResponse
    {
        $request->merge(['status'=>'management']);
        $notification= $this->notificationService->save($request, null);

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('notifications.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified Notification.
     *
     * @param  Notification  $notification
     * @return View
     */
    public function edit(Notification $notification) :View
    {
        // dd($notification->id);
        $action= route('notifications.update', $notification->id);
        $data= $this->notificationService->getItem($notification);
        $head= __('notifications.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Notification in storage.
     * @param NotificationRequest $request
     * @param Notification $notification
     * @return RedirectResponse|Redirector
     */
    public function update(NotificationRequest $request, Notification $notification) :JsonResponse
    {
        $notification= $this->notificationService->save($request, $notification);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('notifications.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Notification from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->notificationService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several notifications by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->notificationService->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Notification from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->notificationService->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several notifications by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->notificationService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
