<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiAdmin\Changes\ChangeExtension\Product;

use KupShop\GraphQLBundle\ApiAdmin\Changes\ChangeExtension\AbstractChangeExtension;
use KupShop\GraphQLBundle\ApiAdmin\Types\Changes\ChangeInterface;
use KupShop\GraphQLBundle\ApiAdmin\Types\Changes\Product\ProductQuantityChange;
use KupShop\KafkaBundle\DBChanges\MessageTypes\DBGenericMessage;

class ProductQuantityExtension extends AbstractChangeExtension
{
    public function createChange(DBGenericMessage $message): ChangeInterface
    {
        return match ($message->getTableName()) {
            'products' => $this->createProductChange($message),
            'products_variations' => $this->createProductVariationChange($message),
            'stores_items' => $this->createStoreItemChange($message),
            default => throw new \RuntimeException('Unknown message type: '.$message->getTableName()),
        };
    }

    protected function getFieldsMap(): array
    {
        if (!findModule(\Modules::STORES)) {
            return [
                'products' => [
                    'in_store' => 'quantity',
                ],
                'products_variations' => [
                    'in_store' => 'quantity',
                ],
            ];
        }

        return [
            'stores_items' => [
                'quantity' => 'quantity',
            ],
        ];
    }

    private function createStoreItemChange(DBGenericMessage $message): ProductQuantityChange
    {
        [$quantity, $quantityPrevious] = $this->getCurrentAndPreviousValue($message, 'quantity', 0);

        return new ProductQuantityChange(
            metadata: $this->createMetadata($message),
            productId: $message->getValue('id_product'),
            variationId: $message->getValue('id_variation'),
            storeId: $message->getValue('id_store'),
            quantity: $quantity,
            quantityPrevious: $quantityPrevious,
        );
    }

    private function createProductChange(DBGenericMessage $message): ProductQuantityChange
    {
        [$quantity, $quantityPrevious] = $this->getCurrentAndPreviousValue($message, 'in_store', 0);

        return new ProductQuantityChange(
            metadata: $this->createMetadata($message),
            productId: $message->getValue('id'),
            variationId: null,
            storeId: null,
            quantity: $quantity,
            quantityPrevious: $quantityPrevious,
        );
    }

    private function createProductVariationChange(DBGenericMessage $message): ProductQuantityChange
    {
        [$quantity, $quantityPrevious] = $this->getCurrentAndPreviousValue($message, 'in_store', 0);

        return new ProductQuantityChange(
            metadata: $this->createMetadata($message),
            productId: $message->getValue('id_product'),
            variationId: $message->getValue('id'),
            storeId: null,
            quantity: $quantity,
            quantityPrevious: $quantityPrevious,
        );
    }
}
