<?xml version="1.0"?>
<!--
/**
 * Copyright © Zhik Pty Ltd. All rights reserved.
 * See LICENSE.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <!-- CSV Processor Configuration -->
    <type name="Zhik\ForwardOrder\Model\Pricing\CsvProcessor">
        <arguments>
            <argument name="logger" xsi:type="object">Zhik\ForwardOrder\Model\Logger</argument>
        </arguments>
    </type>

    <!-- Validator Configuration -->
    <type name="Zhik\ForwardOrder\Model\Pricing\Validator">
        <arguments>
            <argument name="productRepository" xsi:type="object">Magento\Catalog\Api\ProductRepositoryInterface</argument>
            <argument name="searchCriteriaBuilder" xsi:type="object">Magento\Framework\Api\SearchCriteriaBuilder</argument>
            <argument name="logger" xsi:type="object">Zhik\ForwardOrder\Model\Logger</argument>
        </arguments>
    </type>

    <!-- Logger Configuration -->
    <virtualType name="Zhik\ForwardOrder\Model\Logger" type="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="handlers" xsi:type="array">
                <item name="system" xsi:type="object">Zhik\ForwardOrder\Model\Logger\Handler</item>
            </argument>
        </arguments>
    </virtualType>

    <virtualType name="Zhik\ForwardOrder\Model\Logger\Handler" type="Magento\Framework\Logger\Handler\Base">
        <arguments>
            <argument name="fileName" xsi:type="string">/var/log/forward_order_pricing.log</argument>
        </arguments>
    </virtualType>

    <!-- Console Commands -->
    <type name="Magento\Framework\Console\CommandListInterface">
        <arguments>
            <argument name="commands" xsi:type="array">
                <!-- Future console commands will be added here -->
            </argument>
        </arguments>
    </type>

    <!-- Plugin to prevent inventory reservations for forward orders -->
    <!-- sortOrder="1" ensures this runs before any other plugins including Myoba's preference -->
    <type name="Magento\InventorySalesApi\Model\PlaceReservationsForSalesEventInterface">
        <plugin name="zhik_forward_order_prevent_reservation"
                type="Zhik\ForwardOrder\Plugin\InventorySales\PreventForwardOrderReservation"
                sortOrder="1" />
    </type>

    <!-- Plugin to prevent legacy stock deduction for forward orders -->
    <!-- This prevents the SubtractQuoteInventoryObserver from reducing stock -->
    <type name="Magento\CatalogInventory\Observer\SubtractQuoteInventoryObserver">
        <plugin name="zhik_forward_order_prevent_stock_deduction"
                type="Zhik\ForwardOrder\Plugin\CatalogInventory\Observer\PreventStockDeduction"
                sortOrder="1" />
    </type>

    <!-- Plugin to bypass stock validation for forward order quotes -->
    <type name="Magento\CatalogInventory\Api\StockStateInterface">
        <plugin name="zhik_forward_order_bypass_stock_validation"
                type="Zhik\ForwardOrder\Plugin\Quote\BypassStockValidation"
                sortOrder="10" />
    </type>

    <!-- Plugin to bypass stock check for product salability -->
    <type name="Magento\CatalogInventory\Model\Stock\Item">
        <plugin name="zhik_forward_order_bypass_stock_check"
                type="Zhik\ForwardOrder\Plugin\Quote\BypassStockValidation"
                sortOrder="10" />
    </type>

    <!--
        IMPORTANT: These plugins are intentionally disabled - functionality works through alternative implementation

        ForwardOrderPricing Plugin (DISABLED - NOT NEEDED):
        - Forward order prices are loaded directly from database in ForwardOrderGrid::getForwardOrderPriceList()
        - This approach is safer (no Portal contamination) and more efficient
        - Verified working: Prices display correctly as "FO PRICE" in frontend

        BypassSalabilityCheck Plugin (DISABLED - UNDER EVALUATION):
        - Stock bypass is handled by active BypassStockValidation plugin
        - Product availability may already be covered by existing plugins
        - Enable only if products cannot be added to cart when out of stock

        Original global approach caused price bleeding between Portal and Forward Order forms.
        Current implementation uses targeted plugins and direct DB queries for better isolation.
        Tested and verified working with order AUT000031587 (2025-01-28)
    -->
    <!--
    <type name="Magento\Catalog\Model\Product">
        <plugin name="zhik_forward_order_product_pricing"
                type="Zhik\ForwardOrder\Plugin\Catalog\Product\ForwardOrderPricing"
                sortOrder="10" />
        <plugin name="zhik_forward_order_bypass_salability"
                type="Zhik\ForwardOrder\Plugin\Catalog\Product\BypassSalabilityCheck"
                sortOrder="5" />
    </type>
    -->

    <!-- Disabled: Session-based context causes conflicts when both forms open in tabs -->
    <!--
    <type name="Magento\Framework\App\FrontControllerInterface">
        <plugin name="zhik_forward_order_clear_session_flag"
                type="Zhik\ForwardOrder\Plugin\Session\ClearForwardOrderFlag"
                sortOrder="1" />
    </type>
    -->

    <!-- PriceHelper Configuration -->
    <type name="Zhik\ForwardOrder\Helper\PriceHelper">
        <arguments>
            <argument name="logger" xsi:type="object">Zhik\ForwardOrder\Model\Logger</argument>
            <argument name="storeManager" xsi:type="object">Magento\Store\Model\StoreManagerInterface</argument>
        </arguments>
    </type>

    <!-- ForwardOrderGrid Block Configuration -->
    <virtualType name="Zhik\ForwardOrder\Block\Order\ForwardOrderGrid\Virtual" type="Zhik\ForwardOrder\Block\Order\ForwardOrderGrid">
        <arguments>
            <argument name="resourceConnection" xsi:type="object">Magento\Framework\App\ResourceConnection</argument>
            <argument name="logger" xsi:type="object">Zhik\ForwardOrder\Model\Logger</argument>
        </arguments>
    </virtualType>

    <!-- Category Block Configuration -->
    <type name="Zhik\ForwardOrder\Block\Order\Supergrid\Category">
        <arguments>
            <argument name="logger" xsi:type="object">Zhik\ForwardOrder\Model\Logger</argument>
        </arguments>
    </type>

    <!-- Ordering Period Management -->
    <type name="Zhik\ForwardOrder\Model\OrderingPeriod">
        <arguments>
            <argument name="logger" xsi:type="object">Zhik\ForwardOrder\Model\Logger</argument>
        </arguments>
    </type>

    <!-- Forward Order Access Plugin -->
    <type name="Zhik\ForwardOrder\Controller\Order\Index">
        <plugin name="zhik_forward_order_access_control"
                type="Zhik\ForwardOrder\Plugin\Controller\ForwardOrderAccess"
                sortOrder="5" />
    </type>

    <!-- Ordering Period Helper -->
    <type name="Zhik\ForwardOrder\Helper\OrderingPeriod">
        <arguments>
            <argument name="orderingPeriod" xsi:type="object">Zhik\ForwardOrder\Model\OrderingPeriod</argument>
        </arguments>
    </type>

    <!-- Portal Navigation Plugin -->
    <!-- Adds "Create Forward Order" link conditionally based on customer eligibility -->
    <type name="Zhik\Portal\Block\Account\Navigation">
        <plugin name="zhik_forward_order_add_navigation"
                type="Zhik\ForwardOrder\Plugin\Portal\Block\Account\AddForwardOrderNavigation"
                sortOrder="100" />
    </type>

    <!-- Myoba Price Type Plugin -->
    <!-- Intercepts getMyobPrice() to use F prices for forward orders -->
    <type name="Zhik\Myoba\Helper\Data">
        <plugin name="zhik_forward_order_price_type"
                type="Zhik\ForwardOrder\Plugin\Myoba\PriceTypePlugin"
                sortOrder="10" />
    </type>
</config>