<?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\MVC\Controller\AdminController;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;

/**
 * Snippets list controller class.
 */
class SnippetsController extends AdminController
{
    /**
     * The prefix to use with controller messages.
     *
     * @var    string
     */
    protected $text_prefix = 'COM_TASSOSCODESNIPPETS_SNIPPETS';

    /**
     * The option for this component.
     *
     * @var    string
     */
    protected $option = 'com_tassoscodesnippets';

    /**
     * Proxy for getModel.
     *
     * @param   string  $name    The model name. Optional.
     * @param   string  $prefix  The class prefix. Optional.
     * @param   array   $config  Configuration array for model. Optional.
     *
     * @return  \Joomla\CMS\MVC\Model\BaseDatabaseModel
     */
    public function getModel($name = 'Snippet', $prefix = 'Administrator', $config = ['ignore_request' => true])
    {
        return parent::getModel($name, $prefix, $config);
    }

    /**
     * Method to export selected snippets.
     *
     * @return  void
     */
    public function export()
    {
        $this->checkToken();

        $pks = $this->input->post->get('cid', [], 'array');

        if (empty($pks))
        {
            $this->app->enqueueMessage(Text::_('COM_TASSOSCODESNIPPETS_NO_SNIPPET_SELECTED'), 'warning');
            $this->setRedirect('index.php?option=com_tassoscodesnippets&view=snippets');
            return;
        }

        $model = $this->getModel('Snippets');
        $model->export($pks);
    }

    /**
     * Method to handle snippet import.
     *
     * @return  void
     */
    public function import()
    {
        $file = $this->app->input->files->get('file');

        if (empty($file) || empty($file['name']))
        {
            $this->setRedirect('index.php?option=com_tassoscodesnippets&view=snippets&layout=import');
            return;
        }

        $this->checkToken();

        $snippetModel = $this->getModel('Snippet');
        $model        = $this->getModel('Snippets');
        $model->import($snippetModel, $file);
    }

    /**
     * Method to duplicate snippets.
     *
     * @return  void
     */
    public function duplicate()
    {
        // Check for request forgeries
        $this->checkToken();

        $pks = $this->input->post->get('cid', [], 'array');

        try
        {
            if (empty($pks))
            {
                throw new \Exception(Text::_('COM_TASSOSCODESNIPPETS_NO_SNIPPET_SELECTED'));
            }

            $model = $this->getModel();

            foreach ($pks as $pk)
            {
                $model->duplicate($pk);
            }

            $this->setMessage(Text::plural('COM_TASSOSCODESNIPPETS_N_SNIPPETS_DUPLICATED', count($pks)));
        }
        catch (\Exception $e)
        {
            $this->app->enqueueMessage($e->getMessage(), 'warning');
        }

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