<?php

namespace Database\Seeders;

use App\Models\Attendance;
use App\Models\Pengajuan;
use App\Models\User;
use Illuminate\Database\Seeder;

class PengajuanSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $user = User::query()->first();
        if (!$user) {
            $this->command->info('No users found. Skip PengajuanSeeder.');
            return;
        }

        $attendance = Attendance::query()
            ->where('user_id', $user->id)
            ->latest('date')
            ->first();

        if (!$attendance) {
            $attendance = Attendance::query()->create([
                'user_id' => $user->id,
                'date' => now()->toDateString(),
                'check_in' => now()->copy()->setTime(8, 0, 0),
                'check_out' => now()->copy()->setTime(16, 0, 0),
                'status' => 'hadir',
                'notes' => null,
                'device_info' => 'Seeder',
                'location' => null,
            ]);
        }

        Pengajuan::query()->insert([
            [
                'id_user' => $user->id,
                'id_attendance' => $attendance->id,
                'date' => now()->toDateString(),
                'keterangan' => 'Izin pulang lebih awal untuk keperluan keluarga',
                'url_bukti' => 'pengajuan/image.png',
                'status' => 'diajukan',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'id_user' => $user->id,
                'id_attendance' => $attendance->id,
                'date' => now()->subDay()->toDateString(),
                'keterangan' => 'Pengajuan perbaikan jam masuk karena terlambat',
                'url_bukti' => 'pengajuan/image.png',
                'status' => 'disetujui',
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
        $this->command->info('Pengajuan data seeded successfully!');
    }
}
