<?php

/**
 * Customer Model
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Modules\Sync\Customers\Models;

use JsonSerializable;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstracts\Data_Model;
defined('ABSPATH') || exit;
/**
 * Class Customer_Model
 */
class Customer_Model extends Data_Model implements JsonSerializable
{
    /**
     * WooCommerce Customer ID.
     *
     * @var int
     */
    public int $wc_customer_id = 0;
    /**
     * Hub Customer Number.
     *
     * @var string
     */
    public string $CustomerNumber;
    /**
     * Customer Name.
     *
     * @var string
     */
    public string $Name;
    /**
     * Address Line 1.
     *
     * @var string
     */
    public string $Address1;
    /**
     * Address Line 2.
     *
     * @var string
     */
    public string $Address2;
    /**
     * Post Code.
     *
     * @var string
     */
    public string $PostCode;
    /**
     * City.
     *
     * @var string
     */
    public string $City;
    /**
     * Country.
     *
     * @var string
     */
    public string $Country;
    /**
     * Email.
     *
     * @var string
     */
    public string $Email;
    /**
     * Phone.
     *
     * @var string
     */
    public string $Mobile;
    /**
     * Constructor.
     *
     * @param array $args Arguments.
     */
    public function __construct(array $args = array())
    {
        if (empty($args['wc_customer_id']) && !empty($args['Email'])) {
            $woo_customer = get_user_by('email', $args['Email']);
            if (!empty($woo_customer->ID)) {
                $args['wc_customer_id'] = $woo_customer->ID;
            }
        }
        if (!empty($args['wc_customer_id']) && !is_int($args['wc_customer_id'])) {
            $args['wc_customer_id'] = is_numeric($args['wc_customer_id']) ? (int) $args['wc_customer_id'] : null;
        }
        parent::__construct($args);
    }
    /**
     * Get json data.
     */
    public function jsonSerialize(): array
    {
        $properties = get_object_vars($this);
        $properties_to_skip = array('wc_customer_id');
        $customer = array_filter($properties, function ($value, $key) use ($properties_to_skip) {
            return !in_array($key, $properties_to_skip, \true) && !empty($value);
        }, \ARRAY_FILTER_USE_BOTH);
        if (!isset($customer['CustomerNumber'])) {
            $customer['CustomerNumber'] = '';
        }
        return $customer;
    }
}
