<?php

namespace Tops\AmazonSellingPartnerAPI\Api;

class FbaInventoryApi extends BaseApi
{
    /**
     * Operation Get Inventory Summaries
     *
     * @param array $params['details'] true to return inventory summaries with additional summarized inventory details and quantities. Otherwise, returns inventory summaries only (default value).
     * @param enum $params['granularityType'] The granularity type for the inventory aggregation level. (required)
     * @param string $params['granularityId']  The granularity ID for the inventory aggregation level. (required)
     * @param string $params['startDateTime'] A start date and time in ISO8601 format. If specified, all inventory summaries that have changed since then are returned. You must specify a date and time that is no earlier than 18 months prior to the date and time when you call the API. Note: Changes in inboundWorkingQuantity, inboundShippedQuantity and inboundReceivingQuantity are not detected.
     * @param array|string $params['sellerSkus'] A list of seller SKUs for which to return inventory summaries. You may specify up to 50 SKUs.
     * @param string $params['nextToken'] String token returned in the response of your previous request.
     * @param string|array $params['marketplace_ids'] The marketplace ID for the marketplace for which to return inventory summaries. (required)
     *
     * @return json\
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * about more Ref: https://github.com/amzn/selling-partner-api-docs/blob/main/references/fba-inventory-api/fbaInventory.md#getinventorysummaries
     */
    public function getInventorySummaries($params)
    {
        if (!array_key_exists('marketplaceIds', $params) ||  null === $params['marketplaceIds']) {
            throw new \InvalidArgumentException('Missing the required parameter $marketplaceIds when calling getInventorySummaries');
        }

        if (!array_key_exists('granularityType', $params) ||  null === $params['granularityType']) {
            throw new \InvalidArgumentException('Missing the required parameter $granularityType when calling getInventorySummaries');
        }

        if (!array_key_exists('granularity_id', $params) ||  null === $params['granularity_id']) {
            throw new \InvalidArgumentException('Missing the required parameter $granularity_id when calling getInventorySummaries');
        }

        $resource_path = '/fba/inventory/v1/summaries';
        $url = $this->config->getHost() . $resource_path;
        $http_body = '';
        $query_params = [];
        $method = 'GET';

        // query params
        if (array_key_exists('details', $params) && $params['details'] !== null) {
            $query_params['details'] = is_bool( $params['details'] ) ? var_export( $params['details'], TRUE ) : $params['details'];
        }
        if (array_key_exists('granularityType', $params) && $params['granularityType'] !== null) {
            $query_params['granularityType'] = $params['granularityType'];
        }
        if (array_key_exists('granularity_id', $params) && $params['granularity_id'] !== null) {
            $query_params['granularityId'] = $params['granularity_id'];
        }
        if (array_key_exists('startDateTime', $params) && $params['startDateTime'] !== null) {
            $query_params['startDateTime'] = $params['startDateTime'];
        }
        if (array_key_exists('sellerSkus', $params) && $params['sellerSkus'] !== null) {
            $query_params['sellerSkus'] = is_array($params['sellerSkus']) ? implode(',', $params['sellerSkus']) :  $params['seller_skus'];
        }
        if (array_key_exists('nextToken', $params) && $params['nextToken'] !== null) {
            $query_params['nextToken'] = $params['nextToken'];
        }
        if (array_key_exists('marketplaceIds', $params) && $params['marketplaceIds'] !== null) {
            $query_params['marketplaceIds'] = is_array($params['marketplaceIds']) ? implode(',', $params['marketplaceIds']) :  $params['marketplaceIds'];
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }
}
