<?php
require_once __DIR__ . '/BaseModel.php';

/**
 * Activity Logging Class
 * Handles activity logging and audit trails
 */
class ActivityLog extends BaseModel {
    protected $tableName = 'activity_logs';
    protected $tenantColumn = null; // Activity logs are global
    
    public function __construct() {
        parent::__construct();
    }
    
    /**
     * Log an activity
     */
    public function log($userId, $actionType, $module, $referenceId, $oldValues = null, $newValues = null) {
        $data = [
            'user_id' => $userId,
            'action_type' => $actionType,
            'module' => $module,
            'reference_id' => $referenceId,
            'old_values' => $oldValues ? json_encode($oldValues) : null,
            'new_values' => $newValues ? json_encode($newValues) : null,
            'ip_address' => $_SERVER['REMOTE_ADDR'] ?? null,
            'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null
        ];
        
        return $this->create($data);
    }
    
    /**
     * Get activity logs with filters
     */
    public function getLogs($filters = [], $limit = 100) {
        $sql = "SELECT al.*, u.first_name, u.last_name, u.email 
                FROM activity_logs al 
                LEFT JOIN users u ON al.user_id = u.id 
                WHERE 1=1";
        
        $params = [];
        
        if (!empty($filters['user_id'])) {
            $sql .= " AND al.user_id = ?";
            $params[] = $filters['user_id'];
        }
        
        if (!empty($filters['module'])) {
            $sql .= " AND al.module = ?";
            $params[] = $filters['module'];
        }
        
        if (!empty($filters['action_type'])) {
            $sql .= " AND al.action_type = ?";
            $params[] = $filters['action_type'];
        }
        
        if (!empty($filters['start_date'])) {
            $sql .= " AND al.timestamp >= ?";
            $params[] = $filters['start_date'];
        }
        
        if (!empty($filters['end_date'])) {
            $sql .= " AND al.timestamp <= ?";
            $params[] = $filters['end_date'];
        }
        
        $sql .= " ORDER BY al.timestamp DESC LIMIT ?";
        $params[] = $limit;
        
        $result = $this->db->query($sql, $params);
        $logs = [];
        
        while ($row = $result->fetch_assoc()) {
            if ($row['old_values']) {
                $row['old_values'] = json_decode($row['old_values'], true);
            }
            if ($row['new_values']) {
                $row['new_values'] = json_decode($row['new_values'], true);
            }
            $logs[] = $row;
        }
        
        return $logs;
    }
    
    /**
     * Get activity logs by user
     */
    public function getLogsByUser($userId, $limit = 50) {
        return $this->getLogs(['user_id' => $userId], $limit);
    }
    
    /**
     * Get activity logs by module
     */
    public function getLogsByModule($module, $limit = 100) {
        return $this->getLogs(['module' => $module], $limit);
    }
}
?>
