<?php
namespace App\Http\Controllers;use App\Models\UserSetting;use App\Services\ProfileAuditLogger;use Illuminate\Http\{RedirectResponse,Request};use Illuminate\Validation\Rule;use Illuminate\View\View;
class SettingsController extends Controller{private function setting(Request$r):UserSetting{return$r->user()->setting()->firstOrCreate([],UserSetting::defaults());}public function show(Request$r):View{$setting=$this->setting($r);$projects=$r->user()->projects()->active()->orderBy('name')->get();return view('settings.show',compact('setting','projects'));}public function appearance(Request$r,ProfileAuditLogger$a):RedirectResponse{$data=$r->validate(['locale'=>['required',Rule::in(['tr','en'])],'theme'=>['required',Rule::in(['light','dark','system'])],'reduced_motion'=>['nullable','boolean']]);$s=$this->setting($r);$before=$s->only(['theme','reduced_motion']);$s->update(['theme'=>$data['theme'],'reduced_motion'=>$r->boolean('reduced_motion')]);$r->user()->forceFill(['locale'=>$data['locale']])->save();$r->session()->put('locale',$data['locale']);$a->record('settings_appearance_updated',$r->user(),$before,$s->only(['theme','reduced_motion']));return back()->with('status',__('settings.saved'));}public function regional(Request$r,ProfileAuditLogger$a):RedirectResponse{$data=$r->validate(['timezone'=>['required',Rule::in(\DateTimeZone::listIdentifiers())],'date_format'=>['required',Rule::in(['DD.MM.YYYY','YYYY-MM-DD','MM/DD/YYYY'])],'time_format'=>['required',Rule::in(['24','12'])]]);return$this->save($r,$a,'settings_regional_updated',$data);}public function dashboard(Request$r,ProfileAuditLogger$a):RedirectResponse{$data=$r->validate(['default_report_range'=>['required',Rule::in(['7_days','30_days','90_days','this_month'])],'default_project'=>['nullable','string'],'per_page'=>['required',Rule::in([10,25,50,100])]]);$project=null;if($r->filled('default_project'))$project=$r->user()->projects()->active()->where('ulid',$r->input('default_project'))->firstOrFail();return$this->save($r,$a,'settings_dashboard_updated',['default_report_range'=>$data['default_report_range'],'default_project_id'=>$project?->id,'per_page'=>(int)$data['per_page']]);}public function notifications(Request$r,ProfileAuditLogger$a):RedirectResponse{$r->validate(['in_app_notifications'=>['nullable','boolean']]);return$this->save($r,$a,'settings_notifications_updated',['in_app_notifications'=>$r->boolean('in_app_notifications')]);}public function reset(Request$r,ProfileAuditLogger$a):RedirectResponse{$s=$this->setting($r);$before=$s->toArray();$s->update(UserSetting::defaults());$r->user()->forceFill(['locale'=>'tr'])->save();$r->session()->put('locale','tr');$a->record('settings_reset',$r->user(),$before,$s->fresh()->toArray());return back()->with('status',__('settings.reset_done'));}private function save(Request$r,ProfileAuditLogger$a,string$action,array$data):RedirectResponse{$s=$this->setting($r);$before=$s->only(array_keys($data));$s->update($data);$a->record($action,$r->user(),$before,$s->only(array_keys($data)));return back()->with('status',__('settings.saved'));}}
