<?php

/**
 * Hub Accounts Helpers
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\API;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\Models\Hub_Account_Model;
defined('ABSPATH') || exit;
/**
 * Class Helpers
 */
class Helpers
{
    /**
     * Display Hub accounts connection status.
     */
    public static function render_settings_notices(): void
    {
        $failed_accounts = Invalid_Hub_Accounts::get_invalid_accounts();
        if (!empty($failed_accounts)) {
            echo '<div class="error notice">';
            echo '<p>' . esc_html__('Sorry, we\'re unable to connect with the Hub API for the following accounts, please try again.', 'northmill-online-shared') . '</p>';
            echo '<ul>';
            foreach ($failed_accounts as $account) {
                echo '<li>' . esc_html($account->url . ' - ' . $account->user) . '</li>';
            }
            echo '</ul>';
            echo '</div>';
        }
        Invalid_Hub_Accounts::clear_invalid_accounts();
    }
    /**
     * Test connection to Northmill API, and show results.
     *
     * @param Hub_Account_Model $account Account to test.
     *
     * @return string Connection status HTML.
     */
    public static function get_connection_status(Hub_Account_Model $account): string
    {
        if (!$account->is_enabled()) {
            return '';
        }
        $api = new API($account);
        $store_settings = $api->get_store_settings();
        if (!empty($store_settings->Settings)) {
            return '<p style="color: green;">' . esc_html__('Connection successful!', 'northmill-online-shared') . '</p>';
        }
        return '<p style="color: red;">' . esc_html__('Connection failed!', 'northmill-online-shared') . '</p>';
    }
}
