<?php

namespace Modules\User\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use App\Exports\MemberExport;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Models\User;
use Modules\User\Requests\Admin\MemberRequest;
use Modules\User\Services\AddressService;
use Modules\User\Services\ModuleService;
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.';
    //
    private UserService $userService;
    private ModuleService $moduleService;
    private AddressService $addressService;

    public function __construct(UserService $userService , ModuleService $moduleService,AddressService $addressService)
    {
        $this->userService = $userService;
        $this->moduleService = $moduleService;
        $this->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(['roles'=>UserRoleEnum::users()]);
        if (!$request->export)
        {
            $request->merge(['paginate'=>true]);
        }
        $members= $this->userService->get($request);
        if ($request->export)
        {
            return Excel::download(new MemberExport($members), 'Members.xlsx');
        }
        return view($this->viewDirectory.'grid',compact('members'));
    }

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

    /**
     * Store a newly created User in storage.
     * @param MemberRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(MemberRequest $request) :JsonResponse
    {
        $request->merge(['role'=>UserRoleEnum::USER->value]);
        $user= $this->userService->save($request, null);

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

    public function show(User $member) :View
    {
        $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() );
    }
    /**
     * Show the form for editing the specified User.
     *
     * @param  User  $user
     * @return View
     */
    public function edit(User $member) :View
    {
        $action= route('members.update', $member->id);
        $member= $this->userService->getItem($member);
        $head= __('members.edit');
        $roles = UserRoleEnum::users();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified User in storage.
     * @param MemberRequest $request
     * @param User $user
     * @return RedirectResponse|Redirector
     */
    public function update(MemberRequest $request, User $user) :JsonResponse
    {
        $request->merge(['role'=>UserRoleEnum::USER->value]);
        $this->userService->save($request, $user);
        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 Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->userService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

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

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

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