<?php

require_once BASE_PATH . '/app/controllers/admin/AdminBaseController.php';

class AdminLogController extends AdminBaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->requireAdmin();
    }

    public function index(): void
    {
        $search = trim($_GET['search'] ?? '');
        $entityType = trim($_GET['entity_type'] ?? '');
        $actorId = max(0, (int) ($_GET['actor_id'] ?? 0));
        $allowedTypes = ['', 'user', 'product', 'post', 'gallery', 'review', 'system_setting'];
        if (!in_array($entityType, $allowedTypes, true)) {
            $entityType = '';
        }

        $page = max(1, (int) ($_GET['page'] ?? 1));
        $perPage = 20;
        $totalLogs = $this->activityLog()->countForAdmin($search, $entityType, $actorId);
        $totalPages = max(1, (int) ceil($totalLogs / $perPage));
        $page = min($page, $totalPages);
        $logs = $this->activityLog()->getForAdmin(
            $search,
            $entityType,
            $actorId,
            $perPage,
            ($page - 1) * $perPage
        );

        require BASE_PATH . '/app/views/admin/logs/index.php';
    }
}
