<?php namespace App\Http\Controllers; use App\Models\{User,Employee,Intern,Role}; use Illuminate\Http\Request; use Illuminate\Support\Facades\{DB,Hash}; class WorkforceCrudController extends Controller {public function storeEmployee(Request $r){$d=$r->validate(['name'=>'required|string|max:150','email'=>'required|email|unique:users,email','employee_code'=>'required|string|unique:employees,employee_code','designation'=>'nullable|string|max:120','department_id'=>'nullable|exists:departments,id','office_id'=>'nullable|exists:offices,id','shift_id'=>'nullable|exists:shifts,id','joining_date'=>'nullable|date','employment_type'=>'required|string|max:40']);DB::transaction(function()use($d){$u=User::create(['name'=>$d['name'],'email'=>$d['email'],'password'=>Hash::make('ChangeMe@123'),'status'=>'active']);$u->roles()->attach(Role::where('slug','employee')->value('id'));$u->sendEmailVerificationNotification();Employee::create(['user_id'=>$u->id,'employee_code'=>$d['employee_code'],'designation'=>$d['designation']??null,'department_id'=>$d['department_id']??null,'office_id'=>$d['office_id']??null,'shift_id'=>$d['shift_id']??null,'joining_date'=>$d['joining_date']??null,'employment_type'=>$d['employment_type'],'status'=>'active']);});return back()->with('success','Employee created. Temporary password: ChangeMe@123');}public function updateEmployee(Request $r,Employee $employee){$d=$r->validate(['designation'=>'nullable|string|max:120','department_id'=>'nullable|exists:departments,id','office_id'=>'nullable|exists:offices,id','shift_id'=>'nullable|exists:shifts,id','status'=>'required|string|max:40']);$employee->update($d);return back()->with('success','Employee updated.');}public function destroyEmployee(Employee $employee){DB::transaction(fn()=> $employee->user->delete());return back()->with('success','Employee removed.');}public function storeIntern(Request $r){$d=$r->validate(['name'=>'required|string|max:150','email'=>'required|email|unique:users,email','intern_code'=>'required|string|unique:interns,intern_code','department_id'=>'nullable|exists:departments,id','office_id'=>'nullable|exists:offices,id','shift_id'=>'nullable|exists:shifts,id','mentor_user_id'=>'nullable|exists:users,id','start_date'=>'required|date','end_date'=>'nullable|date|after_or_equal:start_date','stipend_amount'=>'required|numeric|min:0','institution'=>'nullable|string|max:160','degree_program'=>'nullable|string|max:160']);DB::transaction(function()use($d){$u=User::create(['name'=>$d['name'],'email'=>$d['email'],'password'=>Hash::make('ChangeMe@123'),'status'=>'active']);$u->roles()->attach(Role::where('slug','intern')->value('id'));$u->sendEmailVerificationNotification();Intern::create(array_merge(array_diff_key($d,array_flip(['name','email'])),['user_id'=>$u->id,'status'=>'active','internship_type'=>'engineering']));});return back()->with('success','Intern created. Temporary password: ChangeMe@123');}public function updateIntern(Request $r,Intern $intern){$d=$r->validate(['department_id'=>'nullable|exists:departments,id','office_id'=>'nullable|exists:offices,id','shift_id'=>'nullable|exists:shifts,id','mentor_user_id'=>'nullable|exists:users,id','end_date'=>'nullable|date','stipend_amount'=>'required|numeric|min:0','status'=>'required|string|max:40']);$intern->update($d);return back()->with('success','Intern updated.');}public function destroyIntern(Intern $intern){DB::transaction(fn()=> $intern->user->delete());return back()->with('success','Intern removed.');}}