<?php

namespace endpoints\customers;

use traits\economic_endpoint_t;

class economic_customers_endpoint
{
    use economic_endpoint_t;

    /**
     * Get a customer by customer number
     * @param int $customer_number
     * @return object
     */
    public function get(int $customer_number): object
    {
        $response = $this->send_request(
            '/customers/' . $customer_number,
            'GET');
        // Return the response as an object
        return json_decode($response);
    }
    /**
     * Create a customer
     * @param array $data
     * @return object
     */
    public function create(array $data): object
    {
        $response = $this->send_request(
            '/customers/',
            'POST',
            json_encode($data)
        );
        return json_decode($response);
    }

    /**
     * Get customers by filters
     * @param array $filters Example: ['filter' => 'value']
     * @param array $pagination Example: ['maxPageSize' => 100, 'skipPages' => 0]
     * @return object {collection: [product], pagination: {maxPageSize: number, skipPages: number, results: number}}
     */
    public function search(array $filters = [], array $pagination = []): object
    {
        $response = $this->send_request(
            '/customers/?filter=' . self::filters($filters) . self::pagination($pagination),
            'GET');
        // Return the response as an object
        return json_decode($response);
    }

    /**
     * Does a customer exist?
     * @param int $customer_number
     * @return bool
     */
    public function exists(int $customer_number): bool
    {
        $response = $this->send_request(
            '/customers/' . $customer_number,
            'GET');
        $tmp = json_decode($response);
        // Return true if the customer exists
        return isset($tmp->customerNumber);
    }
}