<?php
/**
 * Plugin Service Provider (Core + API)
 *
 * Combines CoreServiceProvider and ApiServiceProvider responsibilities
 * without changing behavior.
 */

namespace ZenoChatbot\ServiceProvider;

use ZenoChatbot\Framework\Foundation\ServiceProvider;
use ZenoChatbot\Framework\Http\Ajax;
use ZenoChatbot\Framework\Http\Route;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class PluginServiceProvider extends ServiceProvider
{
    /** @var string REST API namespace */
    private $namespace = 'zeno-chatbot-ai/v1';

    /**
     * Register services (merged from Core::register)
     */
    public function register()
    {
        // Routes are configured in Application class
    }

    /**
     * Boot services (merged Core + Api boot)
     */
    public function boot()
    {
        // WP hooks
        $this->initializeHooks();

        // AJAX handlers
        $this->initializeAjaxHandlers();

        // REST API registration hook
        add_action('rest_api_init', [$this, 'registerRestRoutes']);
    }

    /** Initialize WordPress hooks (from Core) */
    private function initializeHooks()
    {
        $this->loadCustomHooks();
        $this->loadCustomFilters();
    }

    /** Load custom hooks */
    private function loadCustomHooks()
    {
        $actionsFile = $this->app->basePath('src/Hooks/actions.php');
        if (file_exists($actionsFile)) {
            $hookLoader = require $actionsFile;
            if (is_callable($hookLoader)) {
                $hookLoader($this->app);
            }
        }
    }

    /** Load custom filters */
    private function loadCustomFilters()
    {
        $filtersFile = $this->app->basePath('src/Hooks/filters.php');
        if (file_exists($filtersFile)) {
            $filterLoader = require $filtersFile;
            if (is_callable($filterLoader)) {
                $filterLoader($this->app);
            }
        }
    }

    /** Initialize AJAX handlers (from Core) */
    private function initializeAjaxHandlers()
    {
        Ajax::setApp($this->app);
        Ajax::registerAjaxRoutes();
    }

    /** Register REST API routes (from Api) */
    public function registerRestRoutes()
    {
        Route::setApp($this->app);
        Route::registerRestRoutes();
    }

}
