<?php
namespace Modules\VisitorMessage\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Modules\VisitorMessage\Enums\VisitorMessageReplyEnum;
use Modules\VisitorMessage\Requests\GetVisitorMessagesRequest;
use Modules\VisitorMessage\Requests\VisitorMessageRequest;
use Modules\VisitorMessage\Services\VisitorMessageService;
use Modules\VisitorMessage\Models\VisitorMessage;

class VisitorMessageController extends Controller
{
    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath = '/admin/visitorMessages';
    /** View folder */
    private $viewDirectory = 'admin.visitor_messages.';

    private $visitorMessageService;

    public function __construct(VisitorMessageService $visitorMessageService )
    {
        $this->visitorMessageService = $visitorMessageService;
    }


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

    public function grid(Request $request)
    {
        $request->merge(['paginate'=>true]);
        $visitorMessages = $this->visitorMessageService->get($request);
        if($request->export) {
        }
        return view($this->viewDirectory.'grid',compact('visitorMessages'));
    }

    public function create() :View
    {
        $action= route('visitorMessages.store');
        $head = metaFields('visitorMessages', 'add_new', getCurrentLocale()) ?? __('visitorMessages.edit');

        return view( $this->viewDirectory.'form', compact('action', 'head') );
    }

    public function store(VisitorMessageRequest $request)
    {
        $this->visitorMessageService->reply($request,null);

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

    public function edit(VisitorMessage $visitorMessage): View
    {
        $action= route('visitorMessages.update', $visitorMessage->id);
        $head = metaFields('visitorMessages', 'edit', getCurrentLocale()) ?? __('visitorMessages.edit');
        $statuses = VisitorMessageReplyEnum::values();
        $replies = $this->visitorMessageService->replies($visitorMessage);
        return view( $this->viewDirectory.'form', compact('visitorMessage', 'action', 'head' ,'statuses','replies') );
    }

    public function update(VisitorMessageRequest $request, VisitorMessage $visitorMessage)
    {
        $visitorMessage = $this->visitorMessageService->reply($request,$visitorMessage);

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

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

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

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

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