<?php

namespace Database\Seeders;

use App\Models\Attendance;
use App\Models\Employee;
use App\Models\LeaveRequest;
use App\Models\User;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        User::updateOrCreate(['email' => 'admin@ish.co.id'], [
            'name' => 'Andi Ramadhan',
            'password' => bcrypt('password'),
            'role' => 'admin',
        ]);

        User::where('email', 'pegawai@ish.co.id')->delete();

        $employees = [
            ['employee_code' => 'ISH-24001', 'full_name' => 'Dina Permata', 'email' => 'dina.permata@ish.co.id', 'phone' => '081234567801', 'position' => 'Customer Service', 'department' => 'Customer Service', 'join_date' => '2024-01-02'],
            ['employee_code' => 'ISH-24002', 'full_name' => 'Rizky Fajar', 'email' => 'rizky.fajar@ish.co.id', 'phone' => '081234567802', 'position' => 'IT Support', 'department' => 'IT Support', 'join_date' => '2024-01-02'],
            ['employee_code' => 'ISH-24003', 'full_name' => 'Siti Aisyah', 'email' => 'siti.aisyah@ish.co.id', 'phone' => '081234567803', 'position' => 'Finance Staff', 'department' => 'Finance', 'join_date' => '2024-02-12'],
            ['employee_code' => 'ISH-24004', 'full_name' => 'Budi Maulana', 'email' => 'budi.maulana@ish.co.id', 'phone' => '081234567804', 'position' => 'Operations Staff', 'department' => 'Operations', 'join_date' => '2024-03-04'],
        ];

        $passwords = ['ISH-24001' => 'ish24001pass', 'ISH-24002' => 'ish24002pass', 'ISH-24003' => 'ish24003pass', 'ISH-24004' => 'ish24004pass'];
        foreach ($employees as $data) {
            $user = User::updateOrCreate(['email' => $data['email']], ['name' => $data['full_name'], 'password' => bcrypt($passwords[$data['employee_code']]), 'role' => 'employee']);
            $data['user_id'] = $user->id;
            $employee = Employee::updateOrCreate(['employee_code' => $data['employee_code']], $data);
            Attendance::updateOrCreate(
                ['employee_id' => $employee->id, 'attendance_date' => today()],
                ['check_in' => now()->setTime(7, 55), 'check_out' => now()->setTime(17, 2), 'photo_path' => 'attendance/demo-selfie.jpg', 'latitude' => 3.595196, 'longitude' => 98.672226, 'location_name' => 'Kantor ISH Medan', 'status' => $employee->employee_code === 'ISH-24002' ? 'late' : 'present']
            );
        }

        LeaveRequest::firstOrCreate(
            ['employee_id' => Employee::where('employee_code', 'ISH-24003')->value('id'), 'start_date' => today()->addDay()->toDateString(), 'end_date' => today()->addDay()->toDateString()],
            ['type' => 'sick', 'reason' => 'Demam dan perlu istirahat sesuai surat dokter.', 'attachment' => 'leave-attachments/demo-surat-sakit.pdf', 'status' => 'pending']
        );

        LeaveRequest::firstOrCreate(
            ['employee_id' => Employee::where('employee_code', 'ISH-24004')->value('id'), 'start_date' => today()->addDays(3)->toDateString(), 'end_date' => today()->addDays(5)->toDateString()],
            ['type' => 'annual', 'reason' => 'Keperluan keluarga.', 'status' => 'approved', 'review_note' => 'Disetujui admin.']
        );
    }
}
