<?php

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

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;

require_once __DIR__ . '/script.install.helper.php';

class Com_TassosCodesnippetsInstallerScript extends Com_TassosCodesnippetsInstallerScriptHelper
{
	public $name = 'TassosCodesnippets';
	public $alias = 'tassoscodesnippets';
	public $extension_type = 'component';

	public function onAfterInstall()
	{
        if ($this->install_type == 'install') 
        {
            $this->addContentType();
        }
    }

     /**
     * Add content type for UCM/tags support.
     *
     * @return  void
     */
    private function addContentType(): void
    {
        $db = Factory::getContainer()->get(DatabaseInterface::class);

        // Check if already exists
        $query = $db->getQuery(true)
            ->select('COUNT(*)')
            ->from($db->quoteName('#__content_types'))
            ->where($db->quoteName('type_alias') . ' = ' . $db->quote('com_tassoscodesnippets.snippet'));

        $db->setQuery($query);

        if ($db->loadResult())
        {
            return;
        }

        $contentType = (object) [
            'type_title'       => 'Code Snippet',
            'type_alias'       => 'com_tassoscodesnippets.snippet',
            'table'            => json_encode([
                'special' => [
                    'dbtable' => '#__tassos_codesnippets',
                    'key'     => 'id',
                    'type'    => 'SnippetTable',
                    'prefix'  => 'Tassos\\CodeSnippets\\Administrator\\Table\\',
                    'config'  => 'array()'
                ],
                'common' => [
                    'dbtable' => '#__ucm_content',
                    'key'     => 'ucm_id',
                    'type'    => 'Corecontent',
                    'prefix'  => 'Joomla\\CMS\\Table\\',
                    'config'  => 'array()'
                ]
            ]),
            'rules'            => '',
            'field_mappings'   => json_encode([
                'common' => [
                    'core_content_item_id' => 'id',
                    'core_title'           => 'title',
                    'core_state'           => 'state',
                    'core_alias'           => null,
                    'core_created_time'    => 'created',
                    'core_modified_time'   => 'modified',
                    'core_body'            => 'code',
                    'core_hits'            => null,
                    'core_publish_up'      => null,
                    'core_publish_down'    => null,
                    'core_access'          => 'access',
                    'core_params'          => null,
                    'core_featured'        => null,
                    'core_metadata'        => null,
                    'core_language'        => 'language',
                    'core_images'          => null,
                    'core_urls'            => null,
                    'core_version'         => null,
                    'core_ordering'        => 'ordering',
                    'core_metakey'         => null,
                    'core_metadesc'        => null,
                    'core_catid'           => 'catid',
                    'asset_id'             => null
                ],
                'special' => [
                    'location' => 'location',
                    'rules'    => 'rules'
                ]
            ]),
            'router'           => '',
            'content_history_options' => ''
        ];

        $db->insertObject('#__content_types', $contentType);
    }
}