<?php

declare(strict_types=1);

namespace App\Middlewares;

use App\Helpers\Request;
use App\Services\AuditService;

class AuditMiddleware
{
    private static bool $enabled = true;

    public function handle(Request $request): mixed
    {
        return null;
    }

    public static function log(?int $userId, string $action, string $entityType, ?int $entityId, ?array $oldValues = null, ?array $newValues = null): void
    {
        if (!self::$enabled) return;

        try {
            $service = new AuditService();
            $service->log(
                $userId,
                $action,
                $entityType,
                $entityId,
                $oldValues,
                $newValues,
                $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'
            );
        } catch (\Exception $e) {
            error_log('Audit log error: ' . $e->getMessage());
        }
    }

    public static function disable(): void
    {
        self::$enabled = false;
    }

    public static function enable(): void
    {
        self::$enabled = true;
    }
}
