
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
declare(strict_types=1);

abstract class BaseController {
  protected PDO $pdo;
  protected array $config;

  public function __construct(PDO $pdo, array $config) {
    $this->pdo = $pdo;
    $this->config = $config;
  }

  protected function render(string $view, array $params = []): void {
    extract($params);
    $config = $this->config;
    $pdo = $this->pdo;
    $user = auth_user($pdo);
    $flash = flash_get();
    $lang = $_SESSION['lang'] ?? 'en';
    $rtl = is_rtl();
    require __DIR__ . '/../views/layout.php';
  }

protected function log(
  string $entityType,
  int $entityId,
  string $action,
  int|string|null $actorUserId,
  array $meta = []
): void {
  // PDO often returns numeric IDs as strings, so normalize here
  $actorUserId = ($actorUserId === null || $actorUserId === '') ? null : (int)$actorUserId;

  $st = $this->pdo->prepare("
    INSERT INTO activity_logs(entity_type, entity_id, action, actor_user_id, meta_json, ip, user_agent, created_at)
    VALUES(?,?,?,?,?,?,?,?)
  ");
  $st->execute([
    $entityType,
    $entityId,
    $action,
    $actorUserId,
    $meta ? json_encode($meta, JSON_UNESCAPED_UNICODE) : null,
    $_SERVER['REMOTE_ADDR'] ?? null,
    substr($_SERVER['HTTP_USER_AGENT'] ?? '', 0, 250),
    now(),
  ]);
}
}
