<?php

namespace TbiBank\FusionPay\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Catalog\Model\Product;

class ProductHelper extends AbstractHelper
{
    protected $productRepository;
    protected $configurableProductType;

    public function __construct(
        Context $context,
        ProductRepositoryInterface $productRepository,
        Configurable $configurableProductType
    ) {
        parent::__construct($context);
        $this->productRepository = $productRepository;
        $this->configurableProductType = $configurableProductType;
    }

    /**
     * Retrieves all possible combinations of a configurable product's options as separate payloads.
     *
     * @param int $productId The ID of the main product.
     * @return array An array containing possible selection payloads for each combination.
     */
    public function getAllConfigurableCombinations($productId)
    {
        $product = $this->productRepository->getById($productId);
        $allCombinations = [];

        if ($product->getTypeId() === Configurable::TYPE_CODE) {
            $attributes = $this->configurableProductType->getConfigurableAttributes($product);
            $options = [];
            foreach ($attributes as $attribute) {
                $attrId = $attribute->getProductAttribute()->getAttributeId();
                foreach ($attribute->getOptions() as $option) {
                    $options[$attrId][] = $option['value_index'];
                }
            }

            // Generate all possible combinations of these options
            $combinations = $this->generateCombinations($options);

            foreach ($combinations as $combination) {
                $payload = [
                    'form_key' => 'exampleFormKey', // This should be dynamically generated in production
                    'item' => $productId,
                    'product' => $productId,
                    'qty' => '1',
                    'selected_configurable_option' => '',
                ];

                foreach ($combination as $attrId => $valueId) {
                    $payload['super_attribute[' . $attrId . ']'] = $valueId;
                }

                $allCombinations[] = $payload;
            }
        }

        return $allCombinations;
    }

    /**
     * Generates all combinations from attributes and their values.
     *
     * @param array $options Options with attribute IDs as keys and arrays of possible values.
     * @return array All possible combinations.
     */
    private function generateCombinations($options, $combo = [])
    {
        $keys = array_keys($options);
        $result = [];

        if (empty($combo) && !empty($options)) {
            $firstKey = array_shift($keys);
            foreach ($options[$firstKey] as $val) {
                $result = array_merge($result, $this->generateCombinations($options, [$firstKey => $val]));
            }
        } elseif (!empty($options)) {
            $firstKey = array_shift($keys);
            foreach ($options[$firstKey] as $val) {
                foreach ($combo as $cKey => $cVal) {
                    $result[] = array_merge([$firstKey => $val], [$cKey => $cVal]);
                }
            }
        } else {
            $result[] = $combo;
        }

        return $result;
    }
}
