<?php

namespace Modules\User\Controllers\Canteen;

use App\Services\Response\ResponseService;
use Illuminate\Http\RedirectResponse;
//Check the request is valid
use App\Http\Controllers\Controller;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Auth;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
use Modules\Canteen\Services\CanteenService;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Requests\Canteen\LoginRequest;
use Modules\User\Requests\Canteen\ProfileRequest;
use Modules\User\Services\UserService;


class ProfileController extends Controller
{
    private $viewDirectory = 'canteen.profile.';

    public function __construct(
        private CanteenService $canteenService,
        private UserService $userService
    ){}

    /**
     * Show the application dashboard.
     * @return View
     */
    public function profile()
    {
        $head = __('profile.head');
        $action = route('canteen.profile.edit');
        $canteen_id = Auth::user()->canteen_id;
        $canteen = $this->canteenService->getItem($canteen_id);
        $user = $this->userService->canteenUser($canteen_id);
        return view($this->viewDirectory.'index',get_defined_vars());
    }

    public function editProfile(ProfileRequest $request)
    {
        $canteen_id = Auth::user()->canteen_id;
        $user = $this->userService->canteenUser($canteen_id);
        $this->userService->update($request->validated(),$user->id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteen.profile.index'),
        ];
        return ResponseService::response($response);
    }
}
