<?php

namespace Database\Seeders;

use App\Models\Company;
use App\Models\User;
use Illuminate\Database\Seeder;

class EmployeeSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * Seeder untuk menambahkan karyawan: Fitri, Yusuf, Suefi, Tommy, Syahril
     */
    public function run(): void
    {
        // Ambil company pertama (atau buat baru jika tidak ada)
        $company = Company::first();

        if (!$company) {
            $this->command->error('Tidak ada company di database. Jalankan DatabaseSeeder terlebih dahulu.');
            return;
        }

        $employees = [
            [
                'employee_id' => 'EMP002',
                'nip' => '199501012020011001',
                'name' => 'Fitri',
                'email' => 'fitri@contohperusahaan.com',
                'phone' => '08123456792',
                'department' => 'Operasional',
                'position' => 'Staff',
            ],
            [
                'employee_id' => 'EMP003',
                'nip' => '199601012020011001',
                'name' => 'Yusuf',
                'email' => 'yusuf@contohperusahaan.com',
                'phone' => '08123456793',
                'department' => 'Operasional',
                'position' => 'Staff',
            ],
            [
                'employee_id' => 'EMP004',
                'nip' => '199701012020011001',
                'name' => 'Suefi',
                'email' => 'suefi@contohperusahaan.com',
                'phone' => '08123456794',
                'department' => 'Operasional',
                'position' => 'Staff',
            ],
            [
                'employee_id' => 'EMP005',
                'nip' => '199801012020011001',
                'name' => 'Tommy',
                'email' => 'tommy@contohperusahaan.com',
                'phone' => '08123456795',
                'department' => 'Operasional',
                'position' => 'Staff',
            ],
            [
                'employee_id' => 'EMP006',
                'nip' => '199901012020011001',
                'name' => 'Syahril',
                'email' => 'syahril@contohperusahaan.com',
                'phone' => '08123456796',
                'department' => 'IT',
                'position' => 'Developer',
            ],
        ];

        foreach ($employees as $employeeData) {
            // Skip jika user dengan email sudah ada
            if (User::where('email', $employeeData['email'])->exists()) {
                $this->command->info("User {$employeeData['name']} sudah ada, dilewati.");
                continue;
            }

            $user = User::create([
                'company_id' => $company->id,
                'employee_id' => $employeeData['employee_id'],
                'nip' => $employeeData['nip'],
                'name' => $employeeData['name'],
                'email' => $employeeData['email'],
                'password' => 'password',
                'phone' => $employeeData['phone'],
                'role' => 'employee',
                'department' => $employeeData['department'],
                'position' => $employeeData['position'],
                'address' => 'Jakarta',
                'home_latitude' => -6.2000 + (rand(-100, 100) / 1000),
                'home_longitude' => 106.8500 + (rand(-100, 100) / 1000),
                'is_active' => true,
                'approval_status' => 'approved',
            ]);

            $user->assignRole('employee');

            $this->command->info("User {$employeeData['name']} berhasil dibuat.");
        }

        $this->command->info('EmployeeSeeder selesai.');
    }
}
