<?php

/**
 * API requests for the main API account (rather than stock accounts).
 *
 * @package northmill-online-shared
 *
 * @noinspection PhpUnused Unused are for external.
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstracts\Hub_API;

use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstract_Shared_Base;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\Models\Hub_Account_Model;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\WP_Repository;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstracts\Hub_API\Models\Hub_API_Properties;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Modules\Loggers\Log_Level_Enum;
defined('ABSPATH') || exit;
/**
 * Class Flo_Main_API
 */
class Main_API extends API
{
    /**
     * Set up API.
     *
     * @param string|null            $env     Hub API environment. Defaults to environment set in options.
     *                                        Ignored if $account is set.
     * @param Hub_Account_Model|null $account Hub account to use. Defaults to the main account in env.
     */
    public function __construct(?string $env = null, ?Hub_Account_Model $account = null)
    {
        if (!$account) {
            $account = $env ? WP_Repository::get_main_account($env) : WP_Repository::get_current_main_account();
        }
        if (!$account) {
            Abstract_Shared_Base::base_instance()->logger->log('sync', 'Could not find main account in env', array('env' => $env), Log_Level_Enum::ERROR);
            return;
        } elseif (!$account->is_main()) {
            Abstract_Shared_Base::base_instance()->logger->log('sync', 'Account is not main account in env', array('account_basics' => $account->get_account_basics_for_logging(), 'env' => $env), Log_Level_Enum::ERROR);
            return;
        }
        parent::__construct($account);
    }
    /**
     * Get article sets.
     *
     * @param array $args Query parameters.
     *
     * @return array|null    Decoded JSON array of article sets, or null if request failed.
     */
    public function get_article_sets(array $args): ?array
    {
        return $this->request_array('GET', 'articlesets', $args);
    }
    /**
     * Update article in API
     *
     * @param array $article Article data.
     *
     * @return false|string
     */
    public function put_articles(array $article): false|string
    {
        if (isset($article[Hub_API_Properties::ARTICLE_QUANTITY])) {
            unset($article[Hub_API_Properties::ARTICLE_QUANTITY]);
        }
        $body = wp_json_encode(array($article));
        $body = $body ? $body : '';
        $response = $this->request('PUT', API::ARTICLES_ENDPOINT, $body);
        if (!$response) {
            return \false;
        }
        return $this->is_edited_article_successful($response) ? $response : \false;
    }
    /**
     * Create article in API
     *
     * @param array $article Article data.
     *
     * @return false|string
     */
    public function post_articles(array $article): false|string
    {
        $body = wp_json_encode(array($article));
        $body = $body ? $body : '';
        $response = $this->request('POST', API::ARTICLES_ENDPOINT, $body);
        if (!$response) {
            return \false;
        }
        return $this->is_edited_article_successful($response) ? $response : \false;
    }
    /**
     * Validate API response for created/updated article.
     *
     * @param string $response API response.
     *
     * @return bool
     */
    private function is_edited_article_successful(string $response): bool
    {
        $response_object = json_decode($response, \true);
        if (!isset($response_object['list']) || !is_array($response_object['list'])) {
            return \false;
        }
        foreach ($response_object['list'] as $response) {
            if (empty($response['Success'])) {
                return \false;
            }
        }
        return \true;
    }
    /**
     * Get article groups.
     *
     * @param array $args Query parameters.
     *
     * @return array|null    Decoded JSON array of groups, or null if request failed.
     */
    public function get_groups(array $args): ?array
    {
        return $this->request_array('GET', 'groups', $args);
    }
    /**
     * Get articles.
     *
     * @param array $args Query parameters.
     *
     * @return array|null    Decoded JSON array of articles, or null if request failed.
     */
    public function get_campaigns(array $args): ?array
    {
        return $this->request_array('GET', 'campaigns', $args);
    }
}
