<?php

namespace Modules\User\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\User\Enums\UserRoleEnum;
use Modules\User\Services\ModuleService;
use Modules\User\Services\UserService;
use Modules\Currency\Services\CurrencyService;
use Modules\User\Requests\Admin\MemberRequest;

class MemberController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/members';
    /** View folder */
    private $viewDirectory= 'admin.members.';
    //

    public function __construct(
        private UserService $userService,
        private ModuleService $moduleService,
    )
    {}

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

    /**
     * return data of the members.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true,'roles'=>UserRoleEnum::users()]);
        $members= $this->userService->get($request->all());

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

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

    /**
     * Store a newly created User in storage.
     * @param MemberRequest $request
     * @return JsonResponse
     */
    public function store(MemberRequest $request) :JsonResponse
    {
        $this->userService->create($request->validated());

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

    /**
     * Show the form for editing the specified User.
     *
     * @param  string $code
     * @return View
     */
    public function edit(string $code) :View
    {
        $user = $this->userService->getItemByCode($code);
        if (!$user)
        {
            abort(404);
        }
        $action= route('members.update', $code);
        $head= __('members.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified User in storage.
     * @param MemberRequest $request
     * @param string $code
     * @return JsonResponse
     */
    public function update(MemberRequest $request, string $code) :JsonResponse
    {
        $user = $this->userService->getItemByCode($code);
        if (!$user)
        {
            abort(404);
        }
        // dd($request->validated());
        $this->userService->update($request->validated(), $user->id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('members.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified User from storage.
     * @param string $code
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(string $code) :JsonResponse
    {
        $this->userService->destroy($code);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('members.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several members by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $this->userService->destroyAll([
            'ids' => $request->ids,
            'status' => 1
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('members.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore the specified User from storage.
     *
     * @param string $code
     * @return JsonResponse
     */
    public function restore(string $code) :JsonResponse
    {
        $this->userService->destroy($code);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('members.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several members by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $this->userService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('members.index'),
        ];
        return ResponseService::response($response);
    }
}
