<?php

namespace App\Services;

use App\Models\AuditLog;
use Illuminate\Http\Request;

class AuditLogService
{
    public function record(string $action, ?object $entity = null, array $old = [], array $new = [], ?string $description = null): void
    {
        /** @var Request $request */
        $request = request();
        $user = $request->user();

        AuditLog::query()->create([
            'school_id' => $user?->school_id,
            'user_id' => $user?->id,
            'user_role' => $user?->role_key,
            'action' => $action,
            'entity_type' => $entity ? $entity::class : null,
            'entity_id' => $entity->id ?? null,
            'old_value' => $old ?: null,
            'new_value' => $new ?: null,
            'description' => $description,
            'ip_address' => $request->ip(),
            'user_agent' => $request->userAgent(),
            'created_at' => now(),
        ]);
    }
}
