<?php

namespace Modules\User\Controllers\Store;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\User\Requests\Store\ProfileRequest;
use Modules\User\Services\UserService;

class ProfileController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/store/profile';
    /** View folder */
    private $viewDirectory= 'store.profile.';
    //
    private UserService $userService;

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

    /**
     * Display a listing of the users.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $head= __('profile.head');
        $action = route('profile.store');
        $user = Auth::user();
        return view($this->viewDirectory.'form', get_defined_vars());
    }



    /**
     * Store a newly created User in storage.
     * @param ProfileRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(ProfileRequest $request) :JsonResponse
    {
        $user = Auth::user();
        $request->merge(['store_id'=>$user->store_id]);
        $this->userService->save($request, $user);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('profile.index'),
        ];
        return ResponseService::response($response);
    }




}
