<?php

/**
 * @package         Tassos Code Snippets
 * @version         1.0.1-RC2 Free
 *
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            https://www.tassos.gr
 * @copyright       Copyright © 2026 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
 */

namespace Tassos\CodeSnippets\Administrator\Controller;

defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;

/**
 * Controller for the Error Logs view.
 */
class LogsController extends BaseController
{
    /**
     * Delete a specific snippet log file.
     *
     * @return  void
     */
    public function clear(): void
    {
        $this->checkToken();

        $app      = $this->app;
        $logFile  = $app->input->getString('logfile', '');
        $logPath  = $app->get('log_path') . '/tassos_code_snippets';

        // Validate the filename strictly — only allow our own pattern
        if (!preg_match('/^snippet_[a-z0-9\-_]+_\d+\.log$/', $logFile))
        {
            $app->enqueueMessage(Text::_('COM_TASSOSCODESNIPPETS_LOGS_INVALID_FILE'), 'error');
            $this->setRedirect('index.php?option=com_tassoscodesnippets&view=logs');

            return;
        }

        $fullPath = $logPath . '/' . $logFile;

        if (is_file($fullPath) && unlink($fullPath))
        {
            $app->enqueueMessage(Text::_('COM_TASSOSCODESNIPPETS_LOGS_CLEARED'));
        }
        else
        {
            $app->enqueueMessage(Text::_('COM_TASSOSCODESNIPPETS_LOGS_CLEAR_FAILED'), 'error');
        }

        $this->setRedirect('index.php?option=com_tassoscodesnippets&view=logs');
    }
}
