<?php

namespace Database\Seeders;

use App\Models\Announcement;
use App\Models\Banking;
use App\Models\CalendarEvent;
use App\Models\Child;
use App\Models\Company;
use App\Models\Department;
use App\Models\Education;
use App\Models\EmploymentHistory;
use App\Models\MedicalDeclaration;
use App\Models\MedicalHistory;
use App\Models\Spouse;
use App\Models\Staff;
use App\Models\StaffAddress;
use App\Models\StaffLeaveAllocation;
use App\Models\StaffPhone;
use App\Models\StaffSeparation;
use App\Models\StaffTraining;
use App\Models\Unit;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    public function run(): void
    {
        // ── Users ─────────────────────────────────────────────────────────────
        $admin = User::create(['name' => 'Admin HR', 'email' => 'admin@hrportal.my', 'password' => Hash::make('password'), 'role' => 'super_admin']);
        User::create(['name' => 'Johan', 'email' => 'johan@titiansystems.com', 'password' => Hash::make('Tt5dd0n1y!@#'), 'role' => 'super_admin']);

        // ── Companies ─────────────────────────────────────────────────────────
        $nexus   = Company::create(['name' => 'Nexus Technologies Sdn Bhd',   'registration_no' => 'RC-2001-NX', 'industry' => 'Technology', 'address' => 'Level 15, KL Eco City',         'city' => 'Kuala Lumpur', 'state' => 'Wilayah Persekutuan', 'country' => 'Malaysia', 'status' => 'active']);
        $bumi    = Company::create(['name' => 'Bumi Resources Group Sdn Bhd', 'registration_no' => 'RC-1995-BR', 'industry' => 'Resources',  'address' => 'Menara Bumi, Jln Tun Razak',     'city' => 'Kuala Lumpur', 'state' => 'Wilayah Persekutuan', 'country' => 'Malaysia', 'status' => 'active']);
        $pacific = Company::create(['name' => 'Pacific Logistics Sdn Bhd',    'registration_no' => 'RC-2008-PL', 'industry' => 'Logistics',  'address' => 'Port Klang Logistics Hub',       'city' => 'Klang',        'state' => 'Selangor',            'country' => 'Malaysia', 'status' => 'active']);

        $hrNexus = User::create(['name' => 'Siti Aminah', 'email' => 'hr@nexus.my',        'password' => Hash::make('password'), 'role' => 'company_hr', 'company_id' => $nexus->id]);
        $hrBumi  = User::create(['name' => 'Raj Kumar',   'email' => 'hr@bumiresources.my', 'password' => Hash::make('password'), 'role' => 'company_hr', 'company_id' => $bumi->id]);

        // ── Departments & Units ───────────────────────────────────────────────
        // Nexus
        $deptIT      = Department::create(['company_id' => $nexus->id,   'name' => 'Information Technology']);
        $deptHR      = Department::create(['company_id' => $nexus->id,   'name' => 'Human Resources']);
        $deptSales   = Department::create(['company_id' => $nexus->id,   'name' => 'Sales & Marketing']);
        // Bumi
        $deptFin     = Department::create(['company_id' => $bumi->id,    'name' => 'Finance']);
        $deptBumiHR  = Department::create(['company_id' => $bumi->id,    'name' => 'Human Resources']);
        $deptLegal   = Department::create(['company_id' => $bumi->id,    'name' => 'Legal & Compliance']);
        // Pacific
        $deptOps     = Department::create(['company_id' => $pacific->id, 'name' => 'Operations']);
        $deptLogist  = Department::create(['company_id' => $pacific->id, 'name' => 'Logistics']);

        // Nexus – IT units
        $unitDev     = Unit::create(['department_id' => $deptIT->id,     'name' => 'Software Development']);
        $unitInfra   = Unit::create(['department_id' => $deptIT->id,     'name' => 'Infrastructure']);
        $unitQA      = Unit::create(['department_id' => $deptIT->id,     'name' => 'QA & Testing']);
        // Nexus – HR units
        $unitRecruit = Unit::create(['department_id' => $deptHR->id,     'name' => 'Recruitment']);
        $unitPayroll = Unit::create(['department_id' => $deptHR->id,     'name' => 'Payroll']);
        // Bumi – Finance units
        $unitAccts   = Unit::create(['department_id' => $deptFin->id,    'name' => 'Accounts']);
        $unitTreasury= Unit::create(['department_id' => $deptFin->id,    'name' => 'Treasury']);
        // Pacific – Operations units
        $unitWH      = Unit::create(['department_id' => $deptOps->id,    'name' => 'Warehouse']);
        $unitTransp  = Unit::create(['department_id' => $deptOps->id,    'name' => 'Transport']);

        // ── Staff ─────────────────────────────────────────────────────────────
        // Nexus – IT / Software Development
        $ahmad = Staff::create([
            'company_id' => $nexus->id, 'department_id' => $deptIT->id, 'unit_id' => $unitDev->id,
            'staff_no' => 'NX-001', 'full_name' => 'Ahmad Farizal bin Hamid', 'gender' => 'Male',
            'ic_no' => '850312-14-5678', 'date_of_birth' => '1985-03-12', 'nationality' => 'Malaysian',
            'religion' => 'Islam', 'race' => 'Malay', 'marital_status' => 'Married',
            'email' => 'ahmad.farizal@nexus.my', 'mobile_no' => '012-3456789',
            'job_title' => 'Senior Software Engineer', 'employment_type' => 'Permanent',
            'join_date' => '2015-06-01', 'basic_salary' => 8500.00,
            'status' => 'active', 'self_service_token' => 'demo-self-service-token-001',
        ]);

        // Nexus – IT / Infrastructure
        $kevin = Staff::create([
            'company_id' => $nexus->id, 'department_id' => $deptIT->id, 'unit_id' => $unitInfra->id,
            'staff_no' => 'NX-003', 'full_name' => 'Kevin Lim Jun Hao', 'gender' => 'Male',
            'ic_no' => '881204-07-4321', 'date_of_birth' => '1988-12-04', 'nationality' => 'Malaysian',
            'religion' => 'Christianity', 'race' => 'Chinese', 'marital_status' => 'Married',
            'email' => 'kevin.lim@nexus.my', 'mobile_no' => '017-3334455',
            'job_title' => 'Network Engineer', 'employment_type' => 'Permanent',
            'join_date' => '2017-04-10', 'basic_salary' => 6800.00, 'status' => 'active',
        ]);

        // Nexus – IT / QA
        $nurul = Staff::create([
            'company_id' => $nexus->id, 'department_id' => $deptIT->id, 'unit_id' => $unitQA->id,
            'staff_no' => 'NX-004', 'full_name' => 'Nurul Izzah binti Roslan', 'gender' => 'Female',
            'ic_no' => '940208-14-6789', 'date_of_birth' => '1994-02-08', 'nationality' => 'Malaysian',
            'religion' => 'Islam', 'race' => 'Malay', 'marital_status' => 'Single',
            'email' => 'nurul.izzah@nexus.my', 'mobile_no' => '011-2244556',
            'job_title' => 'QA Engineer', 'employment_type' => 'Permanent',
            'join_date' => '2020-09-01', 'basic_salary' => 5200.00, 'status' => 'active',
        ]);

        // Nexus – HR / Recruitment
        $mei = Staff::create([
            'company_id' => $nexus->id, 'department_id' => $deptHR->id, 'unit_id' => $unitRecruit->id,
            'staff_no' => 'NX-002', 'full_name' => 'Mei Ling Tan', 'gender' => 'Female',
            'ic_no' => '900715-07-3456', 'date_of_birth' => '1990-07-15', 'nationality' => 'Malaysian',
            'religion' => 'Buddhism', 'race' => 'Chinese', 'marital_status' => 'Single',
            'email' => 'mei.ling@nexus.my', 'mobile_no' => '016-7654321',
            'job_title' => 'HR Executive', 'employment_type' => 'Permanent',
            'join_date' => '2019-01-15', 'basic_salary' => 4500.00, 'status' => 'active',
        ]);

        // Nexus – HR / Payroll
        $aini = Staff::create([
            'company_id' => $nexus->id, 'department_id' => $deptHR->id, 'unit_id' => $unitPayroll->id,
            'staff_no' => 'NX-005', 'full_name' => 'Aini binti Sulaiman', 'gender' => 'Female',
            'ic_no' => '870615-14-2222', 'date_of_birth' => '1987-06-15', 'nationality' => 'Malaysian',
            'religion' => 'Islam', 'race' => 'Malay', 'marital_status' => 'Married',
            'email' => 'aini.sulaiman@nexus.my', 'mobile_no' => '013-5556677',
            'job_title' => 'Payroll Executive', 'employment_type' => 'Permanent',
            'join_date' => '2016-03-01', 'basic_salary' => 4200.00, 'status' => 'active',
        ]);

        // Nexus – Sales & Marketing
        $darren = Staff::create([
            'company_id' => $nexus->id, 'department_id' => $deptSales->id,
            'staff_no' => 'NX-006', 'full_name' => 'Darren Chong Wei Keong', 'gender' => 'Male',
            'ic_no' => '920330-10-8888', 'date_of_birth' => '1992-03-30', 'nationality' => 'Malaysian',
            'religion' => 'Buddhism', 'race' => 'Chinese', 'marital_status' => 'Single',
            'email' => 'darren.chong@nexus.my', 'mobile_no' => '018-9990001',
            'job_title' => 'Sales Executive', 'employment_type' => 'Permanent',
            'join_date' => '2022-08-01', 'basic_salary' => 4000.00, 'status' => 'active',
        ]);

        // Nexus – IT Trainee
        $trainee1 = Staff::create([
            'company_id' => $nexus->id, 'department_id' => $deptIT->id, 'unit_id' => $unitDev->id,
            'staff_no' => 'NX-T01', 'full_name' => 'Hafiz bin Zulkifli', 'gender' => 'Male',
            'ic_no' => '010415-14-1234', 'date_of_birth' => '2001-04-15', 'nationality' => 'Malaysian',
            'email' => 'hafiz.trainee@nexus.my',
            'job_title' => 'IT Trainee', 'employment_type' => 'Trainee',
            'join_date' => now()->subMonths(2)->format('Y-m-d'),
            'contract_end_date' => now()->addMonths(4)->format('Y-m-d'),
            'status' => 'active',
        ]);

        // Bumi – Finance / Accounts
        $raju = Staff::create([
            'company_id' => $bumi->id, 'department_id' => $deptFin->id, 'unit_id' => $unitAccts->id,
            'staff_no' => 'BR-001', 'full_name' => 'Raju a/l Krishnan', 'gender' => 'Male',
            'ic_no' => '780520-10-1234', 'date_of_birth' => '1978-05-20', 'nationality' => 'Malaysian',
            'religion' => 'Hinduism', 'race' => 'Indian', 'marital_status' => 'Married',
            'email' => 'raju@bumiresources.my', 'mobile_no' => '019-1234567',
            'job_title' => 'Finance Manager', 'employment_type' => 'Permanent',
            'join_date' => '2010-03-01', 'basic_salary' => 9200.00, 'status' => 'active',
        ]);

        // Bumi – Finance / Treasury
        $faridah = Staff::create([
            'company_id' => $bumi->id, 'department_id' => $deptFin->id, 'unit_id' => $unitTreasury->id,
            'staff_no' => 'BR-002', 'full_name' => 'Faridah binti Noor', 'gender' => 'Female',
            'ic_no' => '830410-10-5678', 'date_of_birth' => '1983-04-10', 'nationality' => 'Malaysian',
            'religion' => 'Islam', 'race' => 'Malay', 'marital_status' => 'Married',
            'email' => 'faridah.noor@bumiresources.my', 'mobile_no' => '013-8887766',
            'job_title' => 'Treasury Analyst', 'employment_type' => 'Permanent',
            'join_date' => '2014-07-01', 'basic_salary' => 6500.00, 'status' => 'active',
        ]);

        // Bumi – HR
        $chandran = Staff::create([
            'company_id' => $bumi->id, 'department_id' => $deptBumiHR->id,
            'staff_no' => 'BR-003', 'full_name' => 'Chandran a/l Pillai', 'gender' => 'Male',
            'ic_no' => '760915-10-9999', 'date_of_birth' => '1976-09-15', 'nationality' => 'Malaysian',
            'religion' => 'Hinduism', 'race' => 'Indian', 'marital_status' => 'Married',
            'email' => 'chandran@bumiresources.my', 'mobile_no' => '012-7776655',
            'job_title' => 'HR Manager', 'employment_type' => 'Permanent',
            'join_date' => '2008-01-15', 'basic_salary' => 8000.00, 'status' => 'active',
        ]);

        // Bumi – Legal
        $liyana = Staff::create([
            'company_id' => $bumi->id, 'department_id' => $deptLegal->id,
            'staff_no' => 'BR-004', 'full_name' => 'Liyana binti Azhar', 'gender' => 'Female',
            'ic_no' => '900122-10-3333', 'date_of_birth' => '1990-01-22', 'nationality' => 'Malaysian',
            'religion' => 'Islam', 'race' => 'Malay', 'marital_status' => 'Single',
            'email' => 'liyana.azhar@bumiresources.my', 'mobile_no' => '017-1112233',
            'job_title' => 'Legal Executive', 'employment_type' => 'Contract',
            'join_date' => '2023-01-01', 'contract_end_date' => '2025-12-31',
            'basic_salary' => 5500.00, 'status' => 'active',
        ]);

        // Pacific – Operations / Warehouse
        $sarah = Staff::create([
            'company_id' => $pacific->id, 'department_id' => $deptOps->id, 'unit_id' => $unitWH->id,
            'staff_no' => 'PL-001', 'full_name' => 'Sarah binti Abdullah', 'gender' => 'Female',
            'ic_no' => '920830-05-8765', 'date_of_birth' => '1992-08-30', 'nationality' => 'Malaysian',
            'religion' => 'Islam', 'race' => 'Malay', 'marital_status' => 'Single',
            'email' => 'sarah@pacificlogistics.my', 'mobile_no' => '011-9876543',
            'job_title' => 'Warehouse Supervisor', 'employment_type' => 'Permanent',
            'join_date' => '2021-05-10', 'basic_salary' => 3800.00, 'status' => 'active',
        ]);

        // Pacific – Operations / Transport
        $ramlan = Staff::create([
            'company_id' => $pacific->id, 'department_id' => $deptOps->id, 'unit_id' => $unitTransp->id,
            'staff_no' => 'PL-002', 'full_name' => 'Ramlan bin Othman', 'gender' => 'Male',
            'ic_no' => '810505-05-1111', 'date_of_birth' => '1981-05-05', 'nationality' => 'Malaysian',
            'religion' => 'Islam', 'race' => 'Malay', 'marital_status' => 'Married',
            'email' => 'ramlan@pacificlogistics.my', 'mobile_no' => '019-5554433',
            'job_title' => 'Fleet Coordinator', 'employment_type' => 'Permanent',
            'join_date' => '2013-11-01', 'basic_salary' => 4300.00, 'status' => 'active',
        ]);

        // Pacific – Logistics
        $jessica = Staff::create([
            'company_id' => $pacific->id, 'department_id' => $deptLogist->id,
            'staff_no' => 'PL-003', 'full_name' => 'Jessica Wong Mei Xian', 'gender' => 'Female',
            'ic_no' => '950618-05-7777', 'date_of_birth' => '1995-06-18', 'nationality' => 'Malaysian',
            'religion' => 'Buddhism', 'race' => 'Chinese', 'marital_status' => 'Single',
            'email' => 'jessica.wong@pacificlogistics.my', 'mobile_no' => '016-2223344',
            'job_title' => 'Logistics Officer', 'employment_type' => 'Permanent',
            'join_date' => '2022-02-14', 'basic_salary' => 3600.00, 'status' => 'active',
        ]);

        // ── Former staff (terminated) ─────────────────────────────────────────
        $formerStaff = Staff::create([
            'company_id' => $nexus->id, 'department_id' => $deptIT->id, 'unit_id' => $unitInfra->id,
            'staff_no' => 'NX-EX1', 'full_name' => 'David Lim Chee Keong', 'gender' => 'Male',
            'ic_no' => '880120-14-7890', 'email' => 'david.lim@nexus.my',
            'job_title' => 'IT Support Specialist', 'employment_type' => 'Permanent',
            'join_date' => '2018-03-01', 'basic_salary' => 4200.00, 'status' => 'terminated',
        ]);

        // ── Education ─────────────────────────────────────────────────────────
        Education::create(['staff_id' => $ahmad->id,  'institution' => 'Universiti Malaya',   'qualification' => 'Bachelor', 'field_of_study' => 'Computer Science', 'grade' => 'Second Class Upper', 'start_date' => '2003-07-01', 'end_date' => '2007-06-30']);
        Education::create(['staff_id' => $raju->id,   'institution' => 'TARC College',        'qualification' => 'Diploma',  'field_of_study' => 'Accounting',       'grade' => 'Distinction',        'start_date' => '1997-01-01', 'end_date' => '1999-12-31']);
        Education::create(['staff_id' => $nurul->id,  'institution' => 'UTM Johor Bahru',     'qualification' => 'Bachelor', 'field_of_study' => 'Software Engineering', 'grade' => 'First Class', 'start_date' => '2012-07-01', 'end_date' => '2016-05-30']);
        Education::create(['staff_id' => $jessica->id,'institution' => 'Sunway University',   'qualification' => 'Diploma',  'field_of_study' => 'Business Studies', 'grade' => 'Credit',             'start_date' => '2013-01-01', 'end_date' => '2015-12-31']);

        // ── Banking ───────────────────────────────────────────────────────────
        Banking::create(['staff_id' => $ahmad->id,  'bank_name' => 'Maybank', 'account_no' => '1234-5678-9012', 'account_name' => 'Ahmad Farizal bin Hamid', 'account_type' => 'savings', 'is_primary' => true]);
        Banking::create(['staff_id' => $raju->id,   'bank_name' => 'CIMB',    'account_no' => '9876-5432-1098', 'account_name' => 'Raju a/l Krishnan',       'account_type' => 'savings', 'is_primary' => true]);
        Banking::create(['staff_id' => $sarah->id,  'bank_name' => 'RHB',     'account_no' => '1111-2222-3333', 'account_name' => 'Sarah binti Abdullah',    'account_type' => 'savings', 'is_primary' => true]);

        // ── Spouses ───────────────────────────────────────────────────────────
        Spouse::create(['staff_id' => $ahmad->id,  'full_name' => 'Nurul Ain binti Razak', 'ic_no' => '870625-14-2345', 'phone' => '013-9876543', 'occupation' => 'Teacher']);
        Spouse::create(['staff_id' => $raju->id,   'full_name' => 'Kavitha a/p Murugan',  'ic_no' => '800312-10-5678', 'phone' => '012-4567890', 'occupation' => 'Nurse']);
        Spouse::create(['staff_id' => $ramlan->id, 'full_name' => 'Rohana binti Hassan',  'ic_no' => '830610-05-4444', 'phone' => '013-1112222', 'occupation' => 'Housewife']);

        // ── Children ──────────────────────────────────────────────────────────
        Child::create(['staff_id' => $ahmad->id, 'full_name' => 'Ahmad Irfan bin Ahmad Farizal', 'date_of_birth' => '2012-03-15', 'gender' => 'Male',   'ic_no' => '120315-14-1234']);
        Child::create(['staff_id' => $ahmad->id, 'full_name' => 'Aisyah binti Ahmad Farizal',    'date_of_birth' => '2015-08-20', 'gender' => 'Female', 'ic_no' => '150820-14-5678']);
        Child::create(['staff_id' => $raju->id,  'full_name' => 'Vijay a/l Raju',                'date_of_birth' => '2010-11-05', 'gender' => 'Male',   'ic_no' => '101105-10-9999']);

        // ── Employment History ────────────────────────────────────────────────
        EmploymentHistory::create(['staff_id' => $ahmad->id, 'job_title' => 'Junior Software Engineer', 'department' => 'IT', 'employment_type' => 'Permanent', 'start_date' => '2015-06-01', 'end_date' => '2017-12-31', 'remarks' => 'Joined as junior engineer']);
        EmploymentHistory::create(['staff_id' => $ahmad->id, 'job_title' => 'Software Engineer',        'department' => 'IT', 'employment_type' => 'Permanent', 'start_date' => '2018-01-01', 'end_date' => '2021-06-30', 'remarks' => 'Promoted to mid-level']);
        EmploymentHistory::create(['staff_id' => $ahmad->id, 'job_title' => 'Senior Software Engineer', 'department' => 'IT', 'employment_type' => 'Permanent', 'start_date' => '2021-07-01', 'end_date' => null,          'remarks' => 'Current position']);

        // ── Training ─────────────────────────────────────────────────────────
        StaffTraining::create(['staff_id' => $ahmad->id,  'training_name' => 'Laravel Advanced Workshop', 'provider' => 'Laravel MY', 'training_type' => 'Technical',    'start_date' => '2023-03-15', 'end_date' => '2023-03-17', 'duration_hours' => 24, 'status' => 'completed']);
        StaffTraining::create(['staff_id' => $ahmad->id,  'training_name' => 'AWS Solutions Architect',   'provider' => 'Amazon',     'training_type' => 'Technical',    'start_date' => '2022-11-01', 'end_date' => '2022-11-30', 'duration_hours' => 40, 'status' => 'completed', 'certificate_no' => 'AWS-SAA-2022']);
        StaffTraining::create(['staff_id' => $raju->id,   'training_name' => 'ACCA Financial Reporting',  'provider' => 'ACCA',       'training_type' => 'Professional', 'start_date' => '2024-01-10', 'end_date' => '2024-01-12', 'duration_hours' => 16, 'status' => 'completed']);
        StaffTraining::create(['staff_id' => $nurul->id,  'training_name' => 'ISTQB Certification',       'provider' => 'ISTQB',      'training_type' => 'Technical',    'start_date' => '2023-06-05', 'end_date' => '2023-06-06', 'duration_hours' => 16, 'status' => 'completed', 'certificate_no' => 'ISTQB-2023']);
        StaffTraining::create(['staff_id' => $sarah->id,  'training_name' => 'Occupational Safety (OSHA)','provider' => 'DOSH MY',    'training_type' => 'Compliance',   'start_date' => '2022-09-15', 'end_date' => '2022-09-16', 'duration_hours' => 8,  'status' => 'completed']);

        // ── Medical ───────────────────────────────────────────────────────────
        MedicalHistory::create(['staff_id' => $ahmad->id, 'condition_name' => 'Mild Hypertension', 'diagnosed_date' => '2020-06-15', 'treatment' => 'Medication', 'is_chronic' => true, 'notes' => 'Under control']);
        MedicalDeclaration::create(['staff_id' => $ahmad->id, 'declaration_date' => '2023-01-10', 'has_pre_existing_conditions' => true, 'conditions_detail' => 'Mild hypertension', 'is_fit_for_duty' => true, 'doctor_name' => 'Dr. Azman', 'clinic_name' => 'Klinik Azman']);

        // ── Addresses ─────────────────────────────────────────────────────────
        StaffAddress::create(['staff_id' => $ahmad->id,  'label' => 'Home', 'address' => 'No. 12, Jalan Damai 3',    'city' => 'Petaling Jaya', 'state' => 'Selangor',   'postcode' => '47810', 'country' => 'Malaysia', 'is_current' => true]);
        StaffAddress::create(['staff_id' => $raju->id,   'label' => 'Home', 'address' => 'No. 45, Jalan Imbi',       'city' => 'Kuala Lumpur',  'state' => 'WP KL',      'postcode' => '55100', 'country' => 'Malaysia', 'is_current' => true]);
        StaffAddress::create(['staff_id' => $sarah->id,  'label' => 'Home', 'address' => 'No. 7, Jalan Kapar 2',     'city' => 'Klang',         'state' => 'Selangor',   'postcode' => '41000', 'country' => 'Malaysia', 'is_current' => true]);

        // ── Phones ────────────────────────────────────────────────────────────
        StaffPhone::create(['staff_id' => $ahmad->id, 'phone' => '012-3456789', 'type' => 'mobile', 'is_primary' => true]);
        StaffPhone::create(['staff_id' => $ahmad->id, 'phone' => '03-12345678', 'type' => 'office', 'is_primary' => false]);
        StaffPhone::create(['staff_id' => $raju->id,  'phone' => '019-1234567', 'type' => 'mobile', 'is_primary' => true]);

        // ── Leave Allocations ─────────────────────────────────────────────────
        $year      = now()->year;
        $leaveStaff = [$ahmad, $mei, $aini, $kevin, $nurul, $darren, $raju, $faridah, $chandran, $sarah, $ramlan, $jessica];
        foreach ($leaveStaff as $s) {
            StaffLeaveAllocation::create(['staff_id' => $s->id, 'leave_type' => 'Annual Leave',  'year' => $year, 'days_allocated' => 16, 'days_taken' => rand(0, 8), 'days_carried_forward' => 2, 'days_balance' => 16 + 2 - rand(0, 8)]);
            StaffLeaveAllocation::create(['staff_id' => $s->id, 'leave_type' => 'Medical Leave', 'year' => $year, 'days_allocated' => 14, 'days_taken' => rand(0, 4), 'days_carried_forward' => 0, 'days_balance' => 14 - rand(0, 4)]);
        }

        // ── Calendar Events ───────────────────────────────────────────────────
        CalendarEvent::create(['title' => 'Hari Raya Aidilfitri',               'start_date' => $year . '-03-31', 'end_date' => $year . '-04-01', 'event_type' => 'holiday', 'is_public_holiday' => true,  'is_all_day' => true,  'created_by' => $admin->id]);
        CalendarEvent::create(['title' => 'Labour Day',                          'start_date' => $year . '-05-01', 'event_type' => 'holiday',  'is_public_holiday' => true,  'is_all_day' => true,  'created_by' => $admin->id]);
        CalendarEvent::create(['title' => 'Merdeka Day',                         'start_date' => $year . '-08-31', 'event_type' => 'holiday',  'is_public_holiday' => true,  'is_all_day' => true,  'created_by' => $admin->id]);
        CalendarEvent::create(['title' => 'Q3 All-Hands Meeting',                'start_date' => $year . '-07-15', 'event_type' => 'meeting',  'is_public_holiday' => false, 'is_all_day' => false, 'company_id' => $nexus->id,  'description' => 'Quarterly all-staff meeting', 'created_by' => $hrNexus->id]);
        CalendarEvent::create(['title' => 'Annual Performance Review Deadline',  'start_date' => $year . '-12-15', 'event_type' => 'deadline', 'is_public_holiday' => false, 'is_all_day' => true,  'created_by' => $admin->id]);

        // ── Announcements ─────────────────────────────────────────────────────
        Announcement::create(['title' => 'New Leave Policy Effective Q3', 'content' => 'Effective 1st July, all staff will receive an additional 2 days of annual leave. Please refer to the updated Employee Handbook for details.', 'type' => 'hr', 'priority' => 'high', 'is_pinned' => true, 'target_audience' => 'all', 'publish_date' => now()->subDays(5)->format('Y-m-d'), 'created_by' => $admin->id]);
        Announcement::create(['title' => 'Office Closure: Hari Raya', 'content' => 'The office will be closed from 31st March to 2nd April for Hari Raya Aidilfitri. Emergency contacts will remain active.', 'type' => 'general', 'priority' => 'normal', 'is_pinned' => false, 'target_audience' => 'all', 'publish_date' => now()->subDays(14)->format('Y-m-d'), 'expiry_date' => now()->addDays(30)->format('Y-m-d'), 'created_by' => $admin->id]);
        Announcement::create(['title' => 'IT Infrastructure Maintenance', 'content' => 'Scheduled maintenance on all IT systems this Saturday from 11pm to 3am. Systems may be unavailable during this window.', 'type' => 'operations', 'priority' => 'high', 'is_pinned' => false, 'target_audience' => 'all', 'company_id' => $nexus->id, 'publish_date' => now()->subDays(2)->format('Y-m-d'), 'created_by' => $hrNexus->id]);

        // ── Staff Separation ──────────────────────────────────────────────────
        StaffSeparation::create(['staff_id' => $formerStaff->id, 'separation_type' => 'Resignation', 'last_working_day' => now()->subMonths(3)->format('Y-m-d'), 'effective_date' => now()->subMonths(3)->format('Y-m-d'), 'reason' => 'Better opportunity elsewhere', 'is_eligible_rehire' => true, 'notice_period' => '1 month', 'approved_by' => $admin->id]);

        // ── Applications ──────────────────────────────────────────────────────
        \App\Models\Application::insert([
            ['company_id' => $nexus->id,   'applicant_name' => 'Haziq bin Nordin',   'applicant_email' => 'haziq@gmail.com',   'position_applied' => 'Backend Developer',   'type' => 'job',     'status' => 'shortlisted', 'created_at' => now(), 'updated_at' => now()],
            ['company_id' => $nexus->id,   'applicant_name' => 'Priya a/p Selvam',   'applicant_email' => 'priya@hotmail.com', 'position_applied' => 'HR Assistant',        'type' => 'job',     'status' => 'pending',     'created_at' => now(), 'updated_at' => now()],
            ['company_id' => $bumi->id,    'applicant_name' => 'Amirul Faris',        'applicant_email' => 'amirul@gmail.com', 'position_applied' => 'Finance Executive',   'type' => 'job',     'status' => 'interviewed', 'created_at' => now(), 'updated_at' => now()],
            ['company_id' => $nexus->id,   'applicant_name' => 'Nur Syafiqah',        'applicant_email' => 'syafiqah@live.com','position_applied' => 'IT Intern',           'type' => 'trainee', 'status' => 'accepted',    'created_at' => now(), 'updated_at' => now()],
            ['company_id' => $pacific->id, 'applicant_name' => 'Jason Tan Wei Ming', 'applicant_email' => 'jason@gmail.com',  'position_applied' => 'Logistics Officer',   'type' => 'job',     'status' => 'offered',     'created_at' => now(), 'updated_at' => now()],
        ]);

        // ── Pending Changes ───────────────────────────────────────────────────
        \App\Models\PendingChange::insert([
            ['staff_id' => $ahmad->id, 'company_id' => $nexus->id, 'change_type' => 'contact_update', 'table_name' => 'staff', 'field_changes' => json_encode(['mobile_no' => '012-9999888', 'email' => 'ahmad.new@nexus.my']),         'status' => 'pending', 'created_at' => now(), 'updated_at' => now()],
            ['staff_id' => $mei->id,   'company_id' => $nexus->id, 'change_type' => 'address_update', 'table_name' => 'staff', 'field_changes' => json_encode(['address' => 'No. 8, Jalan PJ 7', 'city' => 'Petaling Jaya', 'postcode' => '47300']), 'status' => 'pending', 'created_at' => now(), 'updated_at' => now()],
        ]);
    }
}
