<?php

namespace App\Http\Controllers\Student;

use App\Http\Controllers\Controller;
use App\Http\Requests\Student\EmailUpdateRequest;
use App\Http\Requests\Student\PasswordUpdateRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\View\View;

class SettingController extends Controller
{
    public function index(Request $request): View
    {
        $user = $request->user();

        return view('student.pages.myprofile.setting', compact('user'));
    }

    public function updateEmail(EmailUpdateRequest $request): RedirectResponse
    {
        $request->user()->update([
            'email' => $request->email,
        ]);

        return redirect()->route('student.profile.account')->with('success', 'Email updated successfully!');
    }

    public function updatePassword(PasswordUpdateRequest $request): RedirectResponse
    {
        $request->user()->update([
            'password' => Hash::make($request->password),
        ]);

        return redirect()->route('student.profile.account')->with('success', 'Password updated successfuly!');
    }
}
