<?php

namespace Database\Seeders;

use App\Models\Employee;
use App\Models\Salary;
use Carbon\Carbon;
use Illuminate\Database\Seeder;

class EmployeeSalarySeeder extends Seeder
{
    public function run(): void
    {
        $this->command->info('🚀 Memulai seeding Employee & Salary...');
        
        $this->createEmployees();
        $this->createSalaries();
        
        $this->command->info('✅ Seeding Employee & Salary selesai!');
    }

    private function createEmployees(): void
    {
        $employees = [
            [
                'employee_id' => 'KRY001',
                'name' => 'Ahmad Rizal Pratama',
                'gender' => 'L',
                'phone' => '0812-3456-7890',
                'email' => 'ahmad.rizal@posgraf.com',
                'address' => 'Jl. Merdeka No. 123, Jakarta Selatan',
                'position' => 'Manager Operasional',
                'join_date' => '2020-01-15',
                'base_salary' => 8500000,
                'status' => 'active',
            ],
            [
                'employee_id' => 'KRY002',
                'name' => 'Siti Nurhaliza',
                'gender' => 'P',
                'phone' => '0813-2345-6789',
                'email' => 'siti.nurhaliza@posgraf.com',
                'address' => 'Jl. Kebon Jeruk No. 45, Jakarta Barat',
                'position' => 'Designer Senior',
                'join_date' => '2021-03-01',
                'base_salary' => 6000000,
                'status' => 'active',
            ],
            [
                'employee_id' => 'KRY003',
                'name' => 'Budi Santoso',
                'gender' => 'L',
                'phone' => '0857-3456-7890',
                'email' => 'budi.santoso@posgraf.com',
                'address' => 'Jl. Sudirman No. 78, Jakarta Pusat',
                'position' => 'Operator Mesin Cetak',
                'join_date' => '2022-06-15',
                'base_salary' => 4800000,
                'status' => 'active',
            ],
            [
                'employee_id' => 'KRY004',
                'name' => 'Dewi Lestari',
                'gender' => 'P',
                'phone' => '0821-4567-8901',
                'email' => 'dewi.lestari@posgraf.com',
                'address' => 'Jl. Gatot Subroto No. 99, Jakarta Selatan',
                'position' => 'Kasir',
                'join_date' => '2023-01-10',
                'base_salary' => 4200000,
                'status' => 'active',
            ],
            [
                'employee_id' => 'KRY005',
                'name' => 'Rudi Hermawan',
                'gender' => 'L',
                'phone' => '0852-5678-9012',
                'email' => 'rudi.hermawan@posgraf.com',
                'address' => 'Jl. Veteran No. 50, Bekasi',
                'position' => 'Operator Finishing',
                'join_date' => '2021-08-20',
                'base_salary' => 4500000,
                'status' => 'active',
            ],
            [
                'employee_id' => 'KRY006',
                'name' => 'Putri Andini',
                'gender' => 'P',
                'phone' => '0878-6789-0123',
                'email' => 'putri.andini@posgraf.com',
                'address' => 'Jl. Imam Bonjol No. 15, Tangerang',
                'position' => 'Designer Junior',
                'join_date' => '2024-02-01',
                'base_salary' => 4000000,
                'status' => 'active',
            ],
            [
                'employee_id' => 'KRY007',
                'name' => 'Agus Setiawan',
                'gender' => 'L',
                'phone' => '0858-7890-1234',
                'email' => 'agus.setiawan@posgraf.com',
                'address' => 'Jl. Ahmad Yani No. 200, Depok',
                'position' => 'Operator Mesin Cetak',
                'join_date' => '2023-07-15',
                'base_salary' => 4500000,
                'status' => 'active',
            ],
            [
                'employee_id' => 'KRY008',
                'name' => 'Rina Marlina',
                'gender' => 'P',
                'phone' => '0877-8901-2345',
                'email' => 'rina.marlina@posgraf.com',
                'address' => 'Jl. Diponegoro No. 33, Bogor',
                'position' => 'Admin & Keuangan',
                'join_date' => '2022-11-01',
                'base_salary' => 4500000,
                'status' => 'active',
            ],
            [
                'employee_id' => 'KRY009',
                'name' => 'Hendra Wijaya',
                'gender' => 'L',
                'phone' => '0822-9012-3456',
                'email' => 'hendra.wijaya@posgraf.com',
                'address' => 'Jl. Pahlawan No. 77, Jakarta Timur',
                'position' => 'Kurir/Delivery',
                'join_date' => '2024-06-01',
                'base_salary' => 3800000,
                'status' => 'active',
            ],
            [
                'employee_id' => 'KRY010',
                'name' => 'Bambang Susilo',
                'gender' => 'L',
                'phone' => '0853-0123-4567',
                'email' => 'bambang.susilo@posgraf.com',
                'address' => 'Jl. Raya Bogor No. 150, Jakarta Timur',
                'position' => 'Operator Mesin Cetak',
                'join_date' => '2020-09-15',
                'base_salary' => 4800000,
                'status' => 'inactive',
            ],
        ];

        foreach ($employees as $data) {
            Employee::firstOrCreate(
                ['employee_id' => $data['employee_id']],
                $data
            );
        }
        
        $this->command->info('  👥 Employees seeded: ' . count($employees));
    }

    private function createSalaries(): void
    {
        $activeEmployees = Employee::where('status', 'active')->get();
        
        // Periods: December 2025, January 2026, February 2026
        $periods = [
            ['period' => '2025-12', 'status' => 'paid', 'paid_date' => Carbon::create(2025, 12, 28)],
            ['period' => '2026-01', 'status' => 'paid', 'paid_date' => Carbon::create(2026, 1, 28)],
            ['period' => '2026-02', 'status' => 'draft', 'paid_date' => null],
        ];

        $totalSalaries = 0;
        
        foreach ($activeEmployees as $employee) {
            foreach ($periods as $periodData) {
                // Randomize components
                $allowance = rand(0, 1) ? rand(3, 8) * 100000 : 0; // 300k-800k atau 0
                $overtime = rand(0, 1) ? rand(1, 5) * 100000 : 0; // 100k-500k lembur
                
                // Bonus: lebih tinggi di akhir tahun (Desember) dan THR kemungkinan
                $bonus = 0;
                if ($periodData['period'] === '2025-12' && rand(0, 2) === 0) {
                    $bonus = rand(5, 20) * 100000; // Bonus akhir tahun
                } elseif (rand(0, 5) === 0) {
                    $bonus = rand(2, 10) * 100000;
                }
                
                // Deduction: kasbon, keterlambatan, dll
                $deduction = rand(0, 4) === 0 ? rand(1, 3) * 50000 : 0;
                
                $total = $employee->base_salary + $allowance + $overtime + $bonus - $deduction;
                
                // Notes based on components
                $notes = [];
                if ($bonus > 0 && $periodData['period'] === '2025-12') {
                    $notes[] = 'Bonus akhir tahun';
                } elseif ($bonus > 0) {
                    $notes[] = 'Bonus performa';
                }
                if ($overtime > 0) {
                    $notes[] = 'Termasuk lembur ' . rand(5, 15) . ' jam';
                }
                if ($deduction > 0) {
                    $notes[] = 'Potongan kasbon';
                }

                Salary::firstOrCreate(
                    [
                        'employee_id' => $employee->id,
                        'period' => $periodData['period'],
                    ],
                    [
                        'base_salary' => $employee->base_salary,
                        'allowance' => $allowance,
                        'overtime' => $overtime,
                        'bonus' => $bonus,
                        'deduction' => $deduction,
                        'total' => $total,
                        'status' => $periodData['status'],
                        'paid_at' => $periodData['paid_date'],
                        'notes' => !empty($notes) ? implode('. ', $notes) : null,
                    ]
                );
                
                $totalSalaries++;
            }
        }
        
        $this->command->info('  💰 Salaries seeded: ' . $totalSalaries . ' records for ' . $activeEmployees->count() . ' employees');
    }
}
