<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

namespace Magetop\Advancepreview\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $setup->startSetup();
        if (version_compare($context->getVersion(), '2.5.3') < 0) {
            $magetopPreviewProductHistory = $installer->getTable('magetop_preview_product_history');
            if ($setup->getConnection()->isTableExists($magetopPreviewProductHistory) != true) {
                $tableMagetopPreviewProductHistory = $setup->getConnection()
                    ->newTable($magetopPreviewProductHistory)
                    ->addColumn(
                        'history_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                        'Id'
                    )
                    ->addColumn(
                        'product_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['nullable' => false],
                        'Quote Item id'
                    )->addColumn(
                        'customer_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['nullable' => false],
                        'Customer Id'
                    )
                    ->addColumn(
                        'last_access',
                        \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
                        null,
                        ['nullable' => true],
                        'Time Access'
                    )
                    ->setComment('Magetop Product History')
                    ->setOption('type', 'InnoDB')
                    ->setOption('charset', 'utf8');
                $setup->getConnection()->createTable($tableMagetopPreviewProductHistory);
            }
        }
        if (version_compare($context->getVersion(), '2.5.4') < 0) {
            $model3D = $installer->getTable('magetop_3d_lists');
            if ($setup->getConnection()->isTableExists($model3D) != true) {
                $tableModel3D  = $setup->getConnection()
                    ->newTable($model3D)
                    ->addColumn(
                        'threed_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                        'Id'
                    )
                    ->addColumn(
                        'product_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['nullable' => false, 'default' => 0],
                        'Quote Item id'
                    )
                    ->addColumn(
                        'type',
                        \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                        null,
                        ['nullable' => false, 'default' => 0],
                        'TYpe id'
                    )
                    ->addColumn(
                        'created_at',
                        \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
                        null,
                        ['nullable' => true],
                        'Create At'
                    )
                    ->setComment('3D model')
                    ->setOption('type', 'InnoDB')
                    ->setOption('charset', 'utf8');
                $setup->getConnection()->createTable($tableModel3D);
            }
        }
        if (version_compare($context->getVersion(), '2.5.5') < 0) {
            $preview3dTable = $installer->getTable('magetop_3d_lists');
            // Check if the table already exists
            if ($setup->getConnection()->isTableExists($preview3dTable) == true) {
                // Declare data
                $columns = [
                    'title' => [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        ['nullable' => true, 'default' => ''],
                        'comment' => 'title',

                    ],
                ];
                $connection = $setup->getConnection();
                foreach ($columns as $name => $definition) {
                    $connection->addColumn($preview3dTable, $name, $definition);
                }
            }
        }
        if (version_compare($context->getVersion(), '2.5.6') < 0) {
            $preview3dTable = $installer->getTable('magetop_3d_lists');
            // Check if the table already exists
            if ($setup->getConnection()->isTableExists($preview3dTable) == true) {
                // Declare data
                $columns = [
                    'filename' => [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        ['nullable' => true, 'default' => ''],
                        'comment' => 'filename',

                    ],
                ];
                $connection = $setup->getConnection();
                foreach ($columns as $name => $definition) {
                    $connection->addColumn($preview3dTable, $name, $definition);
                }
            }
        }
        if (version_compare($context->getVersion(), '2.5.7') < 0) {
            $previewCustomDiscount = $installer->getTable('magetop_preview_custom_discount');
            if ($setup->getConnection()->isTableExists($previewCustomDiscount) != true) {
                $tablePreviewCustomDiscount  = $setup->getConnection()
                    ->newTable($previewCustomDiscount)
                    ->addColumn(
                        'discount_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                        'Id'
                    )
                    ->addColumn(
                        'product_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['nullable' => false, 'default' => 0],
                        'Product Id'
                    )
                    ->addColumn(
                        'discount_title',
                        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        500,
                        ['nullable' => false, 'default' => ''],
                        'Discount Title'
                    )
                    ->addColumn(
                        'discount_price',
                        \Magento\Framework\DB\Ddl\Table::TYPE_FLOAT,
                        null,
                        ['nullable' => false, 'default' => 0],
                        'Discount Price'
                    )
                    ->addColumn(
                        'discount_amount',
                        \Magento\Framework\DB\Ddl\Table::TYPE_FLOAT,
                        null,
                        ['nullable' => false, 'default' => 0],
                        'Discount amount'
                    )
                    ->addColumn(
                        'created_at',
                        \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
                        null,
                        ['nullable' => true],
                        'Create At'
                    )
                    ->setComment('Magetop Preview Discount Table')
                    ->setOption('type', 'InnoDB')
                    ->setOption('charset', 'utf8');
                $setup->getConnection()->createTable($tablePreviewCustomDiscount);
            }
        }
        if (version_compare($context->getVersion(), '2.5.8') < 0) {
            $previewAdditionalOptions = $installer->getTable('magetop_preview_additional_option');
            if ($setup->getConnection()->isTableExists($previewAdditionalOptions) != true) {
                $tablePreviewAdditionalOptions  = $setup->getConnection()
                    ->newTable($previewAdditionalOptions)
                    ->addColumn(
                        'addition_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                        'Id'
                    )
                    ->addColumn(
                        'product_id',
                        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                        null,
                        ['nullable' => false, 'default' => 0],
                        'Product Id'
                    )
                    ->addColumn(
                        'status',
                        \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                        null,
                        ['nullable' => false, 'default' => 1],
                        'Status Id'
                    )
                    ->addColumn(
                        'title',
                        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        500,
                        ['nullable' => false, 'default' => ''],
                        'Title'
                    )
                    ->addColumn(
                        'description',
                        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        500,
                        ['nullable' => false, 'default' => ''],
                        'description'
                    )
                    ->addColumn(
                        'price',
                        \Magento\Framework\DB\Ddl\Table::TYPE_FLOAT,
                        null,
                        ['nullable' => false, 'default' => 0],
                        'Price'
                    )
                    ->addColumn(
                        'created_at',
                        \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME,
                        null,
                        ['nullable' => true],
                        'Create At'
                    )
                    ->setComment('Magetop Preview Discount Table')
                    ->setOption('type', 'InnoDB')
                    ->setOption('charset', 'utf8');
                $setup->getConnection()->createTable($tablePreviewAdditionalOptions);
            }
        }
        $setup->endSetup();
    }
}
