<?php
declare(strict_types=1);

namespace App\Models;

use App\Core\Auth;
use App\Core\Database;

final class AuditLog
{
    public static function write(string $action, ?string $type = null, ?int $id = null, ?array $meta = null): void
    {
        Database::instance()->query(
            'INSERT INTO audit_logs (tenant_id, user_id, action, entity_type, entity_id, ip, meta)
             VALUES (:tid, :uid, :action, :type, :eid, :ip, :meta)',
            [
                'tid' => Auth::instance()->tenantId(),
                'uid' => Auth::instance()->id(),
                'action' => $action,
                'type' => $type,
                'eid' => $id,
                'ip' => request_ip(),
                'meta' => $meta ? json_encode($meta) : null,
            ]
        );
    }
}
