<?php
defined('BASEPATH') OR exit('No direct script access allowed');


function log_activity($module, $action, $desc, $status = 'success')
{
    $CI =& get_instance();

    $ip = $CI->input->ip_address();

    if (!$ip || $ip == '0.0.0.0') {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? 
              $_SERVER['REMOTE_ADDR'] ?? 
              'UNKNOWN';
    }

    $CI->db->insert('activity_logs', [
        'user_id'     => $CI->session->userdata('user_id') ?? 0,
        'module'      => strtolower($module),
        'action'      => strtolower($action),
        'description' => $desc,
        'status'      => strtolower($status),
        'ip_address'  => $ip,
        'created_at'  => date('Y-m-d H:i:s')
    ]);
}