<?php
/**
 * Theme Setup Class
 *
 * Initializes theme supports, navigation menus, image sizes,
 * starter content, and toolkit integrations.
 *
 * @package    Wira
 * @since      1.0.0
 */

namespace Wira;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! class_exists( 'Theme_Setup' ) ) {

	/**
	 * Theme setup handler.
	 *
	 * Implements the singleton pattern to ensure
	 * a single instance throughout the theme lifecycle.
	 *
	 * @since 1.0.0
	 */
	class Theme_Setup {

		/**
		 * Singleton instance.
		 *
		 * @since 1.0.0
		 * @var self|null
		 */
		private static $instance = null;

		/**
		 * Get singleton instance.
		 *
		 * @since 1.0.0
		 * @return self
		 */
		public static function get_instance() {
			if ( null === self::$instance ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Constructor.
		 *
		 * Private to enforce singleton pattern.
		 *
		 * @since 1.0.0
		 */
		private function __construct() {
			$this->load_hooks();
		}

		/**
		 * Register hooks for theme setup.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		private function load_hooks() {
			add_action( 'after_setup_theme', array( $this, 'setup_theme' ) );
		}

		/**
		 * Prevent cloning the singleton instance.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		private function __clone() {}

		/**
		 * Prevent unserializing the singleton instance.
		 *
		 * @since 1.0.0
		 * @throws \Exception If attempted.
		 * @return void
		 */
		public function __wakeup() {
			throw new \Exception( 'Cannot unserialize singleton' );
		}

		/**
		 * Setup theme supports and features.
		 *
		 * Registers theme supports, menus, image sizes,
		 * starter content, and loads toolkit integrations.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function setup_theme() {
			// Load translations from /languages.
			load_theme_textdomain( 'wira', WIRA_DIR . 'languages' );

			// Theme supports.
			add_theme_support( 'title-tag' );
			add_theme_support( 'post-thumbnails' );
			add_theme_support(
				'custom-logo',
				array(
					'height'      => 35,
					'width'       => 200,
					'flex-height' => true,
					'flex-width'  => true,
				)
			);
			add_theme_support( 'html5', array( 'search-form', 'comment-form', 'gallery', 'caption' ) );
			add_theme_support( 'responsive-embeds' );
			add_theme_support( 'wp-block-styles' );
			add_theme_support( 'editor-styles' );
			add_editor_style( '/assets/css/editor-style.css' );
			add_theme_support( 'elementor' );
			add_theme_support( 'customize-selective-refresh-widgets' );
			add_theme_support( 'automatic-feed-links' );
			add_theme_support( 'align-wide' );

			// Custom Header.
			add_theme_support(
				'custom-header',
				array(
					'default-image' => '',
					'width'         => $this->get_content_width(),
					'height'        => 500,
					'flex-height'   => true,
					'flex-width'    => true,
				)
			);

			// Custom Background.
			add_theme_support(
				'custom-background',
				array(
					'default-color' => '#ffffff',
					'default-image' => '',
				)
			);

			// Register Navigation Menus.
			register_nav_menus(
				array(
					'main-menu' => __( 'Main Menu', 'wira' ),
				)
			);

			// Load Toolkit integrations if available.
			if ( class_exists( 'Wtoolkit_Admin_Menu_Loader' ) ) {
				new \Wtoolkit_Admin_Menu_Loader();
			}
			if ( class_exists( 'Wtoolkit_Theme_Options_Loader' ) ) {
				new \Wtoolkit_Theme_Options_Loader();
			}

			// Global Content Width.
			$GLOBALS['content_width'] = apply_filters( 'wira_content_width', 1280 );

			// Post Thumbnail Size.
			add_image_size( 'wira-post-thumb', 600, 400, true );

			// Starter Content.
			add_theme_support(
				'starter-content',
				array(
					'widgets'     => array(),
					'posts'       => array(
						'home' => array(
							'post_type'    => 'page',
							'post_title'   => __( 'Home', 'wira' ),
							'post_content' => '<!-- wp:pattern {"slug":"wira/page-home"} /-->',
						),
					),
					'options'     => array(
						'show_on_front' => 'page',
						'page_on_front' => '{{home}}',
					),
					'nav_menus'   => array(
						'main-menu' => array(
							'name'  => __( 'Main Menu', 'wira' ),
							'items' => array(
								'page_home',
								array(
									'type'  => 'custom',
									'title' => __( 'About', 'wira' ),
									'url'   => '#about',
								),
								array(
									'type'  => 'custom',
									'title' => __( 'Services', 'wira' ),
									'url'   => '#services',
								),
								array(
									'type'  => 'custom',
									'title' => __( 'Features', 'wira' ),
									'url'   => '#features',
								),
								array(
									'type'  => 'custom',
									'title' => __( 'Testimonials', 'wira' ),
									'url'   => '#testimonials',
								),
								array(
									'type'  => 'custom',
									'title' => __( 'Contact', 'wira' ),
									'url'   => '#contact',
								),
							),
						),
					),
					'attachments' => array(),
					'theme_mods'  => array(),
				)
			);
		}

		/**
		 * Return content width.
		 *
		 * @since 1.0.0
		 * @return int Content width in pixels.
		 */
		private function get_content_width() {
			return apply_filters( 'wira_content_width', 1280 );
		}
	}
}
