<?php

namespace Database\Seeders;

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

class AuditLogSeeder extends Seeder
{
    public function run(): void
    {
        $admin = User::where('email', 'admin@smkn1gunungagung.sch.id')->first();
        $superAdmin = User::where('email', 'superadmin@smkn1gunungagung.sch.id')->first();
        $kepsek = User::where('email', 'kepsek@smkn1gunungagung.sch.id')->first();

        $logs = [
            [
                'user' => $superAdmin,
                'action' => 'login',
                'description' => 'Super Admin berhasil masuk ke sistem',
                'hours_ago' => 48,
            ],
            [
                'user' => $admin,
                'action' => 'login',
                'description' => 'Admin TU berhasil masuk ke sistem',
                'hours_ago' => 8,
            ],
            [
                'user' => $admin,
                'action' => 'user.create',
                'description' => 'Menambah data siswa baru melalui form manual',
                'hours_ago' => 7,
            ],
            [
                'user' => $admin,
                'action' => 'machine.sync',
                'description' => 'Menjalankan sinkronisasi mesin fingerprint Lobby Utama',
                'hours_ago' => 6,
            ],
            [
                'user' => $admin,
                'action' => 'leave.approve',
                'description' => 'Menyetujui pengajuan izin siswa',
                'hours_ago' => 5,
            ],
            [
                'user' => $admin,
                'action' => 'warning_letter.create',
                'description' => 'Menerbitkan Surat Peringatan 1 (SP1)',
                'hours_ago' => 4,
            ],
            [
                'user' => $kepsek,
                'action' => 'login',
                'description' => 'Kepala Sekolah berhasil masuk ke sistem',
                'hours_ago' => 3,
            ],
            [
                'user' => $kepsek,
                'action' => 'report.view',
                'description' => 'Melihat laporan rekap kehadiran bulanan',
                'hours_ago' => 2,
            ],
            [
                'user' => $admin,
                'action' => 'setting.update',
                'description' => 'Memperbarui pengaturan identitas sekolah',
                'hours_ago' => 1,
            ],
            [
                'user' => $admin,
                'action' => 'backup.create',
                'description' => 'Membuat backup database manual',
                'hours_ago' => 0.5,
            ],
        ];

        foreach ($logs as $log) {
            if (!$log['user']) {
                continue;
            }

            $at = now()->subHours($log['hours_ago']);

            AuditLog::updateOrCreate(
                [
                    'user_id' => $log['user']->id,
                    'action' => $log['action'],
                    'description' => $log['description'],
                ],
                [
                    'ip_address' => '192.168.1.' . rand(10, 50),
                    'user_agent' => 'Mozilla/5.0 (X11; Linux x86_64) Chrome/138.0.0.0',
                    'created_at' => $at,
                    'updated_at' => $at,
                ]
            );
        }
    }
}
