<?php
/**
 * Admin Settings Controller - Manages CPT modules enable/disable UI
 *
 * @package Sifency_Addons
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;  }

/**
 * Class Sifency_Addons_CPT_Modules_Settings
 *
 * Handles the admin settings page for managing CPT module enable/disable.
 */
class Sifency_Addons_CPT_Modules_Settings {

	/**
	 * Constructor - Initialize admin hooks.
	 */
	public function __construct() {
		add_action( 'wp_ajax_sifency_toggle_cpt_module', array( $this, 'handle_module_toggle' ) );
	}

	/**
	 * Render the admin page.
	 */
	public function render_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'sifency-addons' ) );
		}

		$all_modules = Sifency_Addons_CPT_Module_Manager::get_all_modules();
		?>
		<div class="wrap sifency-cpt-modules-wrap">
			<h1><?php esc_html_e( 'CPT Modules Management', 'sifency-addons' ); ?></h1>
			<p class="description">
				<?php esc_html_e( 'Enable or disable custom post type modules. Disabled modules will not load their files or register their classes.', 'sifency-addons' ); ?>
			</p>

			<table class="wp-list-table widefat striped sifency-modules-table">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Module', 'sifency-addons' ); ?></th>
						<th><?php esc_html_e( 'Status', 'sifency-addons' ); ?></th>
						<th><?php esc_html_e( 'Action', 'sifency-addons' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $all_modules as $module_key => $config ) : ?>
						<?php
						$is_enabled   = Sifency_Addons_CPT_Module_Manager::is_module_enabled( $module_key );
						$status_class = $is_enabled ? 'status-enabled' : 'status-disabled';
						$status_text  = $is_enabled ? esc_html__( 'Enabled', 'sifency-addons' ) : esc_html__( 'Disabled', 'sifency-addons' );
						$button_text  = $is_enabled ? esc_html__( 'Disable', 'sifency-addons' ) : esc_html__( 'Enable', 'sifency-addons' );
						?>
						<tr data-module="<?php echo esc_attr( $module_key ); ?>">
							<td class="module-name">
								<?php if ( ! empty( $config['icon'] ) ) : ?>
									<span class="dashicons <?php echo esc_attr( $config['icon'] ); ?>"></span>
								<?php endif; ?>
								<strong><?php echo esc_html( $config['label'] ); ?></strong>
							</td>
							<td class="module-status">
								<span class="status-badge <?php echo esc_attr( $status_class ); ?>">
									<?php echo esc_html( $status_text ); ?>
								</span>
							</td>
							<td class="module-action">
								<button class="button button-primary sifency-toggle-btn" 
									data-module="<?php echo esc_attr( $module_key ); ?>"
									data-nonce="<?php echo esc_attr( wp_create_nonce( 'sifency_toggle_cpt_' . $module_key ) ); ?>">
									<?php echo esc_html( $button_text ); ?>
								</button>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>

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


		<?php
	}

	/**
	 * Handle AJAX toggle request.
	 */
	public function handle_module_toggle() {
		$nonce  = isset( $_POST['nonce'] ) ? $_POST['nonce'] : '';
		$module = isset( $_POST['module'] ) ? sanitize_key( wp_unslash( $_POST['module'] ) ) : '';

		if ( ! wp_verify_nonce( $nonce, 'sifency_toggle_cpt_' . $module ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Security verification failed.', 'sifency-addons' ) ) );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Insufficient permissions.', 'sifency-addons' ) ) );
		}

		$is_reset = isset( $_POST['reset'] ) ? (bool) wp_unslash( $_POST['reset'] ) : false;

		if ( $is_reset ) {
			Sifency_Addons_CPT_Module_Manager::reset_to_defaults();
			wp_send_json_success( array( 'message' => esc_html__( 'Modules reset to defaults.', 'sifency-addons' ) ) );
		}

		if ( empty( $module ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Invalid module.', 'sifency-addons' ) ) );
		}

		$new_status = Sifency_Addons_CPT_Module_Manager::toggle_module( $module );

		wp_send_json_success(
			array(
				'enabled' => $new_status,
				'message' => $new_status ? esc_html__( 'Module enabled.', 'sifency-addons' ) : esc_html__( 'Module disabled.', 'sifency-addons' ),
			)
		);
	}
}
