<?php

namespace Ezypay\Resource;

use Ezypay\Contract\IDriver;
use Ezypay\Contract\IResourceCreate;
use Ezypay\Contract\IResourceCustomer;
use Ezypay\Contract\IResourceUpdate;
use Ezypay\Result\ResultProducer;
use Ezypay\Validation\ValidationBase;
use Ezypay\Validation\Validator\NotEmptyString;

/**
 * Class Customer
 * @package Ezypay\Resource
 */
class Customer extends Resurce implements IResourceCustomer, IResourceCreate, IResourceUpdate
{
    /**
     * @var string defautl object name
     */
    private $resourceType = 'Customer';

    /**
     * Customer constructor.
     * @param IDriver $connector
     * @param ValidationBase $validation
     */
    public function __construct(IDriver $connector, ValidationBase $validation)
    {

        parent::__construct($connector, $validation, $this->resourceType);
    }

    /**
     * Get the list of customers. You can further filter the list of customer that relates to the individual merchant using merchantId.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Customers/CustomersApi_GetCustomerList
     * @param array $data EzyPay API parametes (see link)
     * @return ResultProducer
     */
    public function findAll(array $data = array())
    {
        $this->settings(array(
            'url' => !empty($data) ? 'customers?' . http_build_query($data) : 'customers',
            'method' => 'GET'
        ));

        return $this->result();
    }

    /**
     * Retrieve the details of an existing customer.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Customers/CustomersApi_GetCustomerById
     * @param $id string EzyPay customer ID
     * @return ResultProducer
     */
    public function findById($id)
    {

        $this->settings(array(
            'url' => 'customers/' . $id,
            'method' => 'GET'
        ));
        return $this->result();
    }

    /**
     * Retrieve the details of an existing customer using referenceId.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Customers/CustomersApi_GetCustomerByReferenceId
     * @param $id string The reference ID of the customer. A unique identifier from a third-party software integration.
     * @return ResultProducer
     */
    public function findByReferenceId($id)
    {
        $this->settings(array(
            'url' => 'customers/reference/' . $id,
            'method' => 'GET'
        ));
        return $this->result();
    }

    /**
     * Add the customer along with optional details such as address, payment method, and subscriptions which you can provide later on
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Customers/CustomersApi_CreateCustomer
     * @param array $data The customer data. Refer to request body schema (see link)
     * @return ResultProducer
     */
    public function create(array $data)
    {

        $this->settings(array(
            'url' => 'customers',
            'method' => 'POST',
            'data' => $data
        ));
        
        $this->addValidator(new NotEmptyString($data['referenceId'],'referenceId is required' ));
        return $this->result();
    }

    /**
     * Update an existing customer details.
     *
     * @link https://demoapi.ezypay.com/help/docs#!/Customers/CustomersApi_UpdateCustomer
     * @param $id string The ID of the customer. A unique identifier generated by the system
     * @param array $data The customer data. Refer to request body schema (see link)
     * @return ResultProducer
     */
    public function update($id, array $data)
    {
        $this->settings(array(
            'url' => 'customers/' . $id,
            'method' => 'PUT',
            'data' => $data
        ));

        return $this->result();
    }

}
