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

class Sifency_Addons_Data_Suite {

	/**
	 * @var Sifency_Admin_Import_UI
	 */
	private $import_ui;

	/**
	 * @var Sifency_Admin_Export_UI
	 */
	private $export_ui;

	/**
	 * @var Sifency_Admin_Reset_UI
	 */
	private $reset_ui;

	/**
	 * Constructor.
	 * Instantiates the necessary UI rendering classes.
	 */
	public function __construct() {
		$theme = wp_get_theme();
		if ( $theme->parent() ) {
			$theme = $theme->parent();
		}
		$theme_name = $theme->get( 'Name' );
		$theme_slug = Sifency_Demo_Helper::get_theme_slug();

		$this->import_ui = new Sifency_Admin_Import_UI( $theme_name, $theme_slug, 'import' );
		$this->export_ui = new Sifency_Admin_Export_UI( $theme_name, $theme_slug, 'export' );
		$this->reset_ui  = new Sifency_Admin_Reset_UI( 'reset' );
	}

	/**
	 * Renders the Data Suite page using WordPress-style tabs (no JS tab switching).
	 */
	public function render() {

		$current_page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : 'sifency-data-suite';
		$active_tab   = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'import';
		?>
		
		<div class="sifency-dashboard-layout">
			<!-- WordPress Native Tabs -->
			<div class="sifency-dashboard-nav">
				<a href="?page=<?php echo esc_attr( $current_page ); ?>&tab=import" 
				class="sifency-nav-item sifency-admin-btn nav-tab <?php echo ( $active_tab === 'import' ) ? 'nav-tab-active' : ''; ?>">
					<span class="sifency-nav-item-icon dashicons dashicons-download"></span>
					<?php esc_html_e( 'Import Tools', 'sifency-addons' ); ?>
				</a>

				<a href="?page=<?php echo esc_attr( $current_page ); ?>&tab=export" 
				class="sifency-nav-item sifency-admin-btn nav-tab <?php echo ( $active_tab === 'export' ) ? 'nav-tab-active' : ''; ?>">
					<span class="sifency-nav-item-icon dashicons dashicons-upload"></span>
					<?php esc_html_e( 'Export Tools', 'sifency-addons' ); ?>
				</a>

				<a href="?page=<?php echo esc_attr( $current_page ); ?>&tab=reset" 
				class="sifency-nav-item sifency-admin-btn nav-tab nav-tab-danger <?php echo ( $active_tab === 'reset' ) ? 'nav-tab-active' : ''; ?>">
					<span class="sifency-nav-item-icon dashicons dashicons-trash"></span>
					<?php esc_html_e( 'Reset Tools', 'sifency-addons' ); ?>
				</a>
			</div>

			<div class="sifency-admin-content sifency-dashboard-content">
					<?php
					switch ( $active_tab ) {
						case 'import':
							echo wp_kses_post( $this->import_ui->render() );
							break;

						case 'export':
							echo wp_kses_post( $this->export_ui->render() );
							break;

						case 'reset':
							echo wp_kses_post( $this->reset_ui->render() );
							break;

						default:
							echo '<p>' . esc_html__( 'Invalid tab selected.', 'sifency-addons' ) . '</p>';
							break;
					}
					?>
			</div>
		</div>
		<?php
	}
}
