<?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\ProviderExport;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Store\Services\StoreService;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Models\User;
use Modules\User\Requests\Admin\ProviderRequest;
use Modules\User\Services\AddressService;
use Modules\User\Services\ModuleService;
use Modules\User\Services\UserService;

class ProviderController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/providers';
    /** View folder */
    private $viewDirectory= 'admin.providers.';
    //
    private UserService $userService;
    private StoreService $storeService;

    public function __construct(UserService $userService ,StoreService $storeService)
    {
        $this->userService = $userService;
        $this->storeService = $storeService;
    }

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

    /**
     * return data of the providers.
     *
     * @param Request $request
     * @return mixed
     */
    public function grid(Request $request) :mixed
    {
        $request->merge(['roles'=>UserRoleEnum::stores()]);
        if (!$request->export)
        {
            $request->merge(['paginate'=>true]);
        }
        $providers= $this->userService->get($request);
        if ($request->export)
        {
            return Excel::download(new ProviderExport($providers), 'Providers.xlsx');
        }
        return view($this->viewDirectory.'grid',compact('providers'));
    }

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

    /**
     * Store a newly created User in storage.
     * @param ProviderRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(ProviderRequest $request) :JsonResponse
    {
        $this->userService->save($request, null);

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

    /**
     * Show the form for editing the specified User.
     *
     * @param  User  $user
     * @return View
     */
    public function edit(User $provider) :View
    {
        $action= route('providers.update', $provider->id);
        $provider= $this->userService->getItem($provider);
        $head= __('providers.edit');
        $roles = UserRoleEnum::stores();
        $stores = $this->storeService->get(request());
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified User in storage.
     * @param ProviderRequest $request
     * @param User $user
     * @return RedirectResponse|Redirector
     */
    public function update(ProviderRequest $request, User $user) :JsonResponse
    {
        $this->userService->save($request, $user);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('providers.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 providers 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 providers 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);
    }
}
