<?php

namespace App\Models;

use App\Core\Model;

class ActivityLog extends Model
{
    protected string $table = 'activity_logs';

    public function log(?int $userId, string $action, string $details = '', string $ip = ''): void
    {
        $stmt = $this->db->prepare(
            "INSERT INTO activity_logs (user_id, action, details, ip_address) VALUES (?, ?, ?, ?)"
        );
        $stmt->execute([$userId, $action, $details, $ip ?: ($_SERVER['REMOTE_ADDR'] ?? '')]);
    }

    public function getRecent(int $limit = 50): array
    {
        $stmt = $this->db->prepare(
            "SELECT l.*, u.username FROM activity_logs l
             LEFT JOIN users u ON l.user_id = u.id
             ORDER BY l.created_at DESC LIMIT ?"
        );
        $stmt->bindValue(1, $limit, \PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll();
    }
}
