<?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\AccountStatusEnum;
use Modules\User\Enums\UserGenderEnum;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Requests\Admin\MemberRequest;
use Modules\User\Services\AddressService;
use Modules\User\Services\UserService;

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 AddressService $addressService,
    )
    {}

    /**
     * Display a listing of the members.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('members.head');
        $roles = UserRoleEnum::users();
        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');
        $roles = UserRoleEnum::users();
        $gender = UserGenderEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    public function show(int $id) :View
    {
        $member = $this->userService->checkItem($id);
        if (!$member){abort(404);}
        $request = request();
        $request->merge(['user_id'=>$member->id,'user_status'=>AccountStatusEnum::USER->value]);
        $addresses= $this->addressService->get($request);
        $head= __('members.addresses');
        return view( $this->viewDirectory.'addresses', 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  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $member = $this->userService->getItem($id);
        if (!$member)
        {
            abort(404);
        }
        $action= route('members.update', $member->id);
        $head= __('members.edit');
        $roles = UserRoleEnum::users();
        $gender = UserGenderEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified User in storage.
     * @param MemberRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(MemberRequest $request, int $id) :JsonResponse
    {
        $member = $this->userService->checkItem($id);
        if (!$member)
        {
            abort(404);
        }
        $this->userService->update($request->validated(), $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 int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->userService->destroy($id);
        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 int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->userService->destroy($id);
        $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);
    }
}
