<?php

namespace Database\Seeders;

use App\Models\Attendance;
use App\Models\Employee;
use App\Models\OfficeLocation;
use Carbon\Carbon;
use Illuminate\Database\Seeder;

class AttendanceSeeder extends Seeder
{
    public function run(): void
    {
        $office = OfficeLocation::first();
        $employees = Employee::with('schedules.shift')->get();

        if ($employees->isEmpty() || !$office) {
            return;
        }

        $today = Carbon::today();

        // Seed attendance for today
        foreach ($employees as $index => $employee) {
            $shift = $employee->currentSchedule()?->shift;
            $shiftId = $shift?->id;

            // Scenario distribution for today:
            // 0..4: Hadir tepat waktu
            // 5..7: Terlambat
            // 8: Izin
            // 9: Belum absen (akan tampil sebagai 'Tidak Hadir' / belum check-in)
            if ($index < 5) {
                Attendance::updateOrCreate(
                    [
                        'employee_id' => $employee->id,
                        'attendance_date' => $today->toDateString(),
                    ],
                    [
                        'shift_id' => $shiftId,
                        'office_location_id' => $office->id,
                        'check_in' => '07:'.str_pad(45 + $index * 2, 2, '0', STR_PAD_LEFT).':00',
                        'check_out' => null,
                        'status' => 'present',
                        'late_minutes' => 0,
                        'latitude_in' => $office->latitude + 0.0001,
                        'longitude_in' => $office->longitude + 0.0001,
                        'distance_in' => 25.5 + ($index * 5),
                        'photo_in' => $employee->photo,
                        'notes' => 'Absensi masuk tepat waktu',
                    ]
                );
            } elseif ($index < 8) {
                $lateMins = ($index - 4) * 12; // 12, 24, 36 mins
                Attendance::updateOrCreate(
                    [
                        'employee_id' => $employee->id,
                        'attendance_date' => $today->toDateString(),
                    ],
                    [
                        'shift_id' => $shiftId,
                        'office_location_id' => $office->id,
                        'check_in' => '08:'.str_pad(10 + $lateMins, 2, '0', STR_PAD_LEFT).':00',
                        'check_out' => null,
                        'status' => 'late',
                        'late_minutes' => $lateMins,
                        'latitude_in' => $office->latitude + 0.0002,
                        'longitude_in' => $office->longitude + 0.0002,
                        'distance_in' => 45.0,
                        'photo_in' => $employee->photo,
                        'notes' => 'Terlambat karena macet',
                    ]
                );
            } elseif ($index == 8) {
                Attendance::updateOrCreate(
                    [
                        'employee_id' => $employee->id,
                        'attendance_date' => $today->toDateString(),
                    ],
                    [
                        'shift_id' => $shiftId,
                        'office_location_id' => $office->id,
                        'check_in' => null,
                        'check_out' => null,
                        'status' => 'permit',
                        'late_minutes' => 0,
                        'notes' => 'Izin keperluan keluarga',
                    ]
                );
            }
        }
    }
}
