<?php
/**
 * Handles syncing of webshop settings.
 *
 * @package northmill-online-shared
 */

declare(strict_types=1);

namespace Northmill_Online_Shared\Modules\Sync\Store_Settings;

use Northmill_Online_Shared\Abstract_Shared_Base;
use Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\Models\Hub_Account_Model;
use Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\WP_Repository;
use Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\WP_Repository_Metas;
use Northmill_Online_Shared\Abstracts\Hub_API\Sync\Abstract_Syncer;
use Northmill_Online_Shared\Helpers\Hook_Helpers;
use Northmill_Online_Shared\Modules\Loggers\Log_Level_Enum;

defined( 'ABSPATH' ) || exit;

/**
 * Syncer class for Store settings.
 */
class Store_Settings_Sync extends Abstract_Syncer {

	/**
	 * Number of items to get in each API request.
	 *
	 * @var int
	 */
	protected int $page_size = 100;


	/**
	 * Construct class.
	 *
	 * @param Hub_Account_Model[] $backoffice_accounts Array of Hub accounts to sync stock for.
	 * @param string              $type                Type of syncer. Can be 'cron' or 'ajax'.
	 */
	public function __construct( array $backoffice_accounts = array(), string $type = 'cron' ) {
		if ( empty( $backoffice_accounts ) ) {
			$backoffice_accounts = WP_Repository::get_all_current_accounts();
		}
		parent::__construct( $backoffice_accounts, $type );
	}

	/**
	 * Sync store settings.
	 *
	 * @param null|array $args Arguments.
	 * @param bool       $is_first_sync If this sync is the first page of the first Hub account.
	 */
	public function sync( ?array $args = null, bool $is_first_sync = false ): bool {
		$current_backoffice_account = $this->get_current_hub_account();

		if ( ! $current_backoffice_account ) {
			return false;
		}

		$store_settings = $current_backoffice_account->api->get_store_settings();
		$this->update_status( 'total', 1 );

		if ( ! isset( $store_settings->Settings ) ) {
			Abstract_Shared_Base::base_instance()->logger->log(
				'sync',
				'Store settings not found in API response.',
				array(
					'response' => $store_settings,
				),
				Log_Level_Enum::ERROR
			);

			return false;
		}

		$org_num                    = $store_settings->Settings->OrganizationNumber ?? '';
		$org_name                   = $store_settings->Settings->CompanyName ?? '';
		$partial_refund_article_sku = $store_settings->Settings->PartialRefundArticleNumber ?? '';
		$shipping_article_sku       = (string) $store_settings->Settings->ShippingArticleNumber ?? '';

		if ( $org_num !== $current_backoffice_account->org_num ) {
			WP_Repository_Metas::update_single(
				$current_backoffice_account->wp_id,
				'org_num',
				$org_num
			);
		}

		if ( $org_name !== $current_backoffice_account->org_name ) {
			WP_Repository_Metas::update_single(
				$current_backoffice_account->wp_id,
				'org_name',
				$org_name
			);
		}

		if ( $partial_refund_article_sku !== $current_backoffice_account->partial_refund_article_sku ) {
			WP_Repository_Metas::update_single(
				$current_backoffice_account->wp_id,
				'partial_refund_article_sku',
				$partial_refund_article_sku
			);
		}

		$current_shipping_settings = $current_backoffice_account->get_shipping_settings();
		$current_shipping_sku      = $current_shipping_settings->get_article_sku();

		if ( $current_shipping_sku !== $shipping_article_sku ) {
			$current_shipping_settings->set_article_sku( $shipping_article_sku );

			WP_Repository_Metas::update_single(
				$current_backoffice_account->wp_id,
				'shipping_settings',
				$current_shipping_settings
			);
		}

		/**
		 * Action to run as part of the store settings sync.
		 *
		 * @param object $store_settings The store settings object from Hub API.
		 * @param bool   $is_main_hub_account If this is the main account.
		 * @param bool   $is_first_sync If this is the first sync.
		 * @param int    $wp_id The WP ID of the current backoffice account.
		 *
		 * @since 2.2.17
		 * @used Northmill Booking
		 */
		Hook_Helpers::do_scoped_action(
			'northmill_online_shared_store_settings_sync',
			$store_settings->Settings,
			$current_backoffice_account->is_main(),
			$is_first_sync,
			$current_backoffice_account->wp_id,
		);

		$this->increase_status( 'update' );

		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_display_name(): string {
		return __( 'Store Settings', 'northmill-online-shared' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_last_synced_wp_option_name(): string {
		return 'nmf_sync_store_settings_last_updated';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_last_synced_api_parameter(): ?string {
		return null;
	}
}
