<?php

declare(strict_types=1);
if (!defined('WPINC')) {
    die();
}
/**
 * The core plugin class.
 */
class CPEPAI_Plugin
{
    /**
     * The services of the plugin.
     *
     * @var array
     */
    protected array $services = [];

    /**
     * Initialize the plugin.
     */
    public function __construct()
    {
        $this->load_services();
    }

    /**
     * Load and register all plugin services.
     *
     * @return void
     */
    private function load_services(): void
    {
        // Initialize options service first as it's needed by other services
        $options_service = new CPEPAI_Options_Service();

        // Initialize database service
        $database_service = new CPEPAI_Database_Service();

        // Initialize posts service
        $posts_service = new CPEPAI_Posts_Service();

        $this->services = [
            new CPEPAI_Admin_Service(), // Handles admin page registration and rendering
            new CPEPAI_Assets_Service(), // Handles scripts and styles
            new CPEPAI_Api_Service($options_service, $database_service, $posts_service), // Handles REST API endpoints
            $options_service, // Handles plugin options and settings
            $database_service, // Handles database operations
            $posts_service // Handles WordPress posts and pages operations
        ];
    }

    /**
     * Run the plugin.
     *
     * @return void
     */
    public function run(): void
    {
        foreach ($this->services as $service) {
            if (method_exists($service, 'register')) {
                $service->register();
            }
        }
    }
}
