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

class Sifency_Addons_Admin_Options_CPT {

	public function render_cpt_tab( $options ) {
		if ( ! class_exists( 'Sifency_Addons_CPT_Module_Manager' ) ) {
			echo '<div class="notice notice-error"><p>CPT Module Manager not found!</p></div>';
			return;
		}

		$all_modules = Sifency_Addons_CPT_Module_Manager::get_all_modules();
		if ( ! is_array( $all_modules ) || empty( $all_modules ) ) {
			echo '<div class="notice notice-error"><p>No CPT modules available!</p></div>';
			return;
		}
		?>
		<?php
		$is_pro_active = false;
		if ( class_exists( 'Sifency_Addons_Theme_Builder_Master' ) && method_exists( 'Sifency_Addons_Theme_Builder_Master', 'sync_environment_state' ) ) {
			try {
				$is_pro_active = Sifency_Addons_Theme_Builder_Master::sync_environment_state();
			} catch ( Exception $e ) {
				$is_pro_active = false;
			}
		}
		if ( ! $is_pro_active && class_exists( 'Sifency_Addons_CPT_Module_Manager' ) ) {
			try {
				$is_pro_active = Sifency_Addons_CPT_Module_Manager::is_pro_active();
			} catch ( Exception $e ) {
				$is_pro_active = false;
			}
		}
		?>
		<div class="sifency-modules-grid">
			<?php
			foreach ( $all_modules as $module_key => $config ) :
				if ( 'edd' === $module_key ) {
					continue;
				}
				if ( 'shop' === $module_key && class_exists( 'WooCommerce' ) ) {
					continue;
				}
				if ( 'blog' === $module_key ) {
					continue;
				}

				if ( ! isset( $config['label'], $config['icon'] ) ) {
					continue;
				}

				$is_enabled = Sifency_Addons_CPT_Module_Manager::is_module_enabled( $module_key );

				$tooltip = '';
				if ( is_array( $config ) && ! empty( $config['tooltip'] ) ) {
					$tooltip = $config['tooltip'];
				}

				?>
				<div class="sifency-module-card">
					<div class="sifency-module-card-header">
						<span class="dashicons <?php echo esc_attr( $config['icon'] ); ?>"></span>
						<h3><?php echo esc_html( $config['label'] ); ?></h3>
						<?php if ( ! $is_pro_active ) : ?>
							<span class="sifency-pro-badge">Pro</span>
						<?php endif; ?>
					</div>
					<div class="sifency-module-card-footer">
						
						<label class="sifency-switch" data-tooltip="<?php echo esc_attr( $tooltip ); ?>">
							<input type="checkbox"
								class="sifency-ajax-save sifency-cpt-module-toggle"
								data-setting="cpt_enabled|<?php echo esc_attr( $module_key ); ?>"
								data-module="<?php echo esc_attr( $module_key ); ?>"
								<?php checked( $is_enabled, true ); ?>
								<?php echo $is_pro_active ? '' : 'disabled="disabled"'; ?>
							>
							<span class="slider round"></span>
						</label>


						<span class="sifency-module-status <?php echo $is_enabled ? 'status-enabled' : 'status-disabled'; ?>">
							<?php echo $is_enabled ? esc_html__( 'Enabled', 'sifency-addons' ) : esc_html__( 'Disabled', 'sifency-addons' ); ?>
						</span>
					</div>
				</div>
			<?php endforeach; ?>
		</div>

		<div class="sifency-cpt-actions">
			<button id="sifency-reset-cpt-defaults" class="button button-secondary sifency-admin-btn" type="button">
				<?php esc_html_e( 'Reset to Defaults', 'sifency-addons' ); ?>
			</button>
		</div>
		<?php
	}
}
