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

/**
 * Handles all admin-facing functionality.
 */
class Sifency_Theme_Builder_Admin {

	public function render_admin_page() {
		$all_tabs      = sifency_addons_themebuilder_get_tabs();
		$nav_structure = function_exists( 'sifency_addons_themebuilder_get_nav_structure' )
			? sifency_addons_themebuilder_get_nav_structure()
			: array();

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$active_tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'all';
		?>
		<div class="sifency-admin-page">
			<div class="sifency-dashboard-layout">
				
				<!-- Navigation Sidebar -->
				<nav class="sifency-dashboard-nav" aria-label="<?php esc_attr_e( 'Theme Builder Navigation', 'sifency-addons' ); ?>">
					<!-- Sidebar Toggle Button -->
					<div class="sifency-sidebar-toggle-wrap">
						<button type="button" class="sifency-sidebar-toggle" aria-label="<?php esc_attr_e( 'Toggle Sidebar', 'sifency-addons' ); ?>" aria-expanded="true">
							<span class="dashicons dashicons-menu"></span>
						</button>
					</div>

					<?php foreach ( $nav_structure as $key => $group_data ) : ?>
						
						<?php
						if ( isset( $group_data['type'] ) && 'single' === $group_data['type'] ) :
							$tab_slug = $group_data['slug'];
							if ( ! isset( $all_tabs[ $tab_slug ] ) ) {
								continue;
							}
							$tab_data = $all_tabs[ $tab_slug ];
							?>
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=sifency-theme-builder&tab=' . $tab_slug ) ); ?>" 
								class="sifency-accordion-summary sifency-nav-item sifency-admin-btn sifency-single-link <?php echo $active_tab === $tab_slug ? 'is-active' : ''; ?>"
								<?php echo ! empty( $tab_data['tooltip'] ) ? 'data-tooltip="' . esc_attr( $tab_data['tooltip'] ) . '"' : ''; ?>>

								<span class="sifency-nav-item-inner">
									<?php if ( ! empty( $tab_data['icon'] ) ) : ?>
										<span class="sifency-nav-item-icon dashicons <?php echo esc_attr( $tab_data['icon'] ); ?>"></span>
									<?php endif; ?>
									<span class="sifency-nav-item-label"><?php echo esc_html( $tab_data['label'] ); ?></span>
								</span>
							</a>
							<?php
						elseif ( isset( $group_data['type'] ) && 'group' === $group_data['type'] ) :
							$is_group_active = in_array( $active_tab, $group_data['items'] );
							?>
							<details class="sifency-nav-accordion <?php echo $is_group_active ? 'is-open' : ''; ?>" <?php echo $is_group_active ? 'open' : ''; ?>>
								<summary class="sifency-accordion-summary" role="button">
									<span class="sifency-accordion-title">
										<?php if ( ! empty( $group_data['icon'] ) ) : ?>
											<span class="sifency-accordion-icon dashicons <?php echo esc_attr( $group_data['icon'] ); ?>"></span>
										<?php endif; ?>
										<span class="sifency-accordion-text">
											<?php echo esc_html( $group_data['label'] ); ?>
										</span>
									</span>
									<span class="sifency-accordion-arrow"></span>
								</summary>

								<div class="sifency-accordion-content">
									<?php
									foreach ( $group_data['items'] as $tab_slug ) :
										if ( ! isset( $all_tabs[ $tab_slug ] ) ) {
											continue;
										}
										$tab_data = $all_tabs[ $tab_slug ];
										?>
										<a href="<?php echo esc_url( admin_url( 'admin.php?page=sifency-theme-builder&tab=' . $tab_slug ) ); ?>"
										class="sifency-nav-item sifency-admin-btn <?php echo $active_tab === $tab_slug ? 'is-active' : ''; ?>"
										<?php echo ! empty( $tab_data['tooltip'] ) ? 'data-tooltip="' . esc_attr( $tab_data['tooltip'] ) . '"' : ''; ?>>


											<?php if ( ! empty( $tab_data['icon'] ) ) : ?>
												<span class="sifency-nav-item-icon dashicons <?php echo esc_attr( $tab_data['icon'] ); ?>"></span>
											<?php endif; ?>

											<span class="sifency-nav-item-label"><?php echo esc_html( $tab_data['label'] ); ?></span>

											<?php if ( ! empty( $tab_data['beta'] ) ) : ?>
												<span class="sifency-beta-badge">Beta</span>
											<?php endif; ?>

											<?php if ( ! empty( $tab_data['sync_state'] ) && ! Sifency_Addons_Theme_Builder_Master::sync_environment_state() ) : ?>
												<span class="sifency-pro-badge">PRO</span>
											<?php endif; ?>

										</a>
									<?php endforeach; ?>
								</div>
							</details>
						<?php endif; ?>
												

					<?php endforeach; ?>
				</nav>

				<div class="sifency-dashboard-content">
					<?php sifency_addons_render_tab_content( $active_tab ); ?>
				</div>

			</div>
		</div>
		<div id="sifency-tooltip"></div>
		<?php sifency_addons_render_conditions_modal(); ?>
		<?php sifency_addons_render_new_template_modal( $all_tabs, $active_tab ); ?>
		<?php
	}
}