<?php
defined('_JEXEC') or die;

use Joomla\CMS\Factory;

// Legacy entry point for Administrator
if (!defined('JPATH_COMPONENT')) {
	define('JPATH_COMPONENT', JPATH_BASE . '/components/com_panggilan');
}
if (!defined('JPATH_COMPONENT_ADMINISTRATOR')) {
	define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR . '/components/com_panggilan');
}
if (!defined('JPATH_COMPONENT_SITE')) {
	define('JPATH_COMPONENT_SITE', JPATH_SITE . '/components/com_panggilan');
}

// Load the controller
require_once __DIR__ . '/controller.php';

// Execute the task
$task = Factory::getApplication()->input->get('task');

// Direct Dispatch for 'item' tasks to ensure namespaced controller is used
if (!empty($task) && strpos($task, '.') !== false) {
    list($controllerName, $taskName) = explode('.', $task);
    
    if ($controllerName === 'item') {
        // Manually load if autoloader issues persist or just to be safe
        if (!class_exists('Joomla\\Component\\Panggilan\\Administrator\\Controller\\ItemController')) {
            require_once __DIR__ . '/src/Controller/ItemController.php';
        }
        $controller = new \Joomla\Component\Panggilan\Administrator\Controller\ItemController();
        $controller->execute($taskName);
        $controller->redirect();
        return;
    } elseif ($controllerName === 'items') {
         if (!class_exists('Joomla\\Component\\Panggilan\\Administrator\\Controller\\ItemsController')) {
            require_once __DIR__ . '/src/Controller/ItemsController.php';
        }
        $controller = new \Joomla\Component\Panggilan\Administrator\Controller\ItemsController();
        $controller->execute($taskName);
        $controller->redirect();
        return;
    }
}

// Default/Fallback Dispatch
$controller = new PanggilanController();
$controller->execute($task);
$controller->redirect();
