<?php

class ThemeRouter
{
    public static function dispatch($module, $action = 'index')
    {
        global $theme, $themeKey, $company, $companyExtra, $isAutoDomain, $siteConfig;

        $context = array(
            'theme' => $theme ?? 'industrial',
            'themeKey' => $themeKey ?? ($theme ?? 'industrial'),
            'company' => $company ?? array(),
            'companyExtra' => $companyExtra ?? array(),
            'isAutoDomain' => $isAutoDomain ?? false,
            'siteConfig' => $siteConfig ?? array(),
        );

        $map = array(
            'home' => 'HomeController',
            'news' => 'NewsController',
            'product' => 'ProductController',
            'case' => 'CaseController',
            'faq' => 'FaqController',
            'page' => 'PageController',
        );

        $class = $map[$module] ?? 'HomeController';
        if(!class_exists($class)){
            http_response_code(500);
            echo 'Controller not found: ' . htmlspecialchars($class, ENT_QUOTES, 'UTF-8');
            return;
        }
        $controller = new $class($context);
        if(!method_exists($controller, $action)) $action = 'index';
        $controller->$action();
    }
}
