<?php
/**
 * Admin tools page.
 *
 * @package flo-sync
 *
 * @phpcs:disable Generic.Commenting.DocComment.MissingShort
 * @phpcs:disable Squiz.Commenting.FunctionComment.MissingParamTag
 */

namespace NMFS\Admin\Pages;

use NMFS\Shared;

defined( 'ABSPATH' ) || exit;

/**
 * Class Admin_Tools
 */
class Admin_Tools {
	private const AJAX_KEY_REMOVE_SYNC_3_DATA = 'remove_sync_3_data';
	public const OPTION_SYNC_3_DATA_REMOVED   = 'northmill_sync_3_data_removed';

	/**
	 * Set up hooks to Shared.
	 *
	 * @param string $service_name_underscored The service name in underscored format.
	 *
	 * @return void
	 */
	public static function setup_shared_hooks( string $service_name_underscored ): void {
		add_filter(
			'northmill_online_shared_admin_tools_tabs_' . $service_name_underscored,
			array( self::class, 'filter_tabs' )
		);

		add_action(
			'northmill_online_shared_admin_page_ajax_tools_' . $service_name_underscored,
			array( self::class, 'register_ajax_actions' )
		);

		add_action(
			'northmill_online_shared_admin_page_script_localization_tools_' . $service_name_underscored,
			array( self::class, 'get_script_localization' ),
			10,
			0
		);
	}

	/**
	 * Filter the tabs on the tools page.
	 *
	 * @param array $tabs The current tabs.
	 *
	 * @return array The updated tabs
	 */
	public static function filter_tabs( array $tabs ): array {
		$tabs['remove_sync_3_data'] = array(
			'title'         => __( 'Remove Sync 3 data', 'flo-sync' ),
			'template_part' => 'remove-sync-3-data',
			'view_params'   => array(
				'sync_3_data_removed' => get_option( self::OPTION_SYNC_3_DATA_REMOVED, false ),
			),
		);

		return $tabs;
	}


	/**
	 * Register the ajax actions.
	 */
	public static function register_ajax_actions(): void {
		Shared::instance()->ajax->add_action(
			self::AJAX_KEY_REMOVE_SYNC_3_DATA,
			array( self::class, 'ajax_remove_sync_3_data' )
		);
	}

	/**
	 * Add the script localization for the page.
	 */
	public static function get_script_localization(): array {

		$loc['remove_sync_3_data_ajax'] = Shared::instance()->ajax->get_action( self::AJAX_KEY_REMOVE_SYNC_3_DATA )?->get_data_for_js();

		$loc['l10n'] = array(
			'confirm_remove_data' => __( 'Are you sure you want to remove all Sync 3 data? This cannot be undone and will prevent downgrading.', 'flo-sync' ),
		);

		return $loc;
	}

	/**
	 * Remove Sync 3 data.
	 *
	 * @return void
	 */
	public static function ajax_remove_sync_3_data(): void {
		global $wpdb;
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( 'Unauthorized', 403 );
		}

		$posts = get_posts(
			array(
				'post_type'      => 'mf_bo_accounts',
				'posts_per_page' => -1,
				'post_status'    => 'any',
				'fields'         => 'ids',
			)
		);
		foreach ( $posts as $post_id ) {
			wp_delete_post( $post_id, true );
		}

		delete_option( 'northmill_flo_settings' );
		delete_option( 'mf_sync_group_last_synced' );
		delete_option( 'nmf_settings_backup_pre.3.0.0' );
		delete_option( 'nmf_article_account_codes' );

		$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'nmf_%_from_pre_3.0.0'" );

		$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_nmf_stock_data'" );
		$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key LIKE 'nmf_bo_account_%'" );
		$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_nmf_availability_meta'" );

		Shared::instance()->admin->notices->get_notice( '4.0-upgrade-clean-up' )?->set_closed();

		update_option( self::OPTION_SYNC_3_DATA_REMOVED, true );
		wp_send_json_success( 'Success' );
	}
}
