<?php namespace Database\Seeders;
use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Hash; use App\Models\{User,Role,Permission,Department,Office,OfficeIpRule,Shift,Employee,Intern,Client,Project,ProjectMember,Task,LeaveType,ExpenseCategory,SalaryRevision,Announcement};
class DatabaseSeeder extends Seeder
{
 public function run():void
 {
  $roles=['founder'=>'Founder / Super Admin','operations'=>'Operations Admin','hr'=>'HR / People Admin','finance'=>'Finance / Accounts','engineering_manager'=>'Engineering Manager','team_lead'=>'Team Lead','employee'=>'Employee','intern'=>'Intern']; foreach($roles as $slug=>$name)Role::firstOrCreate(['slug'=>$slug],['name'=>$name]);
  foreach(['people.view','people.manage','interns.manage','attendance.review','projects.manage','tasks.manage','payroll.manage','expenses.approve','performance.manage','assets.manage','reports.view','settings.manage','audit.view'] as $p) Permission::firstOrCreate(['slug'=>$p],['name'=>ucwords(str_replace(['.','_'],' ',$p))]);
  $founderRole=Role::where('slug','founder')->first(); $founderRole->permissions()->sync(Permission::pluck('id')); $grant=function($role,$perms){Role::where('slug',$role)->first()->permissions()->sync(Permission::whereIn('slug',$perms)->pluck('id'));}; $grant('operations',['people.view','people.manage','interns.manage','attendance.review','projects.manage','tasks.manage','expenses.approve','performance.manage','assets.manage','reports.view','settings.manage','audit.view']); $grant('hr',['people.view','people.manage','interns.manage','attendance.review','performance.manage','assets.manage','reports.view']); $grant('finance',['payroll.manage','expenses.approve','reports.view']); $grant('engineering_manager',['projects.manage','tasks.manage','attendance.review','performance.manage','reports.view']); $grant('team_lead',['projects.manage','tasks.manage']);
  $office=Office::firstOrCreate(['code'=>'MTN-HQ'],['name'=>'SoftSuite HQ','address'=>'Multan, Pakistan','timezone'=>'Asia/Karachi']); OfficeIpRule::firstOrCreate(['office_id'=>$office->id,'cidr'=>'127.0.0.1'],['label'=>'Local development','is_active'=>true]);
  $shift=Shift::firstOrCreate(['name'=>'General Shift'],['start_time'=>'09:00','end_time'=>'18:00','late_grace_minutes'=>10,'is_active'=>true]);
  $eng=Department::firstOrCreate(['code'=>'ENG'],['name'=>'Engineering']); $hrd=Department::firstOrCreate(['code'=>'HR'],['name'=>'People & HR']); $fin=Department::firstOrCreate(['code'=>'FIN'],['name'=>'Finance']);
  $make=function($name,$email,$role){$u=User::firstOrCreate(['email'=>$email],['name'=>$name,'password'=>Hash::make('Password@123'),'status'=>'active','email_verified_at'=>now()]);$u->roles()->syncWithoutDetaching([Role::where('slug',$role)->value('id')]);return $u;};
  $founder=$make('Ahmad Shahzaib','founder@softsuite.local','founder'); $hr=$make('Sara HR','hr@softsuite.local','hr'); $finance=$make('Hamza Finance','finance@softsuite.local','finance'); $lead=$make('Usman Engineering Lead','lead@softsuite.local','engineering_manager'); $emp=$make('Ali Engineer','employee@softsuite.local','employee'); $internUser=$make('Ayesha Intern','intern@softsuite.local','intern');
  Employee::firstOrCreate(['user_id'=>$lead->id],['employee_code'=>'SST-0001','department_id'=>$eng->id,'designation'=>'Engineering Manager','office_id'=>$office->id,'shift_id'=>$shift->id,'joining_date'=>now()->subYears(2),'status'=>'active']);
  $employee=Employee::firstOrCreate(['user_id'=>$emp->id],['employee_code'=>'SST-0002','department_id'=>$eng->id,'designation'=>'Software Engineer','office_id'=>$office->id,'shift_id'=>$shift->id,'manager_user_id'=>$lead->id,'joining_date'=>now()->subMonths(8),'status'=>'active']);
  Intern::firstOrCreate(['user_id'=>$internUser->id],['intern_code'=>'INT-0001','department_id'=>$eng->id,'office_id'=>$office->id,'shift_id'=>$shift->id,'mentor_user_id'=>$lead->id,'institution'=>'Demo University','degree_program'=>'BS Computer Science','internship_type'=>'Engineering','start_date'=>now()->subMonth(),'end_date'=>now()->addMonths(2),'status'=>'active','learning_goals'=>'Laravel, clean architecture, testing','stipend_amount'=>25000]);
  $client=Client::firstOrCreate(['name'=>'Demo Client'],['company'=>'Demo Client Pvt Ltd','status'=>'active']); $project=Project::firstOrCreate(['code'=>'SS-CMS'],['name'=>'SoftSuite Company CMS','client_id'=>$client->id,'manager_user_id'=>$lead->id,'department_id'=>$eng->id,'technology_stack'=>'Laravel 13, Blade, Alpine.js, MySQL','start_date'=>today(),'status'=>'active','priority'=>'high','health'=>'green','budget_hours'=>1200]);
  ProjectMember::firstOrCreate(['project_id'=>$project->id,'user_id'=>$lead->id],['role'=>'Project Manager','allocation_percent'=>30]); ProjectMember::firstOrCreate(['project_id'=>$project->id,'user_id'=>$emp->id],['role'=>'Backend Engineer','allocation_percent'=>80]); ProjectMember::firstOrCreate(['project_id'=>$project->id,'user_id'=>$internUser->id],['role'=>'Engineering Intern','allocation_percent'=>50]);
  Task::firstOrCreate(['project_id'=>$project->id,'title'=>'Implement secure attendance module'],['reporter_user_id'=>$lead->id,'primary_assignee_id'=>$emp->id,'priority'=>'high','status'=>'in_progress','estimate_minutes'=>480,'due_date'=>today()->addDays(5),'acceptance_criteria'=>'IP-restricted punch, rejected-attempt logging, duplicate prevention.']);
  SalaryRevision::firstOrCreate(['employee_id'=>$employee->id,'effective_from'=>today()->subMonths(6)->startOfMonth()],['base_salary'=>120000,'allowances'=>['internet'=>5000],'deductions'=>[],'approved_by'=>$founder->id]);
  foreach([['Annual Leave','AL','employee',18,true],['Sick Leave','SL','all',10,true],['Intern Absence','IA','intern',6,false]] as $l) LeaveType::firstOrCreate(['code'=>$l[1]],['name'=>$l[0],'worker_type'=>$l[2],'annual_days'=>$l[3],'is_paid'=>$l[4],'is_active'=>true]);
  ExpenseCategory::firstOrCreate(['code'=>'OFFICE'],['name'=>'Office Operations']); ExpenseCategory::firstOrCreate(['code'=>'PROJECT'],['name'=>'Project Expense']);
  Announcement::firstOrCreate(['title'=>'Welcome to SoftSuite CMS'],['body'=>'This seeded environment demonstrates separate role-based portals and IP-controlled attendance.','audience'=>'all','published_by'=>$founder->id,'published_at'=>now(),'is_active'=>true]);
 }
}
