<?php

/**
 * Sync other account codes from Hub.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Sync\Bookkeeping\Other_Account_Codes;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\WP_Repository;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\WP_Repository_Metas;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Sync\Abstract_Syncer;
defined('ABSPATH') || exit;
/**
 * Class Sync
 */
class Other_Account_Code_Sync extends Abstract_Syncer
{
    /**
     * Page size will not be changed by ajax (or any other) request.
     *
     * @var bool $force_page_size
     */
    protected bool $force_page_size = \true;
    /**
     * Sync request page size.
     *
     * @var int $page_size
     */
    protected int $page_size = 1000;
    /**
     * Construct class.
     *
     * @param array  $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();
        }
        parent::__construct($backoffice_accounts, $type);
    }
    /**
     * Sync account codes from Hub WooCommerce.
     *
     * @param array|null $args API 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;
        }
        $data = $current_backoffice_account->api->get_other_account_codes($args);
        if (!$data || !isset($data->list) || !is_array($data->list)) {
            return \false;
        }
        $this->update_status('page', $data->Page);
        $this->update_status('total', $data->Total);
        $current_shipping_settings = $current_backoffice_account->get_shipping_settings();
        foreach ($data->list as $account) {
            if ('Shipping' === ($account->OtherAccountSetting->AccountType ?? '')) {
                $api_account_number = (int) $account->OtherAccountSetting->AccountNumber ?? null;
                $current_shipping_account = $current_shipping_settings->get_article_account_code();
                if ($current_shipping_account !== $api_account_number) {
                    $current_shipping_settings->set_article_account_code($api_account_number);
                    WP_Repository_Metas::update_single($current_backoffice_account->wp_id, 'shipping_settings', $current_shipping_settings);
                }
                break;
            }
        }
        return \true;
    }
    /**
     * {@inheritDoc}
     */
    public function get_display_name(): string
    {
        return __('Other Account Codes', 'northmill-online-shared');
    }
    /**
     * {@inheritDoc}
     */
    public function get_last_synced_wp_option_name(): string
    {
        return 'mf_sync_other_account_codes_last_updated';
    }
    /**
     * {@inheritDoc}
     */
    public function get_last_synced_api_parameter(): ?string
    {
        return null;
    }
}
