<?php
/**
 * Main plugin class that coordinates the loaders, routes, and services.
 *
 * @package    100Prints
 */

class Hundred_Prints {

	/**
	 * Main class constructor.
	 */
	public function __construct() {
		$this->load_dependencies();
		$this->define_admin_hooks();
		$this->define_public_hooks();
	}

	/**
	 * Load the required files.
	 */
	private function load_dependencies() {
		// Helpers
		require_once HUNDRED_PRINTS_PATH . 'helpers/encryption.php';
		require_once HUNDRED_PRINTS_PATH . 'helpers/variable-sources.php';
		require_once HUNDRED_PRINTS_PATH . 'helpers/settings.php';
		require_once HUNDRED_PRINTS_PATH . 'helpers/template-helpers.php';
		require_once HUNDRED_PRINTS_PATH . 'helpers/validation.php';

		// Services
		require_once HUNDRED_PRINTS_PATH . 'services/class-connection-service.php';
		require_once HUNDRED_PRINTS_PATH . 'services/class-template-service.php';
		require_once HUNDRED_PRINTS_PATH . 'services/class-payload-resolver.php';
		require_once HUNDRED_PRINTS_PATH . 'services/class-analytics-service.php';

		// Loaders & Registries
		require_once HUNDRED_PRINTS_PATH . 'includes/class-i18n.php';
		require_once HUNDRED_PRINTS_PATH . 'admin/class-admin-loader.php';
		require_once HUNDRED_PRINTS_PATH . 'api/class-api-loader.php';
		require_once HUNDRED_PRINTS_PATH . 'includes/class-block-loader.php';
		require_once HUNDRED_PRINTS_PATH . 'includes/class-shortcode.php';
	}

	/**
	 * Register all admin hooks (page navigation, settings registration, etc.).
	 */
	private function define_admin_hooks() {
		if ( is_admin() ) {
			$admin_loader = new Hundred_Prints_Admin_Loader();
			add_action( 'admin_menu', [ $admin_loader, 'register_admin_pages' ] );
			add_action( 'admin_enqueue_scripts', [ $admin_loader, 'enqueue_admin_assets' ] );
			add_action( 'admin_init', [ $admin_loader, 'handle_welcome_redirect' ] );
			add_action( 'admin_init', [ $admin_loader, 'register_plugin_settings' ] );
		}
	}

	/**
	 * Register all front-facing hooks (rest api, block registration, shortcode, etc.).
	 */
	private function define_public_hooks() {
		// 1. Localization
		$i18n = new Hundred_Prints_i18n();
		add_action( 'plugins_loaded', [ $i18n, 'load_textdomain' ] );

		// 2. REST API
		$api_loader = new Hundred_Prints_API_Loader();
		add_action( 'rest_api_init', [ $api_loader, 'register_rest_routes' ] );

		// 3. Gutenberg Block
		$block_loader = new Hundred_Prints_Block_Loader();
		add_action( 'init', [ $block_loader, 'register_blocks' ] );
		add_action( 'enqueue_block_assets', [ $block_loader, 'enqueue_frontend_assets' ] );
		add_action( 'wp_enqueue_scripts', [ $block_loader, 'enqueue_global_sdk_assets' ] );

		// 4. Shortcode
		$shortcode = new Hundred_Prints_Shortcode();
		add_shortcode( '100prints', [ $shortcode, 'render_shortcode' ] );

		// 5. Maintenance
		$analytics_service = new Hundred_Prints_Analytics_Service();
		add_action( 'hundred_prints_prune_generation_logs', [ $analytics_service, 'prune_old_logs' ] );
	}

	/**
	 * Execute the plugin lifecycle.
	 */
	public function run() {
		// Executed successfully
	}
}
