<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Helpers\ActivityLogger;
use Core\Request;

final class ActivityLogController extends BaseController
{
    public function index(array $params = []): void
    {
        $this->requirePermission('activity_log.view');

        $filters = [
            'module'    => Request::get('module', ''),
            'action'    => Request::get('action', ''),
            'user_id'   => Request::get('user_id', ''),
            'date_from' => Request::get('date_from', ''),
            'date_to'   => Request::get('date_to', date('Y-m-d')),
        ];

        $logs  = ActivityLogger::recent(200, $filters);
        $today = ActivityLogger::countToday();
        $users = $this->db->fetchAll("SELECT id, name FROM users ORDER BY name ASC");

        $this->view('settings.activity_log', [
            'pageTitle'   => 'Activity Log',
            'breadcrumbs' => ['Activity Log' => null],
            'logs'        => $logs,
            'filters'     => $filters,
            'today'       => $today,
            'users'       => $users,
        ]);
    }
}
