<?php namespace App\Http\Controllers; use App\Models\{Project,Client,Department,User,ProjectMember}; use Illuminate\Http\Request; class ProjectController extends Controller {public function index(){return view('projects.index',['projects'=>Project::with('client')->withCount(['members','tasks'])->latest()->paginate(25),'clients'=>Client::where('status','active')->get(),'departments'=>Department::where('is_active',true)->get(),'managers'=>User::whereHas('roles',fn($q)=>$q->whereIn('slug',['founder','engineering_manager','team_lead']))->get()]);}public function show(Project $project){return view('projects.show',['project'=>$project->load(['client','members.user','tasks.assignee']),'users'=>User::where('status','active')->orderBy('name')->get()]);}public function store(Request $r){$d=$r->validate(['code'=>'required|string|max:50|unique:projects,code','name'=>'required|string|max:180','client_id'=>'nullable|exists:clients,id','manager_user_id'=>'nullable|exists:users,id','department_id'=>'nullable|exists:departments,id','technology_stack'=>'nullable|string','start_date'=>'nullable|date','end_date'=>'nullable|date','status'=>'required|string|max:40','priority'=>'required|string|max:40']);Project::create($d);return back()->with('success','Project created.');}public function update(Request $r,Project $project){$d=$r->validate(['name'=>'required|string|max:180','client_id'=>'nullable|exists:clients,id','manager_user_id'=>'nullable|exists:users,id','department_id'=>'nullable|exists:departments,id','technology_stack'=>'nullable|string','start_date'=>'nullable|date','end_date'=>'nullable|date','status'=>'required|string|max:40','priority'=>'required|string|max:40']);$project->update($d);return back()->with('success','Project updated.');}public function destroy(Project $project){$project->delete();return back()->with('success','Project deleted.');}public function member(Request $r,Project $project){$d=$r->validate(['user_id'=>'required|exists:users,id','role'=>'required|string|max:80','allocation_percent'=>'required|integer|min:1|max:100']);ProjectMember::updateOrCreate(['project_id'=>$project->id,'user_id'=>$d['user_id']],$d);return back()->with('success','Project member assigned.');}}