<?php
declare(strict_types=1);

namespace FiloBlu\ExtBlackbirdMenuManager\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;

/**
 * @codeCoverageIgnore
 */
class UpgradeSchema implements UpgradeSchemaInterface
{

    /** @var string */
    const BLACKBIRD_MENUMANAGER_NODE_TABLE = 'blackbird_menumanager_node';
    /** @var string */
    const BLACKBIRD_MENUMANAGER_MENU_TABLE = 'blackbird_menumanager_menu';

    /**
     * {@inheritdoc}
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        if (!$context->getVersion()) {
            return;
        }

        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.1.0') < 0) {
            $this->addCustomerGroupInNodeTable($setup);
            $this->addCustomerGroupInMenuTable($setup);
        }

        if (version_compare($context->getVersion(), '1.2.0') < 0) {
            $this->addMaxItemInNodeTable($setup);
        }

        if (version_compare($context->getVersion(), '1.2.1') < 0) {
            $this->addIdentifierNodeColumn($setup);
        }

        $setup->endSetup();
    }

    /**
     * @param \Magento\Framework\Setup\SchemaSetupInterface $setup
     * @return void
     */
    public function addIdentifierNodeColumn(SchemaSetupInterface $setup)
    {
        $connection = $setup->getConnection();
        if (!$connection->tableColumnExists(self::BLACKBIRD_MENUMANAGER_NODE_TABLE, 'identifier')) {
            $connection->addColumn(
                $setup->getTable(self::BLACKBIRD_MENUMANAGER_NODE_TABLE),
                'identifier',
                [
                    'type'     => Table::TYPE_TEXT,
                    'length'   => 255,
                    'nullable' => true,
                    'comment'  => 'Node Identifier'
                ]
            );
        }
    }

    /**
     * @param SchemaSetupInterface $setup
     * @return void
     */
    protected function addCustomerGroupInNodeTable(SchemaSetupInterface $setup)
    {
        $connection = $setup->getConnection();
        if ($connection->tableColumnExists(self::BLACKBIRD_MENUMANAGER_NODE_TABLE, 'customer_group') === false) {
            $connection->addColumn(
                $setup->getTable(self::BLACKBIRD_MENUMANAGER_NODE_TABLE),
                'customer_group',
                [
                    'type' => Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => true,
                    'comment' => 'Customer Group'
                ]
            );
        }
    }

    /**
     * @param SchemaSetupInterface $setup
     * @return void
     */
    protected function addCustomerGroupInMenuTable(SchemaSetupInterface $setup)
    {
        $connection = $setup->getConnection();
        if ($connection->tableColumnExists(self::BLACKBIRD_MENUMANAGER_MENU_TABLE, 'customer_group') === false) {
            $connection->addColumn(
                $setup->getTable(self::BLACKBIRD_MENUMANAGER_MENU_TABLE),
                'customer_group',
                [
                    'type' => Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => true,
                    'comment' => 'Customer Group'
                ]
            );
        }
    }

    /**
     * @param $setup
     * @return void
     */
    protected function addMaxItemInNodeTable($setup)
    {
        $connection = $setup->getConnection();
        if ($connection->tableColumnExists(self::BLACKBIRD_MENUMANAGER_NODE_TABLE, 'max_item_column') === false) {
            $connection->addColumn(
                $setup->getTable(self::BLACKBIRD_MENUMANAGER_NODE_TABLE),
                'max_item_column',
                [
                    'type' => Table::TYPE_TEXT,
                    'comment' => 'Max items for column'
                ]
            );
        }
    }
}
