<?php
declare(strict_types=1);

// Christo Lochenberg Developments | info@christodev.co.za | +27 61 523 9126
class AdminLogs extends Controller {
    
    public function __construct() {
        if (!isLoggedIn() || !isAdmin()) {
            header('Location: ' . URLROOT . '/users/login');
            exit;
        }
    }

    public function logs(): void {
        $logModel = $this->model('Log');
        $logs = $logModel->getLogs();

        $data = [
            'title' => 'System Logs',
            'logs' => $logs,
            
            // Contextual Help
            'help_title' => 'System Audit Trail Guide',
            'help_content' => '
                <h4 class="font-bold text-gray-800 dark:text-gray-200 mt-0">System Audit Trail</h4>
                <p>The system automatically logs critical events, authentication attempts, and administrative actions in the background.</p>
                <ul class="list-disc pl-5 space-y-2 mt-2">
                    <li><strong>Tracking:</strong> Each log entry records the action type, a description of what occurred, the user responsible, and their IP address at the time.</li>
                    <li><strong>Maintenance:</strong> To prevent the database from growing too large over time, you can periodically use the "Clear All Logs" button. Note that this action is permanent and is itself logged for security purposes.</li>
                </ul>
            '
        ];

        $this->view('admin/logs', $data);
    }

    public function clearLogs(): void {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if ($this->model('Log')->clearLogs()) {
                systemLog('LOGS_CLEARED', 'Admin permanently cleared all system logs.');
                flash('system_msg', 'System logs cleared successfully!', 'success');
            } else {
                flash('system_msg', 'Something went wrong while clearing the logs.', 'error');
            }
            header('Location: ' . URLROOT . '/admin/logs');
            exit;
        } else {
            header('Location: ' . URLROOT . '/admin/logs');
            exit;
        }
    }
}