<?php

namespace App\Services;

use App\Models\AdminAuditLog;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;

class AuditLogService
{
    public function log(
        User $admin,
        string $action,
        Model|string $entity,
        string|int|null $entityId = null,
        ?array $oldValues = null,
        ?array $newValues = null,
        ?string $ipAddress = null,
        ?string $userAgent = null
    ): AdminAuditLog {
        $entityType = $entity instanceof Model ? $entity::class : (string) $entity;
        $resolvedEntityId = $entity instanceof Model ? (string) $entity->getKey() : (string) $entityId;

        return AdminAuditLog::create([
            'admin_id' => $admin->id,
            'action' => $action,
            'entity_type' => $entityType,
            'entity_id' => $resolvedEntityId,
            'old_values_json' => $oldValues,
            'new_values_json' => $newValues,
            'ip_address' => $ipAddress,
            'user_agent' => $userAgent,
        ]);
    }
}

