<?php
use App\Core\Database;

function audit_log($action,$entity,$entityId=null,$details=null)
{
    if (!isset($_SESSION['user'])) return;

    $db = Database::getInstance();

    $db->prepare("
        INSERT INTO audit_logs
        (user_id, action, entity_name, entity_id, details, ip_address, user_agent, org_id)
        VALUES (?,?,?,?,?,?,?,?)
    ")->execute([
        $_SESSION['user']['id'],
        $action,
        $entity,
        $entityId,
        json_encode($details),
        $_SERVER['REMOTE_ADDR'] ?? '',
        $_SERVER['HTTP_USER_AGENT'] ?? '',
        $_SESSION['user']['org_id'] ?? null
    ]);
}
