<?php

/**
 * Handles syncing of bookkeeping vat accounts.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Sync\Bookkeeping\Vat_Account_Codes;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstract_Shared_Base;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\Models\Vat_Account_Code_Model;
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;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Loggers\Log_Level_Enum;
defined('ABSPATH') || exit;
/**
 * Class Sync
 */
class Vat_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);
    }
    /**
     * Syncs bookkeeping vat accounts to WooCommerce
     *
     * @param array|null $args Arguments to pass to API, or null if no 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;
        }
        $vat_accounts = $current_backoffice_account->api->get_vat_accounts($args);
        if (!isset($vat_accounts['list']) || !isset($vat_accounts['Page']) || !isset($vat_accounts['Total'])) {
            Abstract_Shared_Base::base_instance()->logger->log('sync', 'No VAT account codes found in Hub', array('vat_accounts' => $vat_accounts), Log_Level_Enum::WARNING);
            return \true;
            // This isn't necessarily an error, so we return true.
        }
        $this->update_status('page', $vat_accounts['Page']);
        $this->update_status('total', $vat_accounts['Total']);
        $account_codes = array();
        if (is_array($vat_accounts['list'])) {
            foreach ($vat_accounts['list'] as $item) {
                if (empty($item['VatAccountSetting'])) {
                    $this->increase_status('error');
                    continue;
                }
                $account_code = isset($item['VatAccountSetting']['AccountNumber']) ? intval($item['VatAccountSetting']['AccountNumber']) : null;
                $var_rate = isset($item['VatAccountSetting']['VatRate']) ? intval($item['VatAccountSetting']['VatRate']) : null;
                $account_codes[$var_rate] = new Vat_Account_Code_Model(array('id' => (string) $item['VatAccountSetting']['Id'], 'account_code' => $account_code, 'vat_rate' => $var_rate));
                $this->increase_status('update');
            }
            WP_Repository_Metas::update_single($current_backoffice_account->wp_id, 'vat_account_codes', $account_codes);
        }
        return \true;
    }
    /**
     * {@inheritDoc}
     */
    public function get_display_name(): string
    {
        return __('VAT Account Codes', 'northmill-online-shared');
    }
    /**
     * {@inheritDoc}
     */
    public function get_last_synced_wp_option_name(): string
    {
        return 'mf_sync_bookkeeping_last_updated';
    }
    /**
     * {@inheritDoc}
     */
    public function get_last_synced_api_parameter(): ?string
    {
        return null;
    }
}
