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

/**
 * Activity_model — admin audit trail.
 */
#[\AllowDynamicProperties]
class Activity_model extends MY_Model {

	protected $table = 'activity_log';
	protected $order_by = 'created_at DESC';

	/** Record an admin action. */
	public function record($user_id, $action, $detail = '')
	{
		return $this->db->insert($this->table, array(
			'user_id'    => $user_id,
			'action'     => $action,
			'detail'     => $detail,
			'ip_address' => $this->input->ip_address(),
			'created_at' => date('Y-m-d H:i:s'),
		));
	}

	/** Recent activity joined with the acting user's name. */
	public function recent($limit = 15)
	{
		return $this->db->select('activity_log.*, users.name AS user_name')
			->join('users', 'users.id = activity_log.user_id', 'left')
			->order_by('activity_log.created_at DESC')
			->limit($limit)
			->get($this->table)->result_array();
	}
}
