<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

add_action( 'admin_menu', 'sifency_addons_register_admin_menu' );

function sifency_addons_register_admin_menu() {
	if ( ! Sifency_Addons_Theme_Builder_Master::sifency_addons_selective_environment() ) {
		$menu_title = esc_html__( 'Sifency Addons', 'sifency-addons' );
	} else {
		$theme      = wp_get_theme();
		$theme_name = $theme->parent() ? $theme->parent()->get( 'Name' ) : $theme->get( 'Name' );
		// Translators: %s is the Theme Name
		$menu_title = esc_html( sprintf( __( '%s Theme', 'sifency-addons' ), $theme_name ) );
	}

	add_menu_page(
		esc_html__( 'Sifency Addons', 'sifency-addons' ),
		$menu_title,
		'manage_options',
		'sifency-addons',
		'sifency_addons_render_welcome_page',
		SIFENCY_ADDONS_URL . 'assets/images/logo.png',
		25
	);

	$submenus = array(
		array( 'Dashboard', 'sifency-addons', 'sifency_addons_render_welcome_page' ),
		array( 'Theme Builder', 'sifency-theme-builder', 'sifency_addons_render_theme_builder_page' ),
		array( 'Elementor Widgets', 'sifency-addons-widgets', 'sifency_addons_render_addons_widgets_page' ),
		array( 'Reusable Templates', 'sifency-templates-shortcode', 'sifency_addons_render_templates_shortcode_page' ),
		array( 'AI Studio', 'sifency-ai', array( Sifency_Addons_AI_Module::instance(), 'render_admin_page' ) ),
		array( 'Settings', 'sifency-settings', 'sifency_addons_render_settings_page' ),
		array( 'Plugins', 'sifency-extentions', 'sifency_addons_render_extentions_page' ),
		array( 'Prebuilt', 'sifency-prebuild-website', 'sifency_addons_render_prebuild_website_page' ),
		array( 'Analytics', 'sifency-analytics', 'sifency_addons_render_analytics_page' ),
	);

	$is_core_active                           = class_exists( 'Sifency_Essential' );
	$sifency_addons_selective_environment_var = class_exists( 'Sifency_Addons_Theme_Builder_Master' ) &&
	Sifency_Addons_Theme_Builder_Master::sifency_addons_selective_environment();

	if ( ! $is_core_active && ! $sifency_addons_selective_environment_var ) {
		$submenus[] = array( 'Themes', 'sifency-themes', 'sifency_addons_render_themes_page' );
		$submenus[] = array( 'Data Suite', 'sifency-data-suite', 'sifency_addons_render_data_suite_page' );
		$submenus[] = array( 'Why Pro?', 'sifency-why-pro', 'sifency_addons_render_why_pro_page' );

		if ( ! class_exists( 'Sifency_Addons_Theme_Builder_Master' ) || ! Sifency_Addons_Theme_Builder_Master::is_sync_valid() ) {
			$submenus[] = array( 'Activate', 'activation', 'sifency_addons_render_activate_page' );
		}
	}

	$submenus = apply_filters( 'sifency_addons_admin_submenus', $submenus );

	foreach ( $submenus as $submenu ) {
		add_submenu_page(
			'sifency-addons',
			esc_html( $submenu[0] ),
			esc_html( $submenu[0] ),
			'manage_options',
			$submenu[1],
			$submenu[2]
		);
	}
}
/**
 * Renders the admin page layout.
 *
 * @param string   $page_slug   The page slug.
 * @param callable $content_callback The callback function to render the page content.
 */
function sifency_addons_admin_render_layout( $page_slug, $content_callback ) {

	echo '<div class="sifency-admin-wrapper">';

	if ( is_admin() && class_exists( 'Sifency_Addons_Unlock_Notice' ) ) {
		new Sifency_Addons_Unlock_Notice();
	}

	$topbar_file = SIFENCY_ADDONS_DIR . 'classes/admin-module/topbar/class-sifency-admin-topbar.php';
	if ( file_exists( $topbar_file ) ) {
		require_once $topbar_file;
		if ( class_exists( 'Sifency_Addons_Admin_Topbar' ) ) {
			$topbar = new Sifency_Addons_Admin_Topbar();
			$topbar->render( $page_slug );
		}
	}

	if ( is_callable( $content_callback ) ) {
		call_user_func( $content_callback );
	} else {
		echo '<p><em>Page content not available.</em></p>';
	}

	echo '</div>';
}
/**
 * Renders the welcome page.
 */
function sifency_addons_render_welcome_page() {
	$welcome_dashboard = SIFENCY_ADDONS_DIR . 'classes/admin-module/admin-dashboard/class-land.php';
	if ( file_exists( $welcome_dashboard ) ) {
		require_once $welcome_dashboard;
	}
	sifency_addons_admin_render_layout(
		'dashboard',
		function() {
			if ( class_exists( 'Sifency_Welcome_Admin_Dashboard' ) ) {
				$page = new Sifency_Welcome_Admin_Dashboard();
				$page->render_welcome_page();
			}
		}
	);
}

function sifency_addons_render_activate_page() {
	sifency_addons_admin_render_layout(
		'activate',
		function() {
			if ( class_exists( 'Sifency_Addons_Theme_Builder_Master' ) ) {
				Sifency_Addons_Theme_Builder_Master::instance()->render_sifency_addons_hidden_code_form();
			}
		}
	);
}

/**
 * Renders the "Widgets" page (page=sifency-addons-widgets).
 */
function sifency_addons_render_addons_widgets_page() {
	sifency_addons_admin_render_layout(
		'widgets',
		function() {
			$admin_manager = new Sifency_Addons_Admin_Page_Widgets_Options();
			$admin_manager->render_admin_page();
		}
	);
}

function sifency_addons_render_theme_builder_page() {
	sifency_addons_admin_render_layout(
		'theme-builder',
		function() {
			if ( class_exists( 'Sifency_Theme_Builder_Admin' ) ) {
				$admin = new Sifency_Theme_Builder_Admin();
				$admin->render_admin_page();
			}
		}
	);
}

function sifency_addons_render_templates_shortcode_page() {
	$section_template_builder = SIFENCY_ADDONS_DIR . 'classes/template-shortcode/class-template.php';
	if ( file_exists( $section_template_builder ) ) {
		require_once $section_template_builder;
	}
	sifency_addons_admin_render_layout(
		'template-maker',
		function() {
			if ( class_exists( 'Sifency_Section_Builder_Admin_Module' ) ) {
				$page = new Sifency_Section_Builder_Admin_Module();
				$page->render_admin_page();
			}
		}
	);
}

function sifency_addons_render_prebuild_website_page() {
	$prebuild_theme = SIFENCY_ADDONS_DIR . 'classes/admin-module/prebuild/init.php';
	if ( file_exists( $prebuild_theme ) ) {
		require_once $prebuild_theme;
	}
	sifency_addons_admin_render_layout(
		'prebuild-website',
		function() {
			if ( class_exists( 'Sifency_Addons_Prebuild_Website' ) ) {
				$page = new Sifency_Addons_Prebuild_Website();
				$page->render();
			}
		}
	);
}

function sifency_addons_render_themes_page() {
	$theme = SIFENCY_ADDONS_DIR . 'classes/admin-module/theme/class-theme.php';
	if ( file_exists( $theme ) ) {
		require_once $theme;
	}
	sifency_addons_admin_render_layout(
		'themes',
		function() {
			if ( class_exists( 'Sifency_Addons_Admin_Themes' ) ) {
				$page = new Sifency_Addons_Admin_Themes();
				$page->render();
			}
		}
	);
}

function sifency_addons_render_extentions_page() {
	sifency_addons_admin_render_layout(
		'extentions',
		function() {
			if ( class_exists( 'Sifency_Addons_Plugin_Installer_Admin_UI' ) ) {
				$installer_page = Sifency_Addons_Plugin_Installer_Admin_UI::instance();
				$installer_page->render_admin_page();
			}
		}
	);

}

function sifency_addons_render_settings_page() {
	sifency_addons_admin_render_layout(
		'settings',
		function() {
			Sifency_Addons_Admin_Options::instance()->render_settings_page();
		}
	);
}

/**
 * Renders the main "Data Suite" admin page with Import, Export, and Reset tools.
 */
function sifency_addons_render_data_suite_page() {
	sifency_addons_admin_render_layout(
		'data_suite',
		function() {
			if ( class_exists( 'Sifency_Addons_Data_Suite' ) ) {
				$page = new Sifency_Addons_Data_Suite();
				$page->render();
			}
		}
	);
}

/**
 * Render the "Why Pro" admin page
 */
function sifency_addons_render_why_pro_page() {
	$why_pro_file = SIFENCY_ADDONS_DIR . 'classes/admin-module/why-me/why-me.php';
	if ( file_exists( $why_pro_file ) ) {
		require_once $why_pro_file;
	}
	sifency_addons_admin_render_layout(
		'PRO',
		function() {
			if ( class_exists( 'Sifency_Addons_Admin_Why_Pro' ) ) {
				$page = new Sifency_Addons_Admin_Why_Pro();
				$page->render();
			}
		}
	);
}

/**
 * Renders the Analytics Dashboard page
 */
function sifency_addons_render_analytics_page() {
	sifency_addons_admin_render_layout(
		'analytics',
		function() {
			if ( class_exists( 'Sifency_Addons_Widget_Analytics' ) ) {
				Sifency_Addons_Widget_Analytics::render_dashboard();
			} else {
				echo '<p><em>' . esc_html__( 'Analytics class not found.', 'sifency-addons' ) . '</em></p>';
			}
		}
	);
}
