<?php

namespace Modules\User\Controllers\Employee;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Country\Services\CountryService;
use Modules\User\Requests\Employee\ProfileRequest;
use Modules\User\Services\UserService;

class ProfileController extends Controller
{

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

    public function __construct(
        private UserService $userService,
        private CountryService $countryService,
    ) {}

    /**
     * Display a listing of the members.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $id = auth()->user()->id;
        $employee = $this->userService->getItem($id);
        $countries = $this->countryService->get();
        $action= route($this->actionDirectory.'store');
        $head= __('profile.edit');

        return view($this->viewDirectory.'index', get_defined_vars());
    }
    public function store(ProfileRequest $request) :JsonResponse
    {
        $id = auth()->user()->id;
        $employee = $this->userService->checkItem($id);
        if (!$employee)
        {
            abort(404);
        }

        $this->userService->update($request->validated(),$employee->id);


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