<?php

namespace Database\Seeders;

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

class EntityHistorySeeder extends Seeder
{
    public function run(): void
    {
        $admin = User::where('email', 'admin@smkn1gunungagung.sch.id')->first();
        $sampleStudent = User::whereHas('role', fn ($q) => $q->where('name', 'siswa'))
            ->where('is_active', true)
            ->first();
        $sampleTeacher = User::whereHas('role', fn ($q) => $q->where('name', 'guru'))->first();

        if (!$admin) {
            return;
        }

        $histories = [];

        if ($sampleStudent) {
            $histories[] = [
                'user_id' => $admin->id,
                'entity_type' => User::class,
                'entity_id' => $sampleStudent->id,
                'action' => 'created',
                'before_values' => null,
                'after_values' => [
                    'name' => $sampleStudent->name,
                    'nip_nis' => $sampleStudent->nip_nis,
                    'class_name' => $sampleStudent->class_name,
                    'email' => $sampleStudent->email,
                ],
                'created_at' => now()->subDays(10),
            ];

            $histories[] = [
                'user_id' => $admin->id,
                'entity_type' => User::class,
                'entity_id' => $sampleStudent->id,
                'action' => 'updated',
                'before_values' => [
                    'phone_number' => null,
                ],
                'after_values' => [
                    'phone_number' => $sampleStudent->phone_number,
                ],
                'created_at' => now()->subDays(3),
            ];
        }

        if ($sampleTeacher) {
            $histories[] = [
                'user_id' => $admin->id,
                'entity_type' => User::class,
                'entity_id' => $sampleTeacher->id,
                'action' => 'updated',
                'before_values' => [
                    'is_active' => false,
                ],
                'after_values' => [
                    'is_active' => true,
                ],
                'created_at' => now()->subDays(5),
            ];
        }

        $alumni = User::where('email', 'alumni.demo@siswa.smkn1gunungagung.sch.id')->first();
        if ($alumni) {
            $histories[] = [
                'user_id' => $admin->id,
                'entity_type' => User::class,
                'entity_id' => $alumni->id,
                'action' => 'updated',
                'before_values' => [
                    'student_status' => 'aktif',
                    'is_active' => true,
                ],
                'after_values' => [
                    'student_status' => 'lulus',
                    'is_active' => false,
                ],
                'created_at' => now()->subDays(20),
            ];
        }

        foreach ($histories as $history) {
            EntityHistory::updateOrCreate(
                [
                    'entity_type' => $history['entity_type'],
                    'entity_id' => $history['entity_id'],
                    'action' => $history['action'],
                    'user_id' => $history['user_id'],
                ],
                [
                    'before_values' => $history['before_values'],
                    'after_values' => $history['after_values'],
                    'created_at' => $history['created_at'],
                    'updated_at' => $history['created_at'],
                ]
            );
        }
    }
}
