<?php
namespace App\Http\Controllers;use App\Services\ProfileAuditLogger;use Illuminate\Auth\Events\Registered;use Illuminate\Http\{RedirectResponse,Request};use Illuminate\Support\Facades\{Auth,DB,Hash};use Illuminate\Validation\Rules\Password;use Illuminate\Validation\ValidationException;use Illuminate\View\View;
class ProfileController extends Controller{public function show(Request$r):View{$u=$r->user()->load('roles');$sessions=config('session.driver')==='database'?DB::table('sessions')->where('user_id',$u->id)->orderByDesc('last_activity')->get()->map(fn($s)=>['id'=>$s->id,'current'=>$s->id===$r->session()->getId(),'agent'=>mb_substr((string)$s->user_agent,0,100),'last_activity'=>date('d.m.Y H:i',$s->last_activity)]):collect();return view('profile.show',compact('u','sessions'));}public function update(Request$r,ProfileAuditLogger$a):RedirectResponse{$data=$r->validate(['name'=>['required','string','min:2','max:150','not_regex:/[\x00-\x1F\x7F]/']]);$before=$r->user()->name;$r->user()->update(['name'=>trim($data['name'])]);$a->record('profile_updated',$r->user(),['name'=>$before],['name'=>$r->user()->name]);return back()->with('status',__('profile.updated'));}public function email(Request$r,ProfileAuditLogger$a):RedirectResponse{$data=$r->validate(['email'=>['required','email:rfc','max:254','unique:users,email,'.$r->user()->id],'current_password'=>['required','current_password']]);$email=mb_strtolower(trim($data['email']));if($email===$r->user()->email)return back();$before=$r->user()->email;$r->user()->forceFill(['email'=>$email,'email_verified_at'=>null])->save();$r->user()->sendEmailVerificationNotification();$a->record('email_changed',$r->user(),['email'=>$a->mask($before)],['email'=>$a->mask($email)]);return back()->with('status',__('profile.email_updated'));}public function password(Request$r,ProfileAuditLogger$a):RedirectResponse{$data=$r->validate(['current_password'=>['required','current_password'],'password'=>['required','confirmed',Password::defaults()]]);if(Hash::check($data['password'],$r->user()->password))throw ValidationException::withMessages(['password'=>__('profile.same_password')]);$r->user()->forceFill(['password'=>Hash::make($data['password']),'remember_token'=>str()->random(60)])->save();$a->record('password_changed',$r->user());return back()->with('status',__('profile.password_updated'));}public function resend(Request$r,ProfileAuditLogger$a):RedirectResponse{if(!$r->user()->hasVerifiedEmail()){$r->user()->sendEmailVerificationNotification();$a->record('verification_resent',$r->user());}return back()->with('status',__('profile.verification_sent'));}public function logoutOthers(Request$r,ProfileAuditLogger$a):RedirectResponse{$r->validate(['current_password'=>['required','current_password']]);if(config('session.driver')==='database')DB::table('sessions')->where('user_id',$r->user()->id)->where('id','!=',$r->session()->getId())->delete();$a->record('other_sessions_logged_out',$r->user());return back()->with('status',__('profile.sessions_closed'));}}
