<?php

/**
 * Fired during plugin deactivation
 *
 * @link       https://wpiron.com
 * @since      1.0.0
 *
 * @package    iron-security
 * @subpackage iron-security/includes
 */

class iron_security_Deactivator {

	public static function deactivate() {
		self::initialize_filesystem();

		self::flush_rewrite_rules();
		self::remove_plugin_options();
		self::drop_access_log_table();
		self::remove_ai_bot_rules_from_htaccess();
		self::remove_custom_htaccess_rules();
		self::remove_disallow_file_edit_from_config();

	}

	private static function initialize_filesystem() {
		if ( ! function_exists( 'request_filesystem_credentials' ) ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
		}

		global $wp_filesystem;

		$creds = request_filesystem_credentials( '', '', false, false, null );
		if ( ! WP_Filesystem( $creds ) ) {
			return false;
		}

		return true;
	}

	private static function flush_rewrite_rules() {
		flush_rewrite_rules();
	}

	private static function drop_access_log_table() {
		global $wpdb;
		$table_name = $wpdb->prefix . 'iron_security_logs';
		$wpdb->query( "DROP TABLE IF EXISTS $table_name" );
	}

	private static function remove_ai_bot_rules_from_htaccess() {
		global $wp_filesystem;
		$htaccess_path = ABSPATH . '.htaccess';

		if ( ! $wp_filesystem->exists( $htaccess_path ) ) {
			return;
		}

		$htaccess_content = $wp_filesystem->get_contents( $htaccess_path );
		$cleaned_content = preg_replace( '/\n?# Ultimate AI Bot Block List.*?<\\/IfModule>\n/s', '', $htaccess_content );

		if ( $cleaned_content !== $htaccess_content ) {
			$wp_filesystem->put_contents( $htaccess_path, $cleaned_content, FS_CHMOD_FILE );
		}
	}

	private static function remove_plugin_options() {
		delete_option( 'wpironis_options' );
		delete_option( 'wpironis_plugin_settings_loginlogout' );
		delete_option( 'iron_security_2fa_settings' );
	}

	private static function remove_custom_htaccess_rules() {
		$upload_dir = wp_upload_dir();
		$uploads_htaccess = $upload_dir['basedir'] . '/.htaccess';
		$index_file = $upload_dir['basedir'] . '/index.php';

		self::remove_block_from_file( ABSPATH . '.htaccess', '# Iron Security', '# END Iron Security' );
		self::remove_block_from_file( WP_CONTENT_DIR . '/.htaccess', '# Iron Security', '# END Iron Security' );
		self::remove_block_from_file( $uploads_htaccess, '<Files *.php>', '</Files>' );
		self::remove_block_from_file( $uploads_htaccess, '# Iron Security', '# END Iron Security' );

		global $wp_filesystem;
		if ( $wp_filesystem->exists( $index_file ) ) {
			$wp_filesystem->delete( $index_file );
		}
	}

	private static function remove_disallow_file_edit_from_config() {
		global $wp_filesystem;

		$config_path = ABSPATH . 'wp-config.php';

		if ( ! $wp_filesystem->exists( $config_path ) || ! $wp_filesystem->is_writable( $config_path ) ) {
			return;
		}

		$config = $wp_filesystem->get_contents( $config_path );
		$config = preg_replace(
			"/\/\/ IRON SECURITY: DISALLOW_FILE_EDIT\n\s*define\(\s*'DISALLOW_FILE_EDIT'\s*,\s*true\s*\);\s*/",
			'',
			$config
		);

		$wp_filesystem->put_contents( $config_path, $config, FS_CHMOD_FILE );
	}

	private static function remove_block_from_file( $file_path, $start_marker, $end_marker ) {
		global $wp_filesystem;

		if ( ! $wp_filesystem->exists( $file_path ) || ! $wp_filesystem->is_writable( $file_path ) ) {
			return;
		}

		$content = $wp_filesystem->get_contents( $file_path );
		$pattern = '/' . preg_quote( $start_marker, '/' ) . '.*?' . preg_quote( $end_marker, '/' ) . '\s*/s';
		$cleaned = preg_replace( $pattern, '', $content );

		if ( $cleaned !== $content ) {
			$wp_filesystem->put_contents( $file_path, $cleaned, FS_CHMOD_FILE );
		}
	}
}
