<?php
/**
 * @author Aitoc Team
 * @copyright Copyright (c) 2020 Aitoc (https://www.aitoc.com)
 * @package Aitoc_PreOrders
 */


namespace Aitoc\PreOrders\Model\InventorySales;

use Aitoc\PreOrders\Helper\PreOrder as PreOrderHelper;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventoryReservationsApi\Model\GetReservationsQuantityInterface;
use Magento\InventorySales\Model\IsProductSalableForRequestedQtyCondition\IsSalableWithReservationsCondition as InventoryIsSalableWithReservationsCondition;
use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\Catalog\Model\ResourceModel\Product as ProductResourceModel;

class IsSalableWithReservationsCondition extends InventoryIsSalableWithReservationsCondition
{
    /**
     * @var ProductResourceModel
     */
    private $productResourceModel;

    /**
     * @var PreOrderHelper
     */
    private $preOrderHelper;

    /**
     * @var ProductSalableResultInterfaceFactory
     */
    public $productSalableResultFactory;

    /**
     * @var ProductSalabilityErrorInterfaceFactory
     */
    public $productSalabilityErrorFactory;

    public function __construct(
        GetStockItemDataInterface $getStockItemData,
        GetReservationsQuantityInterface $getReservationsQuantity,
        GetStockItemConfigurationInterface $getStockItemConfiguration,
        ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory,
        ProductSalableResultInterfaceFactory $productSalableResultFactory,
        ProductResourceModel $productResourceModel,
        PreOrderHelper $preOrderHelper
    ) {
        parent::__construct($getStockItemData, $getReservationsQuantity, $getStockItemConfiguration,
            $productSalabilityErrorFactory, $productSalableResultFactory);

        $this->productResourceModel = $productResourceModel;
        $this->preOrderHelper = $preOrderHelper;
        $this->productSalableResultFactory = $productSalableResultFactory;
        $this->productSalabilityErrorFactory = $productSalabilityErrorFactory;
    }

    /**
     * @param string $sku
     * @param int    $stockId
     * @param float  $requestedQty
     *
     * @return ProductSalableResultInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface
    {
        $productId = current($this->productResourceModel->getProductsIdsBySkus([$sku]));
        $errors = [];

        if (!$this->preOrderHelper->isQtyAvailable($productId, $requestedQty)) {
            $errors = [
                $this->productSalabilityErrorFactory->create([
                    'code' => 'is_salable_with_reservations-not_enough_qty',
                    'message' => __('The requested qty is not available')
                ])
            ];
        }

        return $this->productSalableResultFactory->create(['errors' => $errors]);
    }
}
