<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class HRDemoSeeder extends Seeder
{
    public function run(): void
    {
        $academyId = 1;
        $userId = 1;
        $now = now();

        // Clean up any orphan data from previous failed runs
        DB::table('trainer_rate_history')->where('academy_id', $academyId)->delete();
        DB::table('trainer_advances')->where('academy_id', $academyId)->delete();
        DB::table('payslip_items')->whereIn('payslip_id', DB::table('payslips')->where('academy_id', $academyId)->pluck('id'))->delete();
        DB::table('payslips')->where('academy_id', $academyId)->delete();
        DB::table('payroll_periods')->where('academy_id', $academyId)->delete();
        DB::table('trainer_compensations')->where('academy_id', $academyId)->delete();
        DB::table('trainer_availability')->whereIn('trainer_id', DB::table('trainers')->where('academy_id', $academyId)->pluck('id'))->delete();
        DB::table('trainer_qualifications')->whereIn('trainer_id', DB::table('trainers')->where('academy_id', $academyId)->pluck('id'))->delete();
        DB::table('trainers')->where('academy_id', $academyId)->delete();
        DB::table('employees')->where('academy_id', $academyId)->delete();

        // ─── Enable payroll settings ───
        DB::table('system_settings')->updateOrInsert(
            ['academy_id' => $academyId, 'key' => 'payroll_enabled'],
            ['group' => 'hr', 'value' => '1', 'type' => 'boolean', 'description_ar' => 'تفعيل نظام الرواتب', 'is_public' => false, 'created_at' => $now, 'updated_at' => $now]
        );
        DB::table('system_settings')->updateOrInsert(
            ['academy_id' => $academyId, 'key' => 'auto_trainer_compensation_enabled'],
            ['group' => 'hr', 'value' => '1', 'type' => 'boolean', 'description_ar' => 'تفعيل حساب مستحقات المدربين تلقائياً', 'is_public' => false, 'created_at' => $now, 'updated_at' => $now]
        );

        // ─── Create branches if missing ───
        $branch1 = DB::table('branches')->where('academy_id', $academyId)->where('name_ar', 'المعادي')->first();
        if (!$branch1) {
            $branch1Id = DB::table('branches')->insertGetId([
                'uuid' => Str::uuid(),
                'academy_id' => $academyId,
                'name' => 'Maadi',
                'name_ar' => 'المعادي',
                'code' => 'MAA',
                'phone' => '0225123456',
                'address' => 'شارع 9 — المعادي',
                'city' => 'القاهرة',
                'governorate' => 'القاهرة',
                'is_active' => true,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        } else {
            $branch1Id = $branch1->id;
        }

        $branch2 = DB::table('branches')->where('academy_id', $academyId)->where('name_ar', 'مصر الجديدة')->first();
        if (!$branch2) {
            $branch2Id = DB::table('branches')->insertGetId([
                'uuid' => Str::uuid(),
                'academy_id' => $academyId,
                'name' => 'Heliopolis',
                'name_ar' => 'مصر الجديدة',
                'code' => 'HEL',
                'phone' => '0224567890',
                'address' => 'شارع الميرغني — مصر الجديدة',
                'city' => 'القاهرة',
                'governorate' => 'القاهرة',
                'is_active' => true,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        } else {
            $branch2Id = $branch2->id;
        }

        // Use existing branch 3 (Sheikh Zayed)
        $branch3Id = 3;

        $branchIds = [$branch1Id, $branch2Id, $branch3Id];

        // ─── Create People for staff (non-trainers) ───
        $staffData = [
            ['name_ar' => 'أحمد وجدي حسن',       'name' => 'Ahmed Wagdy',    'phone' => '01055000001', 'gender' => 'male',   'dob' => '1985-03-12'],
            ['name_ar' => 'سمر حسني إبراهيم',    'name' => 'Samar Hosny',    'phone' => '01055000002', 'gender' => 'female', 'dob' => '1990-07-22'],
            ['name_ar' => 'محمود عادل سيد',       'name' => 'Mahmoud Adel',   'phone' => '01055000003', 'gender' => 'male',   'dob' => '1988-11-05'],
            ['name_ar' => 'هند السعيد محمد',      'name' => 'Hend Elsaeed',   'phone' => '01055000004', 'gender' => 'female', 'dob' => '1992-01-19'],
            ['name_ar' => 'عبدالرحمن فوزي خالد', 'name' => 'Abdelrahman F.', 'phone' => '01055000005', 'gender' => 'male',   'dob' => '1995-06-30'],
            ['name_ar' => 'نورهان ياسر أحمد',    'name' => 'Nourhan Yasser', 'phone' => '01055000006', 'gender' => 'female', 'dob' => '1993-09-14'],
            ['name_ar' => 'طارق حسان محمود',     'name' => 'Tarek Hassan',   'phone' => '01055000099', 'gender' => 'male',   'dob' => '1991-04-11'],
        ];

        $staffPersonIds = [];
        foreach ($staffData as $s) {
            $existing = DB::table('people')->where('academy_id', $academyId)->where('phone', $s['phone'])->first();
            if ($existing) {
                $staffPersonIds[] = $existing->id;
            } else {
                $staffPersonIds[] = DB::table('people')->insertGetId([
                    'uuid' => Str::uuid(),
                    'academy_id' => $academyId,
                    'name' => $s['name'],
                    'name_ar' => $s['name_ar'],
                    'phone' => $s['phone'],
                    'gender' => $s['gender'],
                    'date_of_birth' => $s['dob'],
                    'nationality' => 'Egyptian',
                    'created_by' => $userId,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]);
            }
        }

        // ─── Create People for trainers ───
        $trainerPeople = [
            ['name_ar' => 'كريم عبدالله حسن',  'name' => 'Karim Abdullah',  'phone' => '01099000001', 'gender' => 'male',   'dob' => '1990-05-20'],
            ['name_ar' => 'أحمد سمير فتحي',    'name' => 'Ahmed Samir',     'phone' => '01099000002', 'gender' => 'male',   'dob' => '1988-08-15'],
            ['name_ar' => 'دينا محمد علي',      'name' => 'Dina Mohamed',    'phone' => '01099000003', 'gender' => 'female', 'dob' => '1992-03-10'],
            ['name_ar' => 'طارق حسام الدين',    'name' => 'Tarek Hossam',    'phone' => '01099000004', 'gender' => 'male',   'dob' => '1985-12-01'],
            ['name_ar' => 'مريم خالد إبراهيم',  'name' => 'Mariam Khaled',   'phone' => '01099000005', 'gender' => 'female', 'dob' => '1994-07-25'],
            ['name_ar' => 'حسام الدين محمود',   'name' => 'Hossam Mahmoud',  'phone' => '01099000006', 'gender' => 'male',   'dob' => '1983-02-14'],
            ['name_ar' => 'رانيا سعيد أحمد',   'name' => 'Rania Saeed',     'phone' => '01099000007', 'gender' => 'female', 'dob' => '1991-11-30'],
            ['name_ar' => 'عمر حسن محمد',       'name' => 'Omar Hassan',     'phone' => '01099000008', 'gender' => 'male',   'dob' => '1989-06-18'],
            ['name_ar' => 'سلمى يوسف علي',     'name' => 'Salma Youssef',   'phone' => '01099000009', 'gender' => 'female', 'dob' => '1995-09-05'],
            ['name_ar' => 'يوسف عماد حسين',    'name' => 'Youssef Emad',    'phone' => '01099000010', 'gender' => 'male',   'dob' => '1993-01-22'],
            ['name_ar' => 'ليلى أسامة فتحي',   'name' => 'Laila Osama',     'phone' => '01099000011', 'gender' => 'female', 'dob' => '1996-04-16'],
            ['name_ar' => 'كريم وليد سامي',    'name' => 'Karim Waleed',    'phone' => '01099000012', 'gender' => 'male',   'dob' => '1991-10-08'],
        ];

        $trainerPersonIds = [];
        foreach ($trainerPeople as $t) {
            $existing = DB::table('people')->where('academy_id', $academyId)->where('phone', $t['phone'])->first();
            if ($existing) {
                $trainerPersonIds[] = $existing->id;
            } else {
                $trainerPersonIds[] = DB::table('people')->insertGetId([
                    'uuid' => Str::uuid(),
                    'academy_id' => $academyId,
                    'name' => $t['name'],
                    'name_ar' => $t['name_ar'],
                    'phone' => $t['phone'],
                    'gender' => $t['gender'],
                    'date_of_birth' => $t['dob'],
                    'nationality' => 'Egyptian',
                    'created_by' => $userId,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]);
            }
        }

        // ─── Create Employees ───
        // 6 staff employees + 4 trainer-employees = 10 active + 1 resigned
        $employees = [
            ['person_id' => $staffPersonIds[0], 'number' => 'EMP-001', 'dept' => 'management',  'position' => 'مدير الأكاديمية',  'type' => 'full_time', 'salary' => 2500000, 'branch' => $branch1Id, 'start' => '2023-01-15'],
            ['person_id' => $staffPersonIds[1], 'number' => 'EMP-002', 'dept' => 'reception',   'position' => 'موظفة استقبال',    'type' => 'full_time', 'salary' => 800000,  'branch' => $branch1Id, 'start' => '2023-03-01'],
            ['person_id' => $staffPersonIds[2], 'number' => 'EMP-003', 'dept' => 'operations',  'position' => 'مشرف العمليات',    'type' => 'full_time', 'salary' => 1500000, 'branch' => $branch2Id, 'start' => '2023-02-10'],
            ['person_id' => $staffPersonIds[3], 'number' => 'EMP-004', 'dept' => 'reception',   'position' => 'موظفة استقبال',    'type' => 'part_time', 'salary' => 500000,  'branch' => $branch2Id, 'start' => '2023-06-01'],
            ['person_id' => $staffPersonIds[4], 'number' => 'EMP-005', 'dept' => 'maintenance', 'position' => 'فني صيانة',        'type' => 'full_time', 'salary' => 700000,  'branch' => $branch3Id, 'start' => '2023-09-15'],
            ['person_id' => $staffPersonIds[5], 'number' => 'EMP-006', 'dept' => 'accounting',  'position' => 'محاسبة',           'type' => 'full_time', 'salary' => 1200000, 'branch' => $branch1Id, 'start' => '2024-01-01'],
            // Trainer-employees (salaried trainers)
            ['person_id' => $trainerPersonIds[0], 'number' => 'EMP-007', 'dept' => 'training', 'position' => 'مدرب أول',     'type' => 'full_time', 'salary' => 1800000, 'branch' => $branch1Id, 'start' => '2023-01-15'],
            ['person_id' => $trainerPersonIds[1], 'number' => 'EMP-008', 'dept' => 'training', 'position' => 'مدرب أول',     'type' => 'full_time', 'salary' => 1600000, 'branch' => $branch1Id, 'start' => '2023-02-01'],
            ['person_id' => $trainerPersonIds[5], 'number' => 'EMP-009', 'dept' => 'training', 'position' => 'رئيس مدربين', 'type' => 'full_time', 'salary' => 2200000, 'branch' => $branch2Id, 'start' => '2022-11-01'],
            ['person_id' => $trainerPersonIds[9], 'number' => 'EMP-010', 'dept' => 'training', 'position' => 'مدرب',         'type' => 'contract',  'salary' => 1000000, 'branch' => $branch3Id, 'start' => '2024-03-01'],
        ];

        $employeeIds = [];
        $managerId = null;
        foreach ($employees as $i => $e) {
            $id = DB::table('employees')->insertGetId([
                'uuid' => Str::uuid(),
                'academy_id' => $academyId,
                'person_id' => $e['person_id'],
                'employee_number' => $e['number'],
                'department' => $e['dept'],
                'position' => $e['position'],
                'employment_type' => $e['type'],
                'start_date' => $e['start'],
                'salary_amount' => $e['salary'],
                'salary_frequency' => 'monthly',
                'working_hours_per_week' => $e['type'] === 'part_time' ? 20.0 : 40.0,
                'branch_id' => $e['branch'],
                'manager_id' => $i > 0 ? $managerId : null,
                'status' => 'active',
                'created_by' => $userId,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
            $employeeIds[$e['number']] = $id;
            if ($i === 0) $managerId = $id;
        }

        // Add resigned employee
        DB::table('employees')->insert([
            'uuid' => Str::uuid(),
            'academy_id' => $academyId,
            'person_id' => $staffPersonIds[6],
            'employee_number' => 'EMP-011',
            'department' => 'training',
            'position' => 'مدرب سباحة',
            'employment_type' => 'full_time',
            'start_date' => '2023-06-01',
            'end_date' => '2025-12-31',
            'salary_amount' => 900000,
            'salary_frequency' => 'monthly',
            'working_hours_per_week' => 40.0,
            'branch_id' => $branch2Id,
            'manager_id' => $managerId,
            'status' => 'resigned',
            'termination_reason' => 'انتقل لأكاديمية أخرى',
            'created_by' => $userId,
            'created_at' => $now,
            'updated_at' => $now,
        ]);

        // Set one on-leave
        DB::table('employees')->where('id', $employeeIds['EMP-004'])->update([
            'status' => 'on_leave',
            'notes' => 'إجازة أمومة — العودة المتوقعة 2026-09-01',
        ]);

        // ─── Create Trainers ───
        // 4 employee-based + 8 freelancers = 12 total
        $trainerDefs = [
            // Employee-based trainers (indices 0, 1, 5, 9 of trainerPersonIds → linked to EMP-007..010)
            ['idx' => 0, 'emp' => 'EMP-007', 'num' => 'TR-001', 'model' => 'hybrid',      'session' => 15000, 'hourly' => null,  'group' => null,  'player' => null, 'rev' => null],
            ['idx' => 1, 'emp' => 'EMP-008', 'num' => 'TR-002', 'model' => 'salary',      'session' => null,  'hourly' => null,  'group' => null,  'player' => null, 'rev' => null],
            ['idx' => 5, 'emp' => 'EMP-009', 'num' => 'TR-003', 'model' => 'hybrid',      'session' => null,  'hourly' => null,  'group' => 25000, 'player' => null, 'rev' => null],
            ['idx' => 9, 'emp' => 'EMP-010', 'num' => 'TR-004', 'model' => 'per_group',   'session' => null,  'hourly' => null,  'group' => 20000, 'player' => null, 'rev' => null],
            // Freelancer trainers (indices 2,3,4,6,7,8,10,11)
            ['idx' => 2,  'emp' => null, 'num' => 'TR-005', 'model' => 'per_session', 'session' => 12000, 'hourly' => 15000, 'group' => null,  'player' => null,  'rev' => null],
            ['idx' => 3,  'emp' => null, 'num' => 'TR-006', 'model' => 'per_session', 'session' => 18000, 'hourly' => null,  'group' => null,  'player' => null,  'rev' => null],
            ['idx' => 4,  'emp' => null, 'num' => 'TR-007', 'model' => 'per_session', 'session' => 14000, 'hourly' => null,  'group' => null,  'player' => null,  'rev' => null],
            ['idx' => 6,  'emp' => null, 'num' => 'TR-008', 'model' => 'per_session', 'session' => 16000, 'hourly' => null,  'group' => null,  'player' => null,  'rev' => null],
            ['idx' => 7,  'emp' => null, 'num' => 'TR-009', 'model' => 'hourly',      'session' => null,  'hourly' => 13000, 'group' => null,  'player' => null,  'rev' => null],
            ['idx' => 8,  'emp' => null, 'num' => 'TR-010', 'model' => 'per_session', 'session' => 11000, 'hourly' => null,  'group' => null,  'player' => null,  'rev' => null],
            ['idx' => 10, 'emp' => null, 'num' => 'TR-011', 'model' => 'hourly',      'session' => null,  'hourly' => 14000, 'group' => null,  'player' => null,  'rev' => null],
            ['idx' => 11, 'emp' => null, 'num' => 'TR-012', 'model' => 'per_player',  'session' => 17000, 'hourly' => null,  'group' => null,  'player' => 5000,  'rev' => null],
        ];

        $trainerBios = [
            0  => ['bio_ar' => 'مدرب كرة قدم محترف — خبرة 10 سنوات',     'sports' => '["football"]',           'spec' => '["youth","tactics"]'],
            1  => ['bio_ar' => 'مدرب لياقة بدنية معتمد — AFC Level 2',    'sports' => '["fitness"]',            'spec' => '["strength","cardio"]'],
            2  => ['bio_ar' => 'مدربة جمباز — بطلة مصر سابقة',           'sports' => '["gymnastics"]',         'spec' => '["artistic","rhythm"]'],
            3  => ['bio_ar' => 'مدرب سباحة — خبرة أكاديمية وتنافسية',   'sports' => '["swimming"]',           'spec' => '["freestyle","butterfly"]'],
            4  => ['bio_ar' => 'مدربة كاراتيه — حزام أسود دان 3',       'sports' => '["karate"]',             'spec' => '["kata","kumite"]'],
            5  => ['bio_ar' => 'رئيس المدربين — مدرب متعدد الرياضات',    'sports' => '["football","fitness"]', 'spec' => '["management","planning"]'],
            6  => ['bio_ar' => 'مدربة باليه وجمباز إيقاعي',              'sports' => '["ballet","gymnastics"]','spec' => '["ballet","flexibility"]'],
            7  => ['bio_ar' => 'مدرب كرة سلة — الدوري الممتاز سابقاً',  'sports' => '["basketball"]',         'spec' => '["shooting","defense"]'],
            8  => ['bio_ar' => 'مدربة يوغا وبيلاتس',                     'sports' => '["yoga","pilates"]',     'spec' => '["vinyasa","rehabilitation"]'],
            9  => ['bio_ar' => 'مدرب كيك بوكسينج ولياقة قتالية',         'sports' => '["kickboxing"]',         'spec' => '["combat_fitness","self_defense"]'],
            10 => ['bio_ar' => 'مدربة سباحة أطفال — متخصصة في الأعمار الصغيرة', 'sports' => '["swimming"]', 'spec' => '["kids","water_safety"]'],
            11 => ['bio_ar' => 'مدرب تنس — بطل الجمهورية تحت 18',        'sports' => '["tennis"]',             'spec' => '["singles","juniors"]'],
        ];

        $trainerIds = [];
        foreach ($trainerDefs as $i => $td) {
            $bio = $trainerBios[$i];
            $trainerIds[$i] = DB::table('trainers')->insertGetId([
                'uuid' => Str::uuid(),
                'academy_id' => $academyId,
                'employee_id' => $td['emp'] ? $employeeIds[$td['emp']] : null,
                'person_id' => $trainerPersonIds[$td['idx']],
                'trainer_number' => $td['num'],
                'bio_ar' => $bio['bio_ar'],
                'sports' => $bio['sports'],
                'specializations' => $bio['spec'],
                'compensation_model' => $td['model'],
                'session_rate' => $td['session'],
                'hourly_rate' => $td['hourly'],
                'group_rate' => $td['group'],
                'player_rate' => $td['player'],
                'revenue_share_percent' => $td['rev'],
                'rating' => rand(35, 49) / 10,
                'total_sessions_conducted' => rand(40, 200),
                'max_daily_sessions' => rand(3, 6),
                'max_weekly_hours' => rand(20, 35),
                'status' => 'active',
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        }

        // ─── Trainer Qualifications ───
        $qualifications = [
            [0, 'AFC C License',        'اتحاد الكرة المصري',         '2020-06-15', '2027-06-15', true],
            [0, 'First Aid CPR',        'الهلال الأحمر',              '2024-01-10', '2026-01-10', true],
            [1, 'NASM CPT',             'NASM',                       '2019-03-20', '2027-03-20', true],
            [2, 'شهادة تدريب جمباز',    'الاتحاد المصري للجمباز',    '2021-09-01', null,         true],
            [3, 'FINA Swimming Coach',  'FINA',                       '2022-05-15', '2026-05-15', true],
            [4, 'حزام أسود دان 3',     'الاتحاد العالمي للكاراتيه',  '2018-11-20', null,         true],
            [5, 'AFC B License',        'الاتحاد الآسيوي',            '2019-08-01', '2027-08-01', true],
            [5, 'NSCA CSCS',            'NSCA',                       '2020-02-15', '2026-02-15', true],
            [6, 'RAD Ballet Teacher',   'Royal Academy of Dance',     '2021-03-10', null,         true],
            [7, 'FIBA Coaching License','FIBA',                       '2020-10-01', '2026-10-01', false],
            [10,'STA Baby Swimming',    'STA',                        '2023-07-01', '2025-07-01', true],
            [11,'ITF Coaching Level 2', 'ITF',                        '2022-01-15', '2027-01-15', true],
        ];
        foreach ($qualifications as [$idx, $name, $issuer, $issued, $expiry, $verified]) {
            DB::table('trainer_qualifications')->insert([
                'trainer_id' => $trainerIds[$idx],
                'name' => $name,
                'issuer' => $issuer,
                'issue_date' => $issued,
                'expiry_date' => $expiry,
                'is_verified' => $verified,
                'verified_by' => $verified ? $userId : null,
                'verified_at' => $verified ? $now : null,
                'created_at' => $now,
            ]);
        }

        // ─── Trainer Availability ───
        foreach ($trainerIds as $i => $tId) {
            foreach ([0, 1, 2, 3, 4] as $day) {
                DB::table('trainer_availability')->insert([
                    'trainer_id' => $tId,
                    'type' => 'available',
                    'day_of_week' => $day,
                    'start_time' => $i < 6 ? '08:00' : '14:00',
                    'end_time' => $i < 6 ? '16:00' : '22:00',
                    'created_at' => $now,
                ]);
            }
            if ($i % 3 === 0) {
                DB::table('trainer_availability')->insert([
                    'trainer_id' => $tId,
                    'type' => 'preferred',
                    'day_of_week' => 5,
                    'start_time' => '16:00',
                    'end_time' => '20:00',
                    'created_at' => $now,
                ]);
            }
            if ($i % 4 === 0) {
                DB::table('trainer_availability')->insert([
                    'trainer_id' => $tId,
                    'type' => 'unavailable',
                    'specific_date' => '2026-07-15',
                    'start_time' => '08:00',
                    'end_time' => '22:00',
                    'reason' => 'إجازة شخصية',
                    'created_at' => $now,
                ]);
            }
        }

        // ─── Trainer Compensations (3 months history) ───
        $compensationData = [];
        foreach ($trainerIds as $i => $tId) {
            $td = $trainerDefs[$i];
            $model = $td['model'];

            for ($monthsAgo = 2; $monthsAgo >= 0; $monthsAgo--) {
                $month = now()->subMonths($monthsAgo);
                $sessions = rand(12, 24);

                for ($s = 0; $s < $sessions; $s++) {
                    $day = rand(1, min(28, $month->daysInMonth));
                    $date = $month->copy()->day($day)->toDateString();

                    $type = match ($model) {
                        'per_session', 'hybrid' => 'session_pay',
                        'per_group' => 'group_pay',
                        'per_player' => 'player_pay',
                        'hourly' => 'session_pay',
                        default => 'session_pay',
                    };

                    $rate = match ($model) {
                        'hourly' => $td['hourly'] ?? 12000,
                        'per_group' => $td['group'] ?? 20000,
                        'per_player' => $td['player'] ?? 5000,
                        default => $td['session'] ?? 15000,
                    };

                    $quantity = $model === 'per_player' ? rand(5, 15) : 1;
                    $amount = (int) ($rate * $quantity);
                    $status = $monthsAgo > 0 ? 'paid' : (rand(0, 3) === 0 ? 'pending' : 'approved');

                    $compensationData[] = [
                        'uuid' => Str::uuid(),
                        'academy_id' => $academyId,
                        'trainer_id' => $tId,
                        'date' => $date,
                        'type' => $type,
                        'description' => "حصة تدريبية — {$date}",
                        'quantity' => $quantity,
                        'rate' => $rate,
                        'amount' => $amount,
                        'status' => $status,
                        'approved_by' => $status !== 'pending' ? $userId : null,
                        'approved_at' => $status !== 'pending' ? $now : null,
                        'created_by' => $userId,
                        'created_at' => $now,
                        'updated_at' => $now,
                    ];
                }

                // Bonus for some
                if ($i % 3 === 0 && $monthsAgo === 1) {
                    $compensationData[] = [
                        'uuid' => Str::uuid(),
                        'academy_id' => $academyId,
                        'trainer_id' => $tId,
                        'date' => $month->copy()->day(28)->toDateString(),
                        'type' => 'bonus',
                        'description' => 'مكافأة أداء متميز',
                        'quantity' => 1,
                        'rate' => 50000,
                        'amount' => 50000,
                        'status' => 'paid',
                        'approved_by' => $userId,
                        'approved_at' => $now,
                        'created_by' => $userId,
                        'created_at' => $now,
                        'updated_at' => $now,
                    ];
                }

                // Penalty for trainer 7 (basketball)
                if ($i === 7 && $monthsAgo === 0) {
                    $compensationData[] = [
                        'uuid' => Str::uuid(),
                        'academy_id' => $academyId,
                        'trainer_id' => $tId,
                        'date' => $month->copy()->day(5)->toDateString(),
                        'type' => 'penalty',
                        'description' => 'خصم تأخير — 3 مرات في الأسبوع',
                        'quantity' => 1,
                        'rate' => 10000,
                        'amount' => 10000,
                        'status' => 'approved',
                        'approved_by' => $userId,
                        'approved_at' => $now,
                        'created_by' => $userId,
                        'created_at' => $now,
                        'updated_at' => $now,
                    ];
                }
            }
        }

        foreach (array_chunk($compensationData, 100) as $chunk) {
            DB::table('trainer_compensations')->insert($chunk);
        }

        // ─── Payroll Periods ───
        $periods = [];
        for ($m = 2; $m >= 0; $m--) {
            $start = now()->subMonths($m)->startOfMonth()->toDateString();
            $end = now()->subMonths($m)->endOfMonth()->toDateString();
            $status = $m > 0 ? 'closed' : 'open';

            $periods[$m] = DB::table('payroll_periods')->insertGetId([
                'uuid' => Str::uuid(),
                'academy_id' => $academyId,
                'period_start' => $start,
                'period_end' => $end,
                'status' => $status,
                'total_gross' => 0,
                'total_deductions' => 0,
                'total_net' => 0,
                'payslip_count' => 0,
                'approved_by' => $m > 0 ? $userId : null,
                'approved_at' => $m > 0 ? $now : null,
                'closed_at' => $m > 0 ? $now : null,
                'created_by' => $userId,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        }

        // ─── Payslips for closed periods ───
        $payslipNum = 1;
        foreach ([2, 1] as $m) {
            $periodId = $periods[$m];
            $periodGross = 0;
            $periodDed = 0;
            $periodNet = 0;
            $slipCount = 0;

            foreach ($trainerIds as $i => $tId) {
                $td = $trainerDefs[$i];
                $empId = $td['emp'] ? $employeeIds[$td['emp']] : null;
                $baseSalary = 0;
                if ($empId) {
                    $emp = DB::table('employees')->where('id', $empId)->first();
                    $baseSalary = $emp->salary_amount ?? 0;
                }

                $month = now()->subMonths($m);
                $monthStart = $month->copy()->startOfMonth()->toDateString();
                $monthEnd = $month->copy()->endOfMonth()->toDateString();

                $sessionEarnings = (int) DB::table('trainer_compensations')
                    ->where('trainer_id', $tId)
                    ->where('status', 'paid')
                    ->whereIn('type', ['session_pay', 'group_pay', 'player_pay'])
                    ->whereBetween('date', [$monthStart, $monthEnd])
                    ->sum('amount');

                $bonuses = (int) DB::table('trainer_compensations')
                    ->where('trainer_id', $tId)->where('status', 'paid')
                    ->where('type', 'bonus')
                    ->whereBetween('date', [$monthStart, $monthEnd])
                    ->sum('amount');

                $penalties = (int) DB::table('trainer_compensations')
                    ->where('trainer_id', $tId)
                    ->whereIn('status', ['approved', 'paid'])
                    ->where('type', 'penalty')
                    ->whereBetween('date', [$monthStart, $monthEnd])
                    ->sum('amount');

                $gross = $baseSalary + $sessionEarnings + $bonuses;
                $tax = (int) ($gross * 0.05);
                $insurance = $empId ? 50000 : 0;
                $totalDed = $penalties + $tax + $insurance;
                $net = max(0, $gross - $totalDed);

                $payslipId = DB::table('payslips')->insertGetId([
                    'uuid' => Str::uuid(),
                    'academy_id' => $academyId,
                    'trainer_id' => $tId,
                    'payroll_period_id' => $periodId,
                    'payslip_number' => 'PS-' . str_pad($payslipNum++, 4, '0', STR_PAD_LEFT),
                    'base_amount' => $baseSalary,
                    'session_earnings' => $sessionEarnings,
                    'bonuses' => $bonuses,
                    'penalties' => $penalties,
                    'advances_deducted' => 0,
                    'tax_amount' => $tax,
                    'insurance_amount' => $insurance,
                    'other_deductions' => 0,
                    'gross_amount' => $gross,
                    'total_deductions' => $totalDed,
                    'net_amount' => $net,
                    'status' => 'paid',
                    'approved_by' => $userId,
                    'approved_at' => $now,
                    'paid_at' => $now,
                    'payment_method' => $empId ? 'bank_transfer' : 'cash',
                    'payment_reference' => $empId ? 'TRF-' . $month->format('Ym') . '-' . ($i + 1) : null,
                    'created_by' => $userId,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]);

                $items = [];
                $sort = 0;
                if ($baseSalary > 0) {
                    $items[] = ['payslip_id' => $payslipId, 'type' => 'base_salary', 'description' => 'الراتب الأساسي', 'quantity' => 1, 'rate' => $baseSalary, 'amount' => $baseSalary, 'is_deduction' => false, 'metadata' => '{}', 'sort_order' => $sort++, 'created_at' => $now, 'updated_at' => $now];
                }
                if ($sessionEarnings > 0) {
                    $items[] = ['payslip_id' => $payslipId, 'type' => 'session_pay', 'description' => 'أجر الحصص', 'quantity' => 1, 'rate' => $sessionEarnings, 'amount' => $sessionEarnings, 'is_deduction' => false, 'metadata' => '{}', 'sort_order' => $sort++, 'created_at' => $now, 'updated_at' => $now];
                }
                if ($bonuses > 0) {
                    $items[] = ['payslip_id' => $payslipId, 'type' => 'bonus', 'description' => 'مكافآت', 'quantity' => 1, 'rate' => $bonuses, 'amount' => $bonuses, 'is_deduction' => false, 'metadata' => '{}', 'sort_order' => $sort++, 'created_at' => $now, 'updated_at' => $now];
                }
                if ($penalties > 0) {
                    $items[] = ['payslip_id' => $payslipId, 'type' => 'penalty', 'description' => 'خصومات', 'quantity' => 1, 'rate' => $penalties, 'amount' => $penalties, 'is_deduction' => true, 'metadata' => '{}', 'sort_order' => $sort++, 'created_at' => $now, 'updated_at' => $now];
                }
                if ($tax > 0) {
                    $items[] = ['payslip_id' => $payslipId, 'type' => 'tax', 'description' => 'ضريبة دخل 5%', 'quantity' => 1, 'rate' => $tax, 'amount' => $tax, 'is_deduction' => true, 'metadata' => '{}', 'sort_order' => $sort++, 'created_at' => $now, 'updated_at' => $now];
                }
                if ($insurance > 0) {
                    $items[] = ['payslip_id' => $payslipId, 'type' => 'insurance', 'description' => 'تأمين اجتماعي', 'quantity' => 1, 'rate' => $insurance, 'amount' => $insurance, 'is_deduction' => true, 'metadata' => '{}', 'sort_order' => $sort++, 'created_at' => $now, 'updated_at' => $now];
                }
                if ($items) {
                    DB::table('payslip_items')->insert($items);
                }

                $periodGross += $gross;
                $periodDed += $totalDed;
                $periodNet += $net;
                $slipCount++;
            }

            DB::table('payroll_periods')->where('id', $periodId)->update([
                'total_gross' => $periodGross,
                'total_deductions' => $periodDed,
                'total_net' => $periodNet,
                'payslip_count' => $slipCount,
            ]);
        }

        // ─── Trainer Advances ───
        $advances = [
            ['idx' => 0, 'amount' => 500000, 'rem' => 300000, 'inst' => 100000, 'cnt' => 5, 'paid' => 2, 'status' => 'active',         'date' => '2026-05-01', 'reason' => 'سلفة شخصية — مصاريف طبية'],
            ['idx' => 3, 'amount' => 300000, 'rem' => 200000, 'inst' => 100000, 'cnt' => 3, 'paid' => 1, 'status' => 'active',         'date' => '2026-06-01', 'reason' => 'سلفة — مصاريف سفر'],
            ['idx' => 5, 'amount' => 200000, 'rem' => 0,      'inst' => 100000, 'cnt' => 2, 'paid' => 2, 'status' => 'fully_deducted', 'date' => '2026-03-01', 'reason' => 'سلفة شخصية'],
            ['idx' => 7, 'amount' => 400000, 'rem' => 300000, 'inst' => 100000, 'cnt' => 4, 'paid' => 1, 'status' => 'paused',         'date' => '2026-04-01', 'reason' => 'سلفة طوارئ'],
            ['idx' => 11,'amount' => 150000, 'rem' => 150000, 'inst' => 75000,  'cnt' => 2, 'paid' => 0, 'status' => 'cancelled',      'date' => '2026-06-15', 'reason' => 'تم إلغاء الطلب بناءً على رغبة المدرب'],
        ];

        foreach ($advances as $adv) {
            DB::table('trainer_advances')->insert([
                'uuid' => Str::uuid(),
                'academy_id' => $academyId,
                'trainer_id' => $trainerIds[$adv['idx']],
                'amount' => $adv['amount'],
                'remaining_balance' => $adv['rem'],
                'installment_amount' => $adv['inst'],
                'installments_count' => $adv['cnt'],
                'installments_paid' => $adv['paid'],
                'reason' => $adv['reason'],
                'status' => $adv['status'],
                'issued_date' => $adv['date'],
                'expected_completion_date' => now()->addMonths($adv['cnt'] - $adv['paid'])->toDateString(),
                'approved_by' => $userId,
                'created_by' => $userId,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        }

        // ─── Rate History ───
        $rateChanges = [
            [0, 'session_rate', '12000', '15000', '2026-01-01', 'زيادة سنوية'],
            [2, 'hourly_rate',  '12000', '15000', '2026-04-01', 'ترقية بعد 6 أشهر'],
            [5, 'group_rate',   '20000', '25000', '2026-03-01', 'ترقية لرئيس مدربين'],
            [11,'player_rate',  '4000',  '5000',  '2026-05-01', 'زيادة بناءً على الأداء'],
            [3, 'session_rate', '15000', '18000', '2026-02-01', 'حصول على شهادة جديدة'],
        ];
        foreach ($rateChanges as [$idx, $field, $old, $new, $date, $notes]) {
            DB::table('trainer_rate_history')->insert([
                'academy_id' => $academyId,
                'trainer_id' => $trainerIds[$idx],
                'field' => $field,
                'old_value' => $old,
                'new_value' => $new,
                'effective_from' => $date,
                'changed_by' => $userId,
                'notes' => $notes,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        }

        $this->command?->info('HR Demo data seeded successfully!');
        $this->command?->info("- 11 employees (6 staff + 4 trainer-employees + 1 resigned, 1 on-leave)");
        $this->command?->info("- 12 trainers (4 employee-based + 8 freelancers)");
        $this->command?->info("- 12 qualifications, availability for all");
        $this->command?->info("- ~600+ compensation records (3 months)");
        $this->command?->info("- 3 payroll periods (2 closed with payslips, 1 open)");
        $this->command?->info("- 5 advances (active, paused, fully_deducted, cancelled)");
        $this->command?->info("- 5 rate history records");
        $this->command?->info("- Payroll system ENABLED");
    }
}
