<?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 App\Http\Controllers\Controller;
use Modules\Notification\Enums\ManagementNotificationStatusEnum;
use Modules\Notification\Enums\NotificationStatusEnum;
use Modules\Notification\Requests\Admin\NotificationRequest;
use Modules\Notification\Services\ManagementNotificationService;
use Modules\Notification\Services\NotificationService;
use Modules\User\Enums\UserRoleEnum;
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.';
    //

    public function __construct(
        private NotificationService $notificationService,
        private ManagementNotificationService $managementNotificationService,
        private UserService $userService,
    ) {
    }

    /**
     * Display a listing of the notifications.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request): View
    {
        $title = __('notifications.head');
        $statuses = NotificationStatusEnum::values();
        $users = $this->userService->get();
        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->managementNotificationService->get($request->all());

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

    public function statusNotification(Request $request)
    {
        $title = __('notifications.statuses.' . $request->type);
        if ($request->type == NotificationStatusEnum::PARENT->value) {
            $role = UserRoleEnum::PARENT->value;
        } else {
            $role = UserRoleEnum::CANTEEN->value;
        }
        $users = $this->userService->get(['role' => $role]);
        return view($this->viewDirectory . 'users', get_defined_vars());
    }

    /**
     * Show the form for creating a new Canteen.
     *
     * @return View
     */
    public function create(): View
    {
        $action = route('notifications.store');
        $head = __('notifications.new');
        $statuses = NotificationStatusEnum::values();
        $all = NotificationStatusEnum::ALL->value;
        return view($this->viewDirectory . 'form', get_defined_vars());
    }

    /**
     * Store a newly created Canteen in storage.
     * @param NotificationRequest $request
     * @return JsonResponse
     */
    public function store(NotificationRequest $request): JsonResponse
    {


        $data = $request->validated();
        $data['status'] = ManagementNotificationStatusEnum::MANAGEMENT->value;
        $this->notificationService->create($data);
        $this->managementNotificationService->create($request->validated());

        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 Canteen.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id): View
    {
        $notification = $this->managementNotificationService->getItem($id);
        if (!$notification) {
            abort(404);
        }
        $head = __('notifications.edit');
        $all = NotificationStatusEnum::ALL->value;

        return view($this->viewDirectory . 'show', get_defined_vars());
    }


}
