<?php
/**
 * Mirasvit
 *
 * This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
 * Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
 * If you wish to customize this module for your needs.
 * Please refer to http://www.magentocommerce.com for more information.
 *
 * @category  Mirasvit
 * @package   mirasvit/module-report-api
 * @version   1.0.91
 * @copyright Copyright (C) 2026 Mirasvit (https://mirasvit.com/)
 */



namespace Mirasvit\ReportApi\Service;

use Magento\Directory\Model\Currency;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\StoreManagerInterface;
use Mirasvit\ReportApi\Api\RequestInterface;

class StoreResolver
{
    /** @var string|null */
    private static $baseCurrencyCode = null;

    /** @var StoreManagerInterface */
    private $storeManager;

    private const STORE_ID_COLUMNS = [
        'sales_order|store_id',
        'sales_order_item|store_id',
        'sales_invoice|store_id',
        'sales_creditmemo|store_id',
        'sales_shipment|store_id',
        'quote|store_id',
        'quote_item|store_id',
    ];

    public function __construct(StoreManagerInterface $storeManager)
    {
        $this->storeManager = $storeManager;
    }

    public function registerRequest(RequestInterface $request): void
    {
        self::$baseCurrencyCode = null;

        foreach ($request->getFilters() as $filter) {
            if (in_array($filter->getColumn(), self::STORE_ID_COLUMNS, true)) {
                $value    = $filter->getValue();
                $storeIds = is_array($value) ? array_map('intval', $value) : [(int)$value];
                $storeIds = array_filter($storeIds, function ($id) {
                    return $id > 0;
                });

                if (!empty($storeIds)) {
                    self::$baseCurrencyCode = $this->resolveBaseCurrency($storeIds);
                }

                break;
            }
        }
    }

    public function isResolved(): bool
    {
        return self::$baseCurrencyCode !== null;
    }

    public function getCurrencyModel(): Currency
    {
        $currencyCode = self::$baseCurrencyCode
            ?? $this->storeManager->getStore(0)->getBaseCurrencyCode();

        return ObjectManager::getInstance()
            ->create(Currency::class)
            ->load($currencyCode);
    }

    public function getGlobalCurrencyModel(): Currency
    {
        return ObjectManager::getInstance()
            ->create(Currency::class)
            ->load($this->storeManager->getStore(0)->getBaseCurrencyCode());
    }

    private function resolveBaseCurrency(array $storeIds): ?string
    {
        $baseCurrencyCode = null;

        foreach ($storeIds as $storeId) {
            $storeCurrency = $this->storeManager->getStore($storeId)->getBaseCurrencyCode();

            if ($baseCurrencyCode === null) {
                $baseCurrencyCode = $storeCurrency;
            } elseif ($baseCurrencyCode !== $storeCurrency) {
                return null;
            }
        }

        return $baseCurrencyCode;
    }
}
